001: /*
002: * <copyright>
003: *
004: * Copyright 2003-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.aggagent.query;
028:
029: import java.io.Serializable;
030: import java.util.Iterator;
031: import java.util.List;
032: import java.util.Map;
033:
034: public class CompoundKey implements Serializable {
035: private String[] keys = null;
036: private String stringVal = null;
037:
038: public CompoundKey(String[] k) {
039: keys = new String[k.length];
040: System.arraycopy(k, 0, keys, 0, k.length);
041: makeStringVal();
042: }
043:
044: public CompoundKey(List l, Map m) {
045: keys = new String[l.size()];
046: Iterator i = l.iterator();
047: for (int j = 0; i.hasNext(); j++)
048: keys[j] = m.get(i.next()).toString();
049:
050: makeStringVal();
051: }
052:
053: private void makeStringVal() {
054: StringBuffer buf = new StringBuffer();
055: for (int i = 0; i < keys.length; i++) {
056: buf.append("#");
057: buf.append(keys[i]);
058: }
059: stringVal = buf.toString();
060: }
061:
062: public String toString() {
063: return stringVal;
064: }
065:
066: public int hashCode() {
067: return stringVal.hashCode();
068: }
069:
070: public boolean equals(Object obj) {
071: CompoundKey other = null;
072: if (obj instanceof CompoundKey
073: && (other = (CompoundKey) obj).keys.length == keys.length) {
074: for (int i = 0; i < keys.length; i++)
075: if (!keys[i].equals(other.keys[i]))
076: return false;
077:
078: return true;
079: }
080: return false;
081: }
082:
083: public Iterator getKeys() {
084: return new KeyIterator();
085: }
086:
087: private class KeyIterator implements Iterator {
088: private int index = 0;
089:
090: public boolean hasNext() {
091: return index < keys.length;
092: }
093:
094: public Object next() {
095: return keys[index++];
096: }
097:
098: public void remove() {
099: }
100: }
101: }
|