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.utils;
014
015import static org.apache.juneau.collections.JsonMap.*;
016
017import java.util.*;
018
019/**
020 * Represents a list of objects used to compare objects for equality.
021 *
022 * <h5 class='section'>See Also:</h5><ul>
023 * </ul>
024 */
025public class HashKey {
026
027   //-----------------------------------------------------------------------------------------------------------------
028   // Static
029   //-----------------------------------------------------------------------------------------------------------------
030
031   /**
032    * Static creator.
033    *
034    * @param array The contents of the key.
035    * @return A new bean.
036    */
037   public static HashKey of(Object...array) {
038      return new HashKey(array);
039   }
040
041   //-----------------------------------------------------------------------------------------------------------------
042   // Instance
043   //-----------------------------------------------------------------------------------------------------------------
044
045   private final int hashCode;
046   private final Object[] array;
047
048   HashKey(Object[] array) {
049      this.array = array;
050      this.hashCode = Arrays.hashCode(array);
051   }
052
053   @Override
054   public int hashCode() {
055      return hashCode;
056   }
057
058   @Override
059   public boolean equals(Object o) {
060      HashKey x = (HashKey)o;
061      if (array.length != x.array.length)
062         return false;
063      for (int i = 0; i < array.length; i++)
064         if (! Objects.equals(array[i], x.array[i]))
065            return false;
066      return true;
067   }
068
069   @Override /* Object */
070   public String toString() {
071      return filteredMap()
072         .append("hashCode", hashCode())
073         .append("array", array)
074         .asString();
075   }
076}