Project

General

Profile

1
package eu.dnetlib.pace.model;
2

    
3
import java.util.Map;
4
import java.util.Set;
5

    
6
import com.google.common.collect.Iterables;
7
import com.google.common.collect.Lists;
8
import com.google.common.collect.Maps;
9

    
10
/**
11
 * The Class MapDocument.
12
 */
13
public class MapDocument implements Document {
14

    
15
	/** The identifier. */
16
	private String identifier;
17

    
18
	/** The field map. */
19
	private Map<String, Field> fieldMap;
20

    
21
	/**
22
	 * Instantiates a new map document.
23
	 */
24
	public MapDocument() {
25
		identifier = null;
26
		fieldMap = Maps.newHashMap();
27
	}
28

    
29
	/**
30
	 * Instantiates a new map document.
31
	 *
32
	 * @param identifier
33
	 *            the identifier
34
	 * @param fieldMap
35
	 *            the field map
36
	 */
37
	public MapDocument(final String identifier, final Map<String, Field> fieldMap) {
38
		this.setIdentifier(identifier);
39
		this.fieldMap = fieldMap;
40
	}
41

    
42
	/**
43
	 * Instantiates a new map document.
44
	 *
45
	 * @param identifier
46
	 *            the identifier
47
	 * @param data
48
	 *            the data
49
	 */
50
	public MapDocument(final String identifier, final byte[] data) {
51
		final MapDocument doc = MapDocumentSerializer.decode(data);
52

    
53
		this.fieldMap = doc.fieldMap;
54
		this.identifier = doc.identifier;
55
	}
56

    
57
	/*
58
	 * (non-Javadoc)
59
	 * 
60
	 * @see eu.dnetlib.pace.model.document.Document#fields()
61
	 */
62
	@Override
63
	public Iterable<Field> fields() {
64
		return Lists.newArrayList(Iterables.concat(fieldMap.values()));
65
	}
66

    
67
	/*
68
	 * (non-Javadoc)
69
	 * 
70
	 * @see eu.dnetlib.pace.model.document.Document#values(java.lang.String)
71
	 */
72
	@Override
73
	public Field values(final String name) {
74
		return fieldMap.get(name);
75
	}
76

    
77
	/*
78
	 * (non-Javadoc)
79
	 * 
80
	 * @see eu.dnetlib.pace.model.document.Document#fieldNames()
81
	 */
82
	@Override
83
	public Set<String> fieldNames() {
84
		return fieldMap.keySet();
85
	}
86

    
87
	/*
88
	 * (non-Javadoc)
89
	 * 
90
	 * @see java.lang.Object#toString()
91
	 */
92
	@Override
93
	public String toString() {
94
		return MapDocumentSerializer.toString(this);
95
		// return String.format("Document(%s)", fieldMap.toString());
96
	}
97

    
98
	/**
99
	 * To byte array.
100
	 *
101
	 * @return the byte[]
102
	 */
103
	public byte[] toByteArray() {
104
		return MapDocumentSerializer.toByteArray(this);
105
	}
106

    
107
	/*
108
	 * (non-Javadoc)
109
	 * 
110
	 * @see eu.dnetlib.pace.model.document.Document#getIdentifier()
111
	 */
112
	@Override
113
	public String getIdentifier() {
114
		return identifier;
115
	}
116

    
117
	/**
118
	 * Sets the identifier.
119
	 *
120
	 * @param identifier
121
	 *            the new identifier
122
	 */
123
	public void setIdentifier(final String identifier) {
124
		this.identifier = identifier;
125
	}
126

    
127
	/**
128
	 * Gets the field map.
129
	 *
130
	 * @return the field map
131
	 */
132
	public Map<String, Field> getFieldMap() {
133
		return fieldMap;
134
	}
135

    
136
	/**
137
	 * Sets the field map.
138
	 *
139
	 * @param fieldMap
140
	 *            the field map
141
	 */
142
	public void setFieldMap(final Map<String, Field> fieldMap) {
143
		this.fieldMap = fieldMap;
144
	}
145

    
146
}
(11-11/15)