001 /*
002 * Java Genetic Algorithm Library (jenetics-2.0.2).
003 * Copyright (c) 2007-2014 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 import org.jenetics.util.arrays;
026
027 /**
028 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
029 * @since 1.0
030 * @version 2.0 — <em>$Date: 2014-03-10 $</em>
031 */
032 public final class DefaultHashCodeBuilder implements Hash {
033 private static final int P1 = 47;
034 private static final int P2 = 103;
035 private static final int P3 = 1231;
036 private static final int P4 = 1237;
037
038
039 protected int _hash = 0;
040
041 protected DefaultHashCodeBuilder(final Class<?> type) {
042 _hash = type.hashCode();
043 }
044
045 @Override
046 public DefaultHashCodeBuilder and(final boolean value) {
047 _hash += value ? P3 : P4; return this;
048 }
049
050 @Override
051 public DefaultHashCodeBuilder and(final boolean[] values) {
052 _hash += Arrays.hashCode(values); return this;
053 }
054
055 @Override
056 public DefaultHashCodeBuilder and(final byte value) {
057 _hash += P1*value + P2; return this;
058 }
059
060 @Override
061 public DefaultHashCodeBuilder and(final byte[] values) {
062 _hash += Arrays.hashCode(values); return this;
063 }
064
065 @Override
066 public DefaultHashCodeBuilder and(final char value) {
067 _hash += P1*value + P2; return this;
068 }
069
070 @Override
071 public DefaultHashCodeBuilder and(final char[] values) {
072 _hash += Arrays.hashCode(values); return this;
073 }
074
075 @Override
076 public DefaultHashCodeBuilder and(final short value) {
077 _hash += P1*value + P2; return this;
078 }
079
080 @Override
081 public DefaultHashCodeBuilder and(final short[] values) {
082 _hash += Arrays.hashCode(values); return this;
083 }
084
085 @Override
086 public DefaultHashCodeBuilder and(final int value) {
087 _hash += P1*value + P2; return this;
088 }
089
090 @Override
091 public DefaultHashCodeBuilder and(final int[] values) {
092 _hash += Arrays.hashCode(values); return this;
093 }
094
095 @Override
096 public DefaultHashCodeBuilder and(final long value) {
097 _hash += P1*(int)(value^(value >>> 32)); return this;
098 }
099
100 @Override
101 public DefaultHashCodeBuilder and(final long[] values) {
102 _hash += Arrays.hashCode(values); return this;
103 }
104
105 @Override
106 public DefaultHashCodeBuilder and(final float value) {
107 _hash += P1*Float.floatToIntBits(value); return this;
108 }
109
110 @Override
111 public DefaultHashCodeBuilder and(final float[] values) {
112 _hash += Arrays.hashCode(values); return this;
113 }
114
115 @Override
116 public DefaultHashCodeBuilder and(final double value) {
117 long bits = Double.doubleToLongBits(value);
118 _hash += (int)(bits^(bits >>> 32));
119 return this;
120 }
121
122 @Override
123 public DefaultHashCodeBuilder and(final double[] values) {
124 _hash += Arrays.hashCode(values); return this;
125 }
126
127 @Override
128 public DefaultHashCodeBuilder and(final Object value) {
129 _hash += P1*(value == null ? 0 : value.hashCode()) + P2; return this;
130 }
131
132 @Override
133 public DefaultHashCodeBuilder and(final Object[] values) {
134 _hash += Arrays.hashCode(values); return this;
135 }
136
137 @Override
138 public DefaultHashCodeBuilder and(final Seq<?> values) {
139 _hash += arrays.hashCode(values); return this;
140 }
141
142 @Override
143 public int value() {
144 return _hash;
145 }
146 }
|