Project

General

Profile

1 26600 sandro.lab
package eu.dnetlib.data.mapreduce.util;
2
3
import org.apache.commons.lang.StringUtils;
4
import org.apache.commons.logging.Log;
5
import org.apache.commons.logging.LogFactory;
6
import org.apache.hadoop.io.Text;
7
8
import eu.dnetlib.data.proto.TypeProtos.Type;
9
10
public class OafRowKeyDecoder {
11
12
	/**
13
	 * logger.
14
	 */
15
	private static final Log log = LogFactory.getLog(OafRowKeyDecoder.class); // NOPMD by marko on 11/24/08 5:02 PM
16
17
	private static final String SEPARATOR = "|";
18
19
	public final static String ID_REGEX = "^[0-9][0-9]\\|.{12}::[a-zA-Z0-9]{32}$";
20
21
	private String key;
22
23
	private Type type = null;
24
25
	private String id = null;
26
27
	public static OafRowKeyDecoder decode(final byte[] key) throws IllegalArgumentException {
28
		return new OafRowKeyDecoder(new String(key));
29
	}
30
31
	public static OafRowKeyDecoder decode(final String key) throws IllegalArgumentException {
32
		return new OafRowKeyDecoder(key);
33
	}
34
35
	private OafRowKeyDecoder(final String key) throws IllegalArgumentException {
36
		this.key = key;
37
38
		if (!key.matches(ID_REGEX)) {
39
			String msg = "invalid key: '" + key + "'";
40
			log.error(msg);
41
			throw new IllegalArgumentException(msg);
42
		}
43
44
		int tag = Integer.parseInt(StringUtils.substringBefore(key, SEPARATOR));
45
		this.type = Type.valueOf(tag);
46
		this.id = StringUtils.substringAfter(key, SEPARATOR);
47
48
		// System.out.println(OafRowTypeDecoder.class.getName() +" decoded key: " + split);
49
	}
50
51
	public String getKey() {
52
		return key;
53
	}
54
55
	public Text getKeyAsText() {
56
		return new Text(key);
57
	}
58
59
	public Type getType() {
60
		return type;
61
	}
62
63
	public String getId() {
64
		return id;
65
	}
66
}