DefaultHashCodeBuilder.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.5.0).
003  * Copyright (c) 2007-2016 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019  */
020 package org.jenetics.internal.util;
021 
022 import java.util.Arrays;
023 
024 import org.jenetics.util.Seq;
025 
026 /**
027  * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
028  * @since 1.0
029  * @version 3.0
030  */
031 final class DefaultHashCodeBuilder implements Hash {
032     private static final int P1 = 47;
033     private static final int P2 = 103;
034     private static final int P3 = 1231;
035     private static final int P4 = 1237;
036 
037 
038     protected int _hash = 0;
039 
040     DefaultHashCodeBuilder(final Class<?> type) {
041         _hash = type.hashCode();
042     }
043 
044     @Override
045     public DefaultHashCodeBuilder and(final boolean value) {
046         _hash += value ? P3 : P4; return this;
047     }
048 
049     @Override
050     public DefaultHashCodeBuilder and(final boolean[] values) {
051         _hash += Arrays.hashCode(values); return this;
052     }
053 
054     @Override
055     public DefaultHashCodeBuilder and(final byte value) {
056         _hash += P1*value + P2; return this;
057     }
058 
059     @Override
060     public DefaultHashCodeBuilder and(final byte[] values) {
061         _hash += Arrays.hashCode(values); return this;
062     }
063 
064     @Override
065     public DefaultHashCodeBuilder and(final char value) {
066         _hash += P1*value + P2; return this;
067     }
068 
069     @Override
070     public DefaultHashCodeBuilder and(final char[] values) {
071         _hash += Arrays.hashCode(values); return this;
072     }
073 
074     @Override
075     public DefaultHashCodeBuilder and(final short value) {
076         _hash += P1*value + P2; return this;
077     }
078 
079     @Override
080     public DefaultHashCodeBuilder and(final short[] values) {
081         _hash += Arrays.hashCode(values); return this;
082     }
083 
084     @Override
085     public DefaultHashCodeBuilder and(final int value) {
086         _hash += P1*value + P2; return this;
087     }
088 
089     @Override
090     public DefaultHashCodeBuilder and(final int[] values) {
091         _hash += Arrays.hashCode(values); return this;
092     }
093 
094     @Override
095     public DefaultHashCodeBuilder and(final long value) {
096         _hash += P1*Long.hashCode(value); return this;
097     }
098 
099     @Override
100     public DefaultHashCodeBuilder and(final long[] values) {
101         _hash += Arrays.hashCode(values); return this;
102     }
103 
104     @Override
105     public DefaultHashCodeBuilder and(final float value) {
106         _hash += P1*Float.hashCode(value); return this;
107     }
108 
109     @Override
110     public DefaultHashCodeBuilder and(final float[] values) {
111         _hash += Arrays.hashCode(values); return this;
112     }
113 
114     @Override
115     public DefaultHashCodeBuilder and(final double value) {
116         _hash += P1*Double.hashCode(value); return this;
117     }
118 
119     @Override
120     public DefaultHashCodeBuilder and(final double[] values) {
121         _hash += Arrays.hashCode(values); return this;
122     }
123 
124     @Override
125     public DefaultHashCodeBuilder and(final Object value) {
126         _hash += P1*(value == null ? 0 : value.hashCode()) + P2; return this;
127     }
128 
129     @Override
130     public DefaultHashCodeBuilder and(final Object[] values) {
131         _hash += Arrays.hashCode(values); return this;
132     }
133 
134     @Override
135     public DefaultHashCodeBuilder and(final Seq<?> values) {
136         _hash += Seq.hashCode(values); return this;
137     }
138 
139     @Override
140     public int value() {
141         return _hash;
142     }
143 }