HashCodeBuilder.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.5.0).
003  * Copyright (c) 2007-2013 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.util;
021 
022 /**
023  * Interface for building hash codes. The HashCodeBuilder is created via a
024  * factory method in the {@link object} class.
025  <p/>
026  * Example for calculating the hash code for a given class:
027  * [code]
028  * \@Override
029  * public int hashCode() {
030  *     return object.hashCodeOf(getClass())
031  *                  .and(_prop1)
032  *                  .and(_prop2).value();
033  * }
034  * [/code]
035  *
036  @see object#hashCodeOf(Class)
037  *
038  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
039  @since 1.0
040  @version 1.0 &mdash; <em>$Date: 2013-09-01 $</em>
041  */
042 public abstract class HashCodeBuilder {
043 
044     int _hash = 0;
045 
046     HashCodeBuilder(final Class<?> type) {
047         _hash = type.hashCode();
048     }
049 
050     /**
051      * Add hash code for a {@code boolean}.
052      *
053      @param value the value to add to the hash code.
054      @return {@code this}
055      */
056     public abstract HashCodeBuilder and(final boolean value);
057 
058     /**
059      * Add hash code for an {@code boolean} array.
060      *
061      @param values the value to add to the hash code.
062      @return {@code this}
063      */
064     public abstract HashCodeBuilder and(final boolean[] values);
065 
066     /**
067      * Add hash code for a {@code byte}.
068      *
069      @param value the value to add to the hash code.
070      @return {@code this}
071      */
072     public abstract HashCodeBuilder and(final byte value);
073 
074     /**
075      * Add hash code for an {@code byte} arrays.
076      *
077      @param values the value to add to the hash code.
078      @return {@code this}
079      */
080     public abstract HashCodeBuilder and(final byte[] values);
081 
082     /**
083      * Add hash code for a {@code char}.
084      *
085      @param value the value to add to the hash code.
086      @return {@code this}
087      */
088     public abstract HashCodeBuilder and(final char value);
089 
090     /**
091      * Add hash code for an {@code char} array.
092      *
093      @param values the value to add to the hash code.
094      @return {@code this}
095      */
096     public abstract HashCodeBuilder and(final char[] values);
097 
098     /**
099      * Add hash code for a {@code short}.
100      *
101      @param value the value to add to the hash code.
102      @return {@code this}
103      */
104     public abstract HashCodeBuilder and(final short value);
105 
106     /**
107      * Add hash code for an {@code short} array.
108      *
109      @param values the value to add to the hash code.
110      @return {@code this}
111      */
112     public abstract HashCodeBuilder and(final short[] values);
113 
114     /**
115      * Add hash code for an {@code int}.
116      *
117      @param value the value to add to the hash code.
118      @return {@code this}
119      */
120     public abstract HashCodeBuilder and(final int value);
121 
122     /**
123      * Add hash code for an {@code int} array.
124      *
125      @param values the value to add to the hash code.
126      @return {@code this}
127      */
128     public abstract HashCodeBuilder and(final int[] values);
129 
130     /**
131      * Add hash code for a {@code long}.
132      *
133      @param value the value to add to the hash code.
134      @return {@code this}
135      */
136     public abstract HashCodeBuilder and(final long value);
137 
138     /**
139      * Add hash code for an {@code long} array.
140      *
141      @param values the value to add to the hash code.
142      @return {@code this}
143      */
144     public abstract HashCodeBuilder and(final long[] values);
145 
146     /**
147      * Add hash code for a {@code float}.
148      *
149      @param value the value to add to the hash code.
150      @return {@code this}
151      */
152     public abstract HashCodeBuilder and(final float value);
153 
154     /**
155      * Add hash code for an {@code float} array.
156      *
157      @param values the value to add to the hash code.
158      @return {@code this}
159      */
160     public abstract HashCodeBuilder and(final float[] values);
161 
162     /**
163      * Add hash code for a {@code double}.
164      *
165      @param value the value to add to the hash code.
166      @return {@code this}
167      */
168     public abstract HashCodeBuilder and(final double value);
169 
170     /**
171      * Add hash code for an {@code double} array.
172      *
173      @param values the value to add to the hash code.
174      @return {@code this}
175      */
176     public abstract HashCodeBuilder and(final double[] values);
177 
178     /**
179      * Add hash code for a {@code Object}.
180      *
181      @param value the value to add to the hash code.
182      @return {@code this}
183      */
184     public abstract HashCodeBuilder and(final Object value);
185 
186     /**
187      * Add hash code for an {@code Object}.
188      *
189      @param values the value to add to the hash code.
190      @return {@code this}
191      */
192     public abstract HashCodeBuilder and(final Object[] values);
193 
194     /**
195      * Add hash code for a {@code Seq}.
196      *
197      @param values the value to add to the hash code.
198      @return {@code this}
199      */
200     public abstract HashCodeBuilder and(final Seq<?> values);
201 
202     /**
203      * Return the calculated hash value.
204      *
205      @return the calculated hash value.
206      */
207     public int value() {
208         return _hash;
209     }
210 
211 }
212 
213