Last active
March 10, 2018 15:33
-
-
Save fupfin/6f51048e147c15ee49bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class HashCodeFunctions | |
{ | |
private final static short DEFAULT_PRIME_NUMBER = 31; | |
private HashCodeFunctions() | |
{ | |
throw new AssertionError("Could not be instantiated."); | |
} | |
public static | |
int hash(boolean value) { return (value ? 0 : 1); } | |
public static | |
int hash(byte value) { return value; } | |
public static | |
int hash(char value) { return value; } | |
public static | |
int hash(short value) { return value; } | |
public static | |
int hash(int value) { return value; } | |
public static | |
int hash(long value) { return ((int) (value ^ (value >> 32))); } | |
public static | |
int hash(float value) { return hash(Float.floatToIntBits(value)); } | |
public static | |
int hash(double value) { return hash(Double.doubleToLongBits(value)); } | |
public static | |
int hash(Object value) { return value.hashCode(); } | |
public static | |
int hash(boolean[] values) { return Arrays.hashCode(values); } | |
public static | |
int hash(byte[] values) { return Arrays.hashCode(values); } | |
public static | |
int hash(char[] values) { return Arrays.hashCode(values); } | |
public static | |
int hash(short[] values) { return Arrays.hashCode(values); } | |
public static | |
int hash(int[] values) { return Arrays.hashCode(values); } | |
public static | |
int hash(long[] values) { return Arrays.hashCode(values); } | |
public static | |
int hash(float[] values) { return Arrays.hashCode(values); } | |
public static | |
int hash(double[] values) { return Arrays.hashCode(values); } | |
public static | |
int hash(Object[] values) { return Arrays.hashCode(values); } | |
public static | |
int totalHash(int... hashcodes) | |
{ | |
return totalHash(0, DEFAULT_PRIME_NUMBER, hashcodes); | |
} | |
public static | |
int totalHash(int init, short prime, int... hashcodes) | |
{ | |
int result = init; | |
for(int hashcode: hashcodes) | |
result = result * prime + hashcode; | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HashCodeTest | |
{ | |
long id = 1; | |
int age = 30; | |
String name = "noname"; | |
LocalDate birthday = LocalDate.of(1980, 1, 1); | |
int[] rates = {1,2,3,4}; | |
@Override public | |
int hashCode() | |
{ | |
return totalHash(hash(id), hash(age), hash(name), hash(birthday), hash(rates)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment