summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/util/MapHelper.java
blob: 61133cd91ce597f3a7b4a4a210d36c2e40971939 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                                

                           






                                                                               

                             
 




                                                                                                 
                                                     




                                   
                                                   
                                                             
 


                                                                     

                                                             




                                                                        
                 
                              
         
 
 
package org.openslx.dozmod.util;

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

import org.apache.log4j.Logger;

public class MapHelper {

	private static final Logger LOGGER = Logger.getLogger(MapHelper.class);

	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;
	}

}