Shift Builder<P> from toInstance() to build()
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / HashCodeBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util;
9
10 import org.opendaylight.yangtools.concepts.Builder;
11
12 /**
13  * Utility class for incrementally building object hashCode by hashing together
14  * component objects, one by one.
15  *
16  * @param <T> Component objec type
17  */
18 public final class HashCodeBuilder<T> implements Builder<Integer> {
19     private int currentHash;
20
21     /**
22      * Create a new instance, with internal hash initialized to 1,
23      * equivalent of {@link #HashCodeBuilder(1)}.
24      */
25     public HashCodeBuilder() {
26         this(1);
27     }
28
29     /**
30      * Create a new instance, with internal hash set to specified seed.
31      *
32      * @param seedHash Seed hash value
33      */
34     public HashCodeBuilder(final int seedHash) {
35         this.currentHash = seedHash;
36     }
37
38     /**
39      * Determine the next hash code combining a base hash code and the
40      * hash code of an object.
41      *
42      * @param hashCode base hash code
43      * @param obj Object to be added
44      * @return Combined hash code
45      */
46     public static int nextHashCode(final int hashCode, final Object obj) {
47         return 31 * hashCode + obj.hashCode();
48     }
49
50     /**
51      * Update the internal hash code with the hash code of a component
52      * object.
53      *
54      * @param obj Component object
55      */
56     public void addArgument(final T obj) {
57         currentHash = nextHashCode(currentHash, obj);
58     }
59
60     @Override
61     public Integer build() {
62         return currentHash;
63     }
64
65     @Deprecated
66     public Integer toInstance() {
67         return currentHash;
68     }
69 }