summaryrefslogtreecommitdiff
path: root/src/core/src/data/MVDataEntry.java
blob: 2d92030a792ccc9b614d92660dccc51f2760d144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * SSSync, a Simple and Stupid Synchronizer for data with multi-valued attributes
 * Copyright (C) 2014  Ludovic Pouzenc <ludovic@pouzenc.fr>
 *  
 * This file is part of SSSync.
 *
 *  SSSync is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SSSync is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with SSSync.  If not, see <http://www.gnu.org/licenses/>
 */

package data;

import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;

import com.google.common.collect.HashMultimap;

/**
 * Generic Multi-Valued data type. Each object store a particular entry.
 * Semantics are like in LDAP directories : an entry = a key + a set of multi-valued attributes.
 * Relational data like in RDMS are more constrained : columns are fixed for an entire table.
 * Null and empty string attribute value are silently discarded.
 * 
 * @author lpouzenc
 */
public class MVDataEntry implements Comparable<MVDataEntry> {

	/** 
	 * The key part that identify this particular entry.
	 */
	private final String key;
	/**
	 * The data part of this particular entry.
	 */
	private HashMultimap<String,String> attrValPairs; //FIXME : memory inefficient 
	//XXX Consider https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/map/MultiValueMap.html
	
	// Constructors
	
	/**
	 * Build a fresh empty MVDataEntry.
	 * @param key Unique key identifying this entry
	 */
	public MVDataEntry(String key) {
		if ( key == null ) {
			throw new IllegalArgumentException("key must be non-null");
		}
		this.key = key;
		this.attrValPairs = HashMultimap.create();
	}
	
	/**
	 * Build a fresh empty MVDataEntry with hints about expected attr/values count.
	 * @param key Unique key identifying this entry
	 */
	public MVDataEntry(String key, int expectedAttribs, int expectedValuesPerAttrib) {
		if ( key == null ) {
			throw new IllegalArgumentException("key must be non-null");
		}
		this.key = key;
		this.attrValPairs = HashMultimap.create(expectedAttribs, expectedValuesPerAttrib);
	}
	
	/**
	 * Deep copy of an existing MVDataEntry.
	 * @param key Unique key identifying this entry
	 */
	public MVDataEntry(final MVDataEntry copyFrom) {
		this.key=copyFrom.key; // String is immutable, so ref copy is okay
		this.attrValPairs = HashMultimap.create(copyFrom.attrValPairs);
	}	
	
	/**
	 * Proxy function to return all attribute/value pairs.
	 *  One can use read a MVDataEntry without depending on non-standard HashMultimap.
	 * @return 
	 */
	public Set<Entry<String, String>> getAllEntries() {
		return this.attrValPairs.entries();
	}	
	
	/**
	 * Proxy function to add an attribute/value pair in attrValPairs.
	 *  One can use MVDataEntry without depending on non-standard HashMultimap.
	 * 
	 * @param attr
	 * @param value
	 */
	public void put(String attr, String... values) {
		for (String value: values) {
			if ( value != null && !value.isEmpty() ) {
				this.attrValPairs.put(attr, value);
			}
		}
	}

	/**
	 * Proxy function to get all values from a particular attribute.
	 *  One can use MVDataEntry without depending on non-standard HashMultimap.
	 * @param attr
	 * @return
	 */
	public Set<String> getValues(String attr) {
		return this.attrValPairs.get(attr);
	}
	
	/**
	 * Helper function to insert multiple values from a single string.
	 * 
	 * @param attr
	 * @param value
	 * @param splitRegex
	 */
	public void splitAndPut(String attr, String value, String splitRegex) {
		if ( value != null ) {
			for (String v : value.split(splitRegex)) {
				put(attr, v);
			}
		}
	}
	
	/**
	 * Helper function to return list of changed attributes.
	 * Note : this don't keep track of deleted attributes.
	 * @param original
	 * @return
	 */
	public Set<String> getChangedAttributes(MVDataEntry original) {
		HashSet<String> result = new HashSet<String>();
		
		for (String attr: this.attrValPairs.keySet()) {
			Set<String> thisValue = this.attrValPairs.get(attr);
			Set<String> originalValue = original.attrValPairs.get(attr);
			if ( ! thisValue.equals(originalValue) ) {
				result.add(attr);
			}
		}
			
		return result;
	}

	/**
	 * Augment this entry with attr/values from other entries.
	 * @param appendMode Select behavior on an existing attribute : append values or replace them
	 * @param entries Entries to merge with current entry 
	 */
	public void mergeValues(boolean appendMode, MVDataEntry... entries) {
		for(MVDataEntry entry : entries) {
			if ( ! appendMode ) {
				for (String attr : entry.attrValPairs.keySet()) {
					this.attrValPairs.removeAll(attr);
				}
			}
			this.attrValPairs.putAll(entry.attrValPairs);
		}		
	}
	
	/**
	 * Check if this entry seems contains useful data.
	 * @return true if this entry seems contains useful data
	 */
	public boolean isValid() {
		boolean validKey=(this.key != null && this.key.length() > 0 );
		boolean validVal=(this.attrValPairs != null && ! this.attrValPairs.isEmpty());
		
		return (validKey && validVal);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object obj) {
		// Check for self-comparison (compare object references)
		if ( this == obj ) { return true; }
		// Check non-nullity and type
		if ( !( obj instanceof MVDataEntry) ) { return false; }
		// Cast safely
		MVDataEntry other = (MVDataEntry) obj;
		// Check all fields (known to be always non null)
		return ( this.key.equals(other.key) && this.attrValPairs.equals(other.attrValPairs) );
	}

	/**
	 * Compares entries. Ordering of entries is the ordering of their keys.
	 * (java.lang.String default ordering : lexicographical ascending order) 
	 */
	@Override
	public int compareTo(MVDataEntry other) {
		return this.key.compareTo(other.key);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		return "{key=" + key + ", attrValPairs=" + attrValPairs.toString() + "}";
	}

	
	// Boring accessors
	/**
	 * @return the attrValPairs
	 */
	public HashMultimap<String, String> getAttrValPairs() {
		return attrValPairs;
	}

	/**
	 * @param attrValPairs the attrValPairs to set
	 */
	public void setAttrValPairs(HashMultimap<String, String> attrValPairs) {
		this.attrValPairs = attrValPairs;
	}

	/**
	 * @return the key (guaranteed to be non-null)
	 */
	public String getKey() {
		return key;
	}



}