001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package com.sun.data.provider.impl;
043:
044: import com.sun.data.provider.RowKey;
045:
046: /**
047: * <p>ObjectArrayRowKey uses an object array as the identifier for a data row
048: * in a {@link com.sun.data.provider.TableDataProvider}.</p>
049: *
050: * @author Joe Nuxoll
051: */
052: public class ObjectArrayRowKey extends RowKey {
053:
054: /**
055: * Constructs an ObjectArrayRowKey using the specified array of objects
056: *
057: * @param objects The desired array of objects
058: */
059: public ObjectArrayRowKey(Object[] objects) {
060: super (objects != null ? String.valueOf(objects.hashCode())
061: : String.valueOf(objects));
062: this .objects = objects;
063: }
064:
065: /**
066: * Returns the Object[] of this ObjectArrayRowKey
067: *
068: * @return This ObjectArrayRowKey's object array value
069: */
070: public Object[] getObjects() {
071: return objects;
072: }
073:
074: /**
075: * Returns the pattern:
076: *
077: * object1hash|object2hash|object3hash
078: *
079: * {@inheritDoc}
080: */
081: public String getRowId() {
082: if (objects != null) {
083: StringBuffer buf = new StringBuffer();
084: for (int i = 0; i < objects.length; i++) {
085: buf.append(objects[i] != null ? String
086: .valueOf(objects[i].hashCode()) : String
087: .valueOf(objects[i]));
088: if (i < objects.length - 1) {
089: buf.append("|");
090: }
091: }
092: return buf.toString();
093: }
094: return super .getRowId();
095: }
096:
097: /**
098: * Standard equals implementation. This method compares the ObjectArrayRowKey
099: * object values for == equality. If the passed Object is not an
100: * ObjectArrayRowKey instance, the superclass (RowKey) gets a chance to evaluate
101: * the Object for equality.
102: *
103: * @param o the Object to check equality
104: * @return true if equal, false if not
105: * @see Object#equals(Object)
106: */
107: public boolean equals(Object o) {
108: if (o instanceof ObjectArrayRowKey) {
109: Object[] orkOs = ((ObjectArrayRowKey) o).getObjects();
110: if (orkOs == null || orkOs.length != objects.length) {
111: return false;
112: }
113: for (int i = 0; i < orkOs.length; i++) {
114: Object o1 = orkOs[i];
115: Object o2 = objects[i];
116: if (o1 != o2 && (o1 != null && !o1.equals(o2))) {
117: return false;
118: }
119: }
120: return true;
121: }
122: return super .equals(o);
123: }
124:
125: // /**
126: // * <p>Standard implementation of compareTo(Object). This compares the
127: // * stored object arrays in the ObjectArrayRowKeys. If the contained
128: // * objects do not implement Comparable, the superclass version of
129: // * compareTo(Object) is invoked.</p>
130: // *
131: // * {@inheritDoc}
132: // */
133: // public int compareTo(Object o) {
134:
135: // if (o instanceof ObjectArrayRowKey) {
136: // Object[] orkOs = ((ObjectArrayRowKey)o).getObjects();
137: // if (orkOs == null || orkOs.length != objects.length) {
138: // return false;
139: // }
140: // for (int i = 0; i < orkOs.length; i++) {
141: // Object o1 = orkOs[i];
142: // Object o2 = objects[i];
143: // if (o1 != o2 && (o1 != null && !o1.equals(o2))) {
144: // return false;
145: // }
146: // }
147: // return true;
148: // }
149: // return super.equals(o);
150:
151: // if (object instanceof Comparable && o instanceof ObjectRowKey) {
152: // Object ork = ((ObjectRowKey)o).getObject();
153: // return ((Comparable)object).compareTo(ork);
154: // }
155: // return super.compareTo(o);
156: // }
157:
158: private Object[] objects;
159: }
|