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.commons.collections4.keyvalue;
018
019import java.io.Serializable;
020import java.util.Arrays;
021
022/**
023 * A <code>MultiKey</code> allows multiple map keys to be merged together.
024 * <p>
025 * The purpose of this class is to avoid the need to write code to handle
026 * maps of maps. An example might be the need to look up a file name by
027 * key and locale. The typical solution might be nested maps. This class
028 * can be used instead by creating an instance passing in the key and locale.
029 * <p>
030 * Example usage:
031 * <pre>
032 * // populate map with data mapping key+locale to localizedText
033 * Map map = new HashMap();
034 * MultiKey multiKey = new MultiKey(key, locale);
035 * map.put(multiKey, localizedText);
036 *
037 * // later retrieve the localized text
038 * MultiKey multiKey = new MultiKey(key, locale);
039 * String localizedText = (String) map.get(multiKey);
040 * </pre>
041 *
042 * @since 3.0
043 */
044public class MultiKey<K> implements Serializable {
045    // This class could implement List, but that would confuse it's purpose
046
047    /** Serialisation version */
048    private static final long serialVersionUID = 4465448607415788805L;
049
050    /** The individual keys */
051    private final K[] keys;
052    /** The cached hashCode */
053    private transient int hashCode;
054
055    /**
056     * Constructor taking two keys.
057     * <p>
058     * The keys should be immutable
059     * If they are not then they must not be changed after adding to the MultiKey.
060     *
061     * @param key1  the first key
062     * @param key2  the second key
063     */
064    @SuppressWarnings("unchecked")
065    public MultiKey(final K key1, final K key2) {
066        this((K[]) new Object[] { key1, key2 }, false);
067    }
068
069    /**
070     * Constructor taking three keys.
071     * <p>
072     * The keys should be immutable
073     * If they are not then they must not be changed after adding to the MultiKey.
074     *
075     * @param key1  the first key
076     * @param key2  the second key
077     * @param key3  the third key
078     */
079    @SuppressWarnings("unchecked")
080    public MultiKey(final K key1, final K key2, final K key3) {
081        this((K[]) new Object[] {key1, key2, key3}, false);
082    }
083
084    /**
085     * Constructor taking four keys.
086     * <p>
087     * The keys should be immutable
088     * If they are not then they must not be changed after adding to the MultiKey.
089     *
090     * @param key1  the first key
091     * @param key2  the second key
092     * @param key3  the third key
093     * @param key4  the fourth key
094     */
095    @SuppressWarnings("unchecked")
096    public MultiKey(final K key1, final K key2, final K key3, final K key4) {
097        this((K[]) new Object[] {key1, key2, key3, key4}, false);
098    }
099
100    /**
101     * Constructor taking five keys.
102     * <p>
103     * The keys should be immutable
104     * If they are not then they must not be changed after adding to the MultiKey.
105     *
106     * @param key1  the first key
107     * @param key2  the second key
108     * @param key3  the third key
109     * @param key4  the fourth key
110     * @param key5  the fifth key
111     */
112    @SuppressWarnings("unchecked")
113    public MultiKey(final K key1, final K key2, final K key3, final K key4, final K key5) {
114        this((K[]) new Object[] {key1, key2, key3, key4, key5}, false);
115    }
116
117    /**
118     * Constructor taking an array of keys which is cloned.
119     * <p>
120     * The keys should be immutable
121     * If they are not then they must not be changed after adding to the MultiKey.
122     * <p>
123     * This is equivalent to <code>new MultiKey(keys, true)</code>.
124     *
125     * @param keys  the array of keys, not null
126     * @throws IllegalArgumentException if the key array is null
127     */
128    public MultiKey(final K[] keys) {
129        this(keys, true);
130    }
131
132    /**
133     * Constructor taking an array of keys, optionally choosing whether to clone.
134     * <p>
135     * <b>If the array is not cloned, then it must not be modified.</b>
136     * <p>
137     * This method is public for performance reasons only, to avoid a clone.
138     * The hashcode is calculated once here in this method.
139     * Therefore, changing the array passed in would not change the hashcode but
140     * would change the equals method, which is a bug.
141     * <p>
142     * This is the only fully safe usage of this constructor, as the object array
143     * is never made available in a variable:
144     * <pre>
145     * new MultiKey(new Object[] {...}, false);
146     * </pre>
147     * <p>
148     * The keys should be immutable
149     * If they are not then they must not be changed after adding to the MultiKey.
150     *
151     * @param keys  the array of keys, not null
152     * @param makeClone  true to clone the array, false to assign it
153     * @throws IllegalArgumentException if the key array is null
154     * @since 3.1
155     */
156    public MultiKey(final K[] keys, final boolean makeClone) {
157        super();
158        if (keys == null) {
159            throw new IllegalArgumentException("The array of keys must not be null");
160        }
161        if (makeClone) {
162            this.keys = keys.clone();
163        } else {
164            this.keys = keys;
165        }
166
167        calculateHashCode(keys);
168    }
169
170    //-----------------------------------------------------------------------
171    /**
172     * Gets a clone of the array of keys.
173     * <p>
174     * The keys should be immutable
175     * If they are not then they must not be changed.
176     *
177     * @return the individual keys
178     */
179    public K[] getKeys() {
180        return keys.clone();
181    }
182
183    /**
184     * Gets the key at the specified index.
185     * <p>
186     * The key should be immutable.
187     * If it is not then it must not be changed.
188     *
189     * @param index  the index to retrieve
190     * @return the key at the index
191     * @throws IndexOutOfBoundsException if the index is invalid
192     * @since 3.1
193     */
194    public K getKey(final int index) {
195        return keys[index];
196    }
197
198    /**
199     * Gets the size of the list of keys.
200     *
201     * @return the size of the list of keys
202     * @since 3.1
203     */
204    public int size() {
205        return keys.length;
206    }
207
208    //-----------------------------------------------------------------------
209    /**
210     * Compares this object to another.
211     * <p>
212     * To be equal, the other object must be a <code>MultiKey</code> with the
213     * same number of keys which are also equal.
214     *
215     * @param other  the other object to compare to
216     * @return true if equal
217     */
218    @Override
219    public boolean equals(final Object other) {
220        if (other == this) {
221            return true;
222        }
223        if (other instanceof MultiKey) {
224            final MultiKey<?> otherMulti = (MultiKey<?>) other;
225            return Arrays.equals(keys, otherMulti.keys);
226        }
227        return false;
228    }
229
230    /**
231     * Gets the combined hash code that is computed from all the keys.
232     * <p>
233     * This value is computed once and then cached, so elements should not
234     * change their hash codes once created (note that this is the same
235     * constraint that would be used if the individual keys elements were
236     * themselves {@link java.util.Map Map} keys.
237     *
238     * @return the hash code
239     */
240    @Override
241    public int hashCode() {
242        return hashCode;
243    }
244
245    /**
246     * Gets a debugging string version of the key.
247     *
248     * @return a debugging string
249     */
250    @Override
251    public String toString() {
252        return "MultiKey" + Arrays.toString(keys);
253    }
254
255    /**
256     * Calculate the hash code of the instance using the provided keys.
257     * @param keys the keys to calculate the hash code for
258     */
259    private void calculateHashCode(final Object[] keys)
260    {
261        int total = 0;
262        for (final Object key : keys) {
263            if (key != null) {
264                total ^= key.hashCode();
265            }
266        }
267        hashCode = total;
268    }
269
270    /**
271     * Recalculate the hash code after deserialization. The hash code of some
272     * keys might have change (hash codes based on the system hash code are
273     * only stable for the same process).
274     * @return the instance with recalculated hash code
275     */
276    protected Object readResolve() {
277        calculateHashCode(keys);
278        return this;
279    }
280}