2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
10 import org.opendaylight.yangtools.concepts.Builder;
13 * Utility class for incrementally building object hashCode by hashing together
14 * component objects, one by one.
16 * @param <T> Component objec type
18 public final class HashCodeBuilder<T> implements Builder<Integer> {
19 private int currentHash;
22 * Create a new instance, with internal hash initialized to 1,
23 * equivalent of {@link #HashCodeBuilder(1)}.
25 public HashCodeBuilder() {
30 * Create a new instance, with internal hash set to specified seed.
32 * @param seedHash Seed hash value
34 public HashCodeBuilder(final int seedHash) {
35 this.currentHash = seedHash;
39 * Determine the next hash code combining a base hash code and the
40 * hash code of an object.
42 * @param hashCode base hash code
43 * @param obj Object to be added
44 * @return Combined hash code
46 public static int nextHashCode(final int hashCode, final Object obj) {
47 return 31 * hashCode + obj.hashCode();
51 * Update the internal hash code with the hash code of a component
54 * @param obj Component object
56 public void addArgument(final T obj) {
57 currentHash = nextHashCode(currentHash, obj);
61 public Integer toInstance() {