| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.util; |
|---|
| 34 | |
|---|
| 35 | import java.io.Serializable; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * The class can be used to easily implement {@link Object#hashCode()}. |
|---|
| 39 | * |
|---|
| 40 | * @author Michael Griffel |
|---|
| 41 | * |
|---|
| 42 | */ |
|---|
| 43 | public final class HashCode |
|---|
| 44 | implements Serializable |
|---|
| 45 | { |
|---|
| 46 | private static final long serialVersionUID = 1688234599683404302L; |
|---|
| 47 | |
|---|
| 48 | private int mHashCode = HashCodeUtil.SEED; |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Updates the internal hash code with the given Boolean. |
|---|
| 52 | * @param aBoolean the Boolean to add to the hash code. |
|---|
| 53 | */ |
|---|
| 54 | public void hash (boolean aBoolean) |
|---|
| 55 | { |
|---|
| 56 | mHashCode = HashCodeUtil.hash(mHashCode, aBoolean); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Updates the internal hash code with the given character. |
|---|
| 61 | * @param aChar the character to add to the hash code. |
|---|
| 62 | */ |
|---|
| 63 | public void hash (char aChar) |
|---|
| 64 | { |
|---|
| 65 | mHashCode = HashCodeUtil.hash(mHashCode, aChar); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Updates the internal hash code with the given double. |
|---|
| 70 | * @param aDouble the double to add to the hash code. |
|---|
| 71 | */ |
|---|
| 72 | public void hash (double aDouble) |
|---|
| 73 | { |
|---|
| 74 | mHashCode = HashCodeUtil.hash(mHashCode, aDouble); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Updates the internal hash code with the given float. |
|---|
| 79 | * @param aFloat the float to add to the hash code. |
|---|
| 80 | */ |
|---|
| 81 | public void hash (float aFloat) |
|---|
| 82 | { |
|---|
| 83 | mHashCode = HashCodeUtil.hash(mHashCode, aFloat); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | * Updates the internal hash code with the given integer. |
|---|
| 88 | * @param aInt the integer to add to the hash code. |
|---|
| 89 | */ |
|---|
| 90 | public void hash (int aInt) |
|---|
| 91 | { |
|---|
| 92 | mHashCode = HashCodeUtil.hash(mHashCode, aInt); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Updates the internal hash code with the given long. |
|---|
| 97 | * @param aLong the long to add to the hash code. |
|---|
| 98 | */ |
|---|
| 99 | public void hash (long aLong) |
|---|
| 100 | { |
|---|
| 101 | mHashCode = HashCodeUtil.hash(mHashCode, aLong); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * Updates the internal hash code with the given object. |
|---|
| 106 | * <code>aObject</code> is a possibly-null object field, and possibly |
|---|
| 107 | * an array. If <code>aObject</code> is an array, then each element |
|---|
| 108 | * may be a primitive or a possibly-null object. |
|---|
| 109 | * |
|---|
| 110 | * @param aObject the object to add to the hash code. |
|---|
| 111 | */ |
|---|
| 112 | public void hash (Object aObject) |
|---|
| 113 | { |
|---|
| 114 | mHashCode = HashCodeUtil.hash(mHashCode, aObject); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * Returns the current snapshot of the hash code. |
|---|
| 119 | * @return the current snapshot of the hash code. |
|---|
| 120 | */ |
|---|
| 121 | public int toInt () |
|---|
| 122 | { |
|---|
| 123 | return mHashCode; |
|---|
| 124 | } |
|---|
| 125 | } |
|---|