001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.internal; 014 015/** 016 * Utility class for generating integer hash codes. 017 * 018 * <p> 019 * General usage: 020 * <p class='bcode w800'> 021 * int hashCode = new HashCode().add("foobar").add(myobject).add(123).get(); 022 * </p> 023 */ 024public class HashCode { 025 026 private int hashCode = 1; 027 028 /** 029 * Create a new HashCode object. 030 * 031 * @return A new HashCode object. 032 */ 033 public static final HashCode create() { 034 return new HashCode(); 035 } 036 037 /** 038 * Calculates a hash code over the specified objects. 039 * 040 * @param objects The objects to calculate a hashcode over. 041 * @return A numerical hashcode value. 042 */ 043 public static final int of(Object...objects) { 044 HashCode x = create(); 045 for (Object oo : objects) 046 x.add(oo); 047 return x.get(); 048 } 049 050 /** 051 * Hashes the hashcode of the specified object into this object. 052 * 053 * @param o The object whose hashcode will be hashed with this object. 054 * @return This object (for method chaining). 055 */ 056 public HashCode add(Object o) { 057 o = unswap(o); 058 add(o == null ? 0 : o.hashCode()); 059 return this; 060 } 061 062 /** 063 * Hashes the hashcode into this object. 064 * 065 * <p> 066 * The formula is simply <c>hashCode = 31*hashCode + i;</c> 067 * 068 * @param i The hashcode to hash into this object's hashcode. 069 * @return This object (for method chaining). 070 */ 071 public HashCode add(int i) { 072 hashCode = 31*hashCode + i; 073 return this; 074 } 075 076 /** 077 * Return the calculated hashcode value. 078 * 079 * @return The calculated hashcode. 080 */ 081 public int get() { 082 return hashCode; 083 } 084 085 /** 086 * Converts the object to a normalized form before grabbing it's hashcode. 087 * 088 * <p> 089 * Subclasses can override this method to provide specialized handling (e.g. converting numbers to strings so that 090 * <c>123</c> and <js>"123"</js> end up creating the same hashcode.) 091 * 092 * <p> 093 * Default implementation does nothing. 094 * 095 * @param o The object to normalize before getting it's hashcode. 096 * @return The normalized object. 097 */ 098 protected Object unswap(Object o) { 099 return o; 100 } 101}