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

                                

                           
 

                        

                             
 




                                                                                                 
                                                     




                                   
                                                   
                                                             
 


                                                                     

                                                             




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

}