summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/MapHelper.java
blob: 6c0763d065fcd14c913564ae4c50902d907a2c28 (plain) (blame)
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
package org.openslx.dozmod.util;

import java.util.Map;
import java.util.Map.Entry;

public class MapHelper {

	private MapHelper() {
	}

	public static <K, T> boolean hasChanged(final Map<K, T> oldMap, final Map<K, T> newMap) {
		return compare(oldMap, newMap) != 0;
	}
	
	public static <K, T> int compare(final Map<K, T> oldMap, final Map<K, T> newMap) {
		if (oldMap == null && newMap == null)
			return 0;
		if (oldMap == null)
			return -1;
		if (newMap == null)
			return 1;
		if (oldMap.size() != newMap.size())
			return oldMap.size() - newMap.size();

		// Same amount of entries, check if any entry changed
		int retval = 0;
		for (Entry<K, T> entry : oldMap.entrySet()) {
			T current = entry.getValue();
			T other = newMap.get(entry.getKey());
			if (other == null) {
				retval -= 1;
				continue;
			}
			retval += current.hashCode() - other.hashCode();
		}
		return retval;
	}

}