studio.raptor.ddal.common.builder.HashCodeBuilder Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package studio.raptor.ddal.common.builder;
/**
* @since 3.0.0
*/
public class HashCodeBuilder implements Builder {
/**
* Constant to use in building the hashCode.
*/
private final int iConstant;
/**
* Running total of the hashCode.
*/
private int iTotal = 0;
/**
*
* Uses two hard coded choices for the constants needed to build a hashCode
.
*
*/
public HashCodeBuilder() {
iConstant = 37;
iTotal = 17;
}
/**
* Append a hashCode
for a boolean
.
This adds
* 1
when true, and 0
when false to the hashCode
.
* This is in contrast to the standard java.lang.Boolean.hashCode
handling, which
* computes a hashCode
value of 1231
for java.lang.Boolean
* instances that represent true
or 1237
for
* java.lang.Boolean
instances that represent false
.
This is in
* accordance with the Effective Java design.
*
* @param value the boolean to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final boolean value) {
iTotal = iTotal * iConstant + (value ? 0 : 1);
return this;
}
/**
*
* Append a hashCode
for a boolean
array.
*
*
* @param array the array to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final boolean[] array) {
if (array == null) {
iTotal = iTotal * iConstant;
} else {
for (final boolean element : array) {
append(element);
}
}
return this;
}
// -------------------------------------------------------------------------
/**
*
* Append a hashCode
for a byte
.
*
*
* @param value the byte to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final byte value) {
iTotal = iTotal * iConstant + value;
return this;
}
// -------------------------------------------------------------------------
/**
*
* Append a hashCode
for a byte
array.
*
*
* @param array the array to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final byte[] array) {
if (array == null) {
iTotal = iTotal * iConstant;
} else {
for (final byte element : array) {
append(element);
}
}
return this;
}
/**
*
* Append a hashCode
for a char
.
*
*
* @param value the char to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final char value) {
iTotal = iTotal * iConstant + value;
return this;
}
/**
*
* Append a hashCode
for a char
array.
*
*
* @param array the array to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final char[] array) {
if (array == null) {
iTotal = iTotal * iConstant;
} else {
for (final char element : array) {
append(element);
}
}
return this;
}
/**
*
* Append a hashCode
for a double
.
*
*
* @param value the double to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final double value) {
return append(Double.doubleToLongBits(value));
}
/**
*
* Append a hashCode
for a double
array.
*
*
* @param array the array to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final double[] array) {
if (array == null) {
iTotal = iTotal * iConstant;
} else {
for (final double element : array) {
append(element);
}
}
return this;
}
/**
*
* Append a hashCode
for a float
.
*
*
* @param value the float to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final float value) {
iTotal = iTotal * iConstant + Float.floatToIntBits(value);
return this;
}
/**
*
* Append a hashCode
for a float
array.
*
*
* @param array the array to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final float[] array) {
if (array == null) {
iTotal = iTotal * iConstant;
} else {
for (final float element : array) {
append(element);
}
}
return this;
}
/**
*
* Append a hashCode
for an int
.
*
*
* @param value the int to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final int value) {
iTotal = iTotal * iConstant + value;
return this;
}
/**
*
* Append a hashCode
for an int
array.
*
*
* @param array the array to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final int[] array) {
if (array == null) {
iTotal = iTotal * iConstant;
} else {
for (final int element : array) {
append(element);
}
}
return this;
}
/**
*
* Append a hashCode
for a long
.
*
*
* @param value the long to add to the hashCode
* @return this
*/
// NOTE: This method uses >> and not >>> as Effective Java and
// Long.hashCode do. Ideally we should switch to >>> at
// some stage. There are backwards compat issues, so
// that will have to wait for the time being. cf LANG-342.
public HashCodeBuilder append(final long value) {
iTotal = iTotal * iConstant + ((int) (value ^ (value >> 32)));
return this;
}
/**
*
* Append a hashCode
for a long
array.
*
*
* @param array the array to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final long[] array) {
if (array == null) {
iTotal = iTotal * iConstant;
} else {
for (final long element : array) {
append(element);
}
}
return this;
}
/**
*
* Append a hashCode
for an Object
.
*
*
* @param object the Object to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final Object object) {
if (object == null) {
iTotal = iTotal * iConstant;
} else {
if (object.getClass().isArray()) {
// factor out array case in order to keep method small enough
// to be inlined
appendArray(object);
} else {
iTotal = iTotal * iConstant + object.hashCode();
}
}
return this;
}
/**
*
* Append a hashCode
for an array.
*
*
* @param object the array to add to the hashCode
*/
private void appendArray(final Object object) {
// 'Switch' on type of array, to dispatch to the correct handler
// This handles multi dimensional arrays
if (object instanceof long[]) {
append((long[]) object);
} else if (object instanceof int[]) {
append((int[]) object);
} else if (object instanceof short[]) {
append((short[]) object);
} else if (object instanceof char[]) {
append((char[]) object);
} else if (object instanceof byte[]) {
append((byte[]) object);
} else if (object instanceof double[]) {
append((double[]) object);
} else if (object instanceof float[]) {
append((float[]) object);
} else if (object instanceof boolean[]) {
append((boolean[]) object);
} else {
// Not an array of primitives
append((Object[]) object);
}
}
/**
*
* Append a hashCode
for an Object
array.
*
*
* @param array the array to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final Object[] array) {
if (array == null) {
iTotal = iTotal * iConstant;
} else {
for (final Object element : array) {
append(element);
}
}
return this;
}
/**
*
* Append a hashCode
for a short
.
*
*
* @param value the short to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final short value) {
iTotal = iTotal * iConstant + value;
return this;
}
/**
*
* Append a hashCode
for a short
array.
*
*
* @param array the array to add to the hashCode
* @return this
*/
public HashCodeBuilder append(final short[] array) {
if (array == null) {
iTotal = iTotal * iConstant;
} else {
for (final short element : array) {
append(element);
}
}
return this;
}
/**
*
* Adds the result of super.hashCode() to this builder.
*
*
* @param superHashCode the result of calling super.hashCode()
* @return this HashCodeBuilder, used to chain calls.
* @since 2.0
*/
public HashCodeBuilder appendSuper(final int superHashCode) {
iTotal = iTotal * iConstant + superHashCode;
return this;
}
/**
*
* Return the computed hashCode
.
*
*
* @return hashCode
based on the fields appended
*/
public int toHashCode() {
return iTotal;
}
/**
* Returns the computed hashCode
.
*
* @return hashCode
based on the fields appended
* @since 3.0
*/
@Override
public Integer build() {
return toHashCode();
}
/**
*
* The computed hashCode
from toHashCode() is returned due to the likelihood
* of bugs in mis-calling toHashCode() and the unlikeliness of it mattering what the hashCode for
* HashCodeBuilder itself is.
*
* @return hashCode
based on the fields appended
* @since 2.5
*/
@Override
public int hashCode() {
return toHashCode();
}
@Override
public boolean equals(Object obj) {
return !(obj instanceof HashCodeBuilder) && hashCode() == obj.hashCode();
}
}