001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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 */
017package org.apache.juneau.utils;
018
019import static org.apache.juneau.collections.JsonMap.*;
020
021import java.util.*;
022
023/**
024 * Represents a list of objects used to compare objects for equality.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 * </ul>
028 */
029public class HashKey {
030
031   //-----------------------------------------------------------------------------------------------------------------
032   // Static
033   //-----------------------------------------------------------------------------------------------------------------
034
035   /**
036    * Static creator.
037    *
038    * @param array The contents of the key.
039    * @return A new bean.
040    */
041   public static HashKey of(Object...array) {
042      return new HashKey(array);
043   }
044
045   //-----------------------------------------------------------------------------------------------------------------
046   // Instance
047   //-----------------------------------------------------------------------------------------------------------------
048
049   private final int hashCode;
050   private final Object[] array;
051
052   HashKey(Object[] array) {
053      this.array = array;
054      this.hashCode = Arrays.hashCode(array);
055   }
056
057   @Override
058   public int hashCode() {
059      return hashCode;
060   }
061
062   @Override
063   public boolean equals(Object o) {
064      HashKey x = (HashKey)o;
065      if (array.length != x.array.length)
066         return false;
067      for (int i = 0; i < array.length; i++)
068         if (! Objects.equals(array[i], x.array[i]))
069            return false;
070      return true;
071   }
072
073   @Override /* Object */
074   public String toString() {
075      return filteredMap()
076         .append("hashCode", hashCode())
077         .append("array", array)
078         .asString();
079   }
080}