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>IndexRowKey uses an int index as the identifier for a data row in a
048: * {@link com.sun.data.provider.TableDataProvider}.</p>
049: *
050: * @author Joe Nuxoll
051: * Winston Prakash (Buf Fixes and clean up)
052: */
053: public class IndexRowKey extends RowKey {
054:
055: /**
056: * Constructs a new IndexRowKey from the passed rowId String
057: *
058: * @param rowId The canonical row ID string to parse into an int
059: * @return An IndexRowKey representing the passed rowId
060: * @throws java.lang.NumberFormatException If the passed String is not
061: * parsable into an int
062: */
063: public static IndexRowKey create(String rowId)
064: throws NumberFormatException {
065: return new IndexRowKey(Integer.parseInt(rowId));
066: }
067:
068: /**
069: * Constructs an IndexRowKey using the specified index
070: *
071: * @param index The desired index
072: */
073: public IndexRowKey(int index) {
074: super (String.valueOf(index));
075: this .index = index;
076: }
077:
078: /**
079: * Returns the index of this IndexRowKey
080: *
081: * @return This IndexRowKey's index value
082: */
083: public int getIndex() {
084: return index;
085: }
086:
087: /**
088: * <p>Compare this instance to another {@link IndexRowKey} instance.</p>
089: *
090: * {@inheritDoc}
091: */
092: public int compareTo(Object o) {
093: IndexRowKey instance = (IndexRowKey) o;
094: int oindex = instance.getIndex();
095: if (this .index < oindex) {
096: return -1;
097: } else if (this .index > oindex) {
098: return 1;
099: } else {
100: return 0;
101: }
102: }
103:
104: /**
105: * Standard equals implementation. This method compares the IndexRowKey
106: * index values for == equality. If the passed Object is not an
107: * IndexRowKey instance, the superclass (RowKey) gets a chance to evaluate
108: * the Object for equality.
109: *
110: * {@inheritDoc}
111: */
112: public boolean equals(Object o) {
113: if (o instanceof IndexRowKey) {
114: return ((IndexRowKey) o).getIndex() == this .index;
115: }
116: return super .equals(o);
117: }
118:
119: private int index;
120:
121: /**
122: * <p>Return a printable version of this instance.</p>
123: *
124: * {@inheritDoc}
125: */
126: public String toString() {
127: return "RowKey[" + index + "]"; //NOI18N
128: }
129: }
|