KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > collection > CompoundKey


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.collection;
23
24 import java.io.Serializable JavaDoc;
25
26 import org.jboss.util.NullArgumentException;
27 import org.jboss.util.Objects;
28 import org.jboss.util.HashCode;
29 import org.jboss.util.Strings;
30
31 /**
32  * An immutable compound key class.
33  *
34  * @version <tt>$Revision: 1958 $</tt>
35  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
36  */

37 public class CompoundKey
38    implements Serializable JavaDoc, Cloneable JavaDoc
39 {
40    /** The elements of the key */
41    private final Object JavaDoc elements[];
42
43    /**
44     * Construct a CompoundKey.
45     *
46     * @param elements Elements of the key.
47     */

48    public CompoundKey(final Object JavaDoc elements[]) {
49       if (elements == null)
50          throw new NullArgumentException("elements");
51
52       this.elements = elements;
53    }
54
55    /**
56     * Construct a CompoundKey.
57     *
58     * @param a Element.
59     * @param b Element.
60     */

61    public CompoundKey(final Object JavaDoc a, final Object JavaDoc b) {
62       this(new Object JavaDoc[] { a, b });
63    }
64
65    /**
66     * Construct a CompoundKey.
67     *
68     * @param a Element.
69     * @param b Element.
70     * @param c Element.
71     */

72    public CompoundKey(final Object JavaDoc a, final Object JavaDoc b, final Object JavaDoc c) {
73       this(new Object JavaDoc[] { a, b, c });
74    }
75
76    /**
77     * Test the equality of an object with this.
78     *
79     * @param obj Object to test equality with.
80     * @return True if object is equal.
81     */

82    public boolean equals(final Object JavaDoc obj) {
83       if (obj == this) return true;
84
85       if (obj != null && obj.getClass() == getClass()) {
86          CompoundKey key = (CompoundKey)obj;
87
88          return Objects.equals(key.elements, elements);
89       }
90
91       return false;
92    }
93
94    /**
95     * Get the hash code of this object.
96     *
97     * @return Hash code.
98     */

99    public int hashCode() {
100       return HashCode.generate(elements);
101    }
102
103    /**
104     * Return a string representation of this object.
105     *
106     * @return A string representation of this object.
107     */

108    public String JavaDoc toString() {
109       return super.toString() + Strings.join(elements, "[", ",", "]");
110    }
111
112    /**
113     * Return a shallow cloned copy of this object.
114     *
115     * @return Shallow cloned copy of this object.
116     */

117    public Object JavaDoc clone() {
118       try {
119          return super.clone();
120       }
121       catch (CloneNotSupportedException JavaDoc e) {
122          throw new InternalError JavaDoc();
123       }
124    }
125 }
126
Popular Tags