001: ///////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: MultipleKey.java 1903 2007-10-24 09:05:37Z manuel_gay $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.commons;
025:
026: import java.io.Serializable;
027: import java.util.Dictionary;
028: import java.util.Vector;
029:
030: /**
031: * Holds the values for a group of fields. Every element of a MultipleKey holds an Object. In case of a null value being
032: * passed to one of the constructors, the element is replaced by a placeholder.
033: *
034: * @author Cristian Bogdan
035: * @version $Id: MultipleKey.java 1903 2007-10-24 09:05:37Z manuel_gay $
036: */
037: public class MultipleKey extends Vector implements Serializable {
038:
039: private static final long serialVersionUID = 1L;
040:
041: /**
042: * Dummy object holding the place for a null element
043: */
044: static private final Object theNull = new Object() {
045: public String toString() {
046: return "null key member";
047: }
048: };
049:
050: public MultipleKey(int size) {
051: super (size);
052: }
053:
054: public MultipleKey(Vector v) {
055: this (v, v.size());
056: }
057:
058: public MultipleKey(Vector v, int size) {
059: super (size);
060: for (; elementCount < v.size(); elementCount++)
061: elementData[elementCount] = checkNull(v
062: .elementAt(elementCount));
063: }
064:
065: /**
066: * Extends the Vector v and adds the object
067: * @param v An instance of Vector
068: * @param o An object to be added
069: */
070: public MultipleKey(Vector v, Object o) {
071: this (v, v.size() + 1);
072: setAt(o, v.size());
073: }
074:
075: public MultipleKey(Object o[]) {
076: this (o, o.length);
077: }
078:
079: public MultipleKey(Object o[], int size) {
080: super (size);
081: for (; elementCount < o.length; elementCount++)
082: elementData[elementCount] = checkNull(o[elementCount]);
083: }
084:
085: /**
086: * Keeps together the values associated with the give keys
087: *
088: * @param keys
089: * A vector of keys
090: * @param data
091: * The dictionary containing the values of the keys
092: */
093: public MultipleKey(Vector keys, Dictionary data) {
094: this (keys, data, keys.size());
095: }
096:
097: /**
098: * Keeps together the values associated with the given keys
099: *
100: * @param keys
101: * A vector of keys
102: * @param data
103: * The dictionary containing the values of the keys
104: * @param size
105: * The size of the MultipleKey object to be constructed
106: */
107: public MultipleKey(Vector keys, Dictionary data, int size) {
108: super (size);
109: for (; elementCount < keys.size(); elementCount++)
110: elementData[elementCount] = checkNull(data.get(keys
111: .elementAt(elementCount)));
112: }
113:
114: /**
115: * Keeps together the values associated with the given keys
116: *
117: * @param indexes
118: * A vector of keys
119: * @param data
120: * The object array containing the values of the keys
121: */
122: public MultipleKey(Vector indexes, Object[] data) {
123: this (indexes, data, indexes.size());
124: }
125:
126: /**
127: * Keep together the values associated with the given keys
128: *
129: * @param indexes
130: * A vector of keys
131: * @param data
132: * The object array containing the values of the keys
133: * @param size
134: * The size of the MultipleKey object to be constructed
135: */
136: public MultipleKey(Vector indexes, Object[] data, int size) {
137: super (size);
138: for (; elementCount < indexes.size(); elementCount++)
139: elementData[elementCount] = checkNull(data[((Integer) indexes
140: .elementAt(elementCount)).intValue()]);
141: }
142:
143: public void setAt(Object o, int n) {
144: if (elementCount <= n)
145: elementCount = n + 1;
146: elementData[n] = checkNull(o);
147: }
148:
149: protected Object checkNull(Object o) {
150: return o == null ? theNull : o;
151: }
152:
153: /**
154: * Compares the elements of two MultipleKey objects
155: *
156: * @param o
157: * The object to be compared with
158: */
159: public boolean equals(Object o) {
160: for (int i = 0; i < elementCount; i++)
161: if (!((MultipleKey) o).elementAt(i).equals(elementData[i]))
162: return false;
163: return true;
164: }
165:
166: /**
167: * Computes a hash code of the MultipleKey by adding the individual hashcodes of each element
168: *
169: * @return The hashcode of the MultipleKey
170: */
171: public int hashCode() {
172: int ret = 0;
173: for (int i = 0; i < elementCount; i++)
174: ret += elementData[i].hashCode();
175: return ret;
176: }
177: }
|