Coverage Report - org.apache.tapestry5.internal.util.MultiKey
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiKey
100%
22/22
100%
10/10
0
 
 1  
 // Copyright 2006 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.internal.util;
 16  
 
 17  
 import java.util.Arrays;
 18  
 
 19  
 /**
 20  
  * Combines multiple values to form a single composite key. MultiKey can often be used as an alternative to nested
 21  
  * maps.
 22  
  */
 23  
 public final class MultiKey
 24  
 {
 25  
     private static final int PRIME = 31;
 26  
 
 27  
     private final Object[] values;
 28  
 
 29  
     private final int hashCode;
 30  
 
 31  
     /**
 32  
      * Creates a new instance from the provided values. It is assumed that the values provided are good map keys
 33  
      * themselves -- immutable, with proper implementations of equals() and hashCode().
 34  
      *
 35  
      * @param values
 36  
      */
 37  
     public MultiKey(Object... values)
 38  10749
     {
 39  10749
         this.values = values;
 40  
 
 41  10749
         hashCode = PRIME * Arrays.hashCode(this.values);
 42  10749
     }
 43  
 
 44  
     @Override
 45  
     public int hashCode()
 46  
     {
 47  13427
         return hashCode;
 48  
     }
 49  
 
 50  
     @Override
 51  
     public boolean equals(Object obj)
 52  
     {
 53  8025
         if (this == obj)
 54  2
             return true;
 55  8023
         if (obj == null)
 56  2
             return false;
 57  8021
         if (getClass() != obj.getClass())
 58  2
             return false;
 59  8019
         final MultiKey other = (MultiKey) obj;
 60  
 
 61  8019
         return Arrays.equals(values, other.values);
 62  
     }
 63  
 
 64  
     @Override
 65  
     public String toString()
 66  
     {
 67  4
         StringBuilder builder = new StringBuilder("MultiKey[");
 68  
 
 69  4
         boolean first = true;
 70  
 
 71  10
         for (Object o : values)
 72  
         {
 73  6
             if (!first)
 74  2
                 builder.append(", ");
 75  
 
 76  6
             builder.append(o);
 77  
 
 78  6
             first = false;
 79  
         }
 80  
 
 81  4
         builder.append("]");
 82  
 
 83  4
         return builder.toString();
 84  
     }
 85  
 
 86  
 }