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 org.netbeans.modules.cnd.repository.testbench.sfs;
043:
044: import java.io.*;
045: import org.netbeans.modules.cnd.repository.spi.Key;
046: import org.netbeans.modules.cnd.repository.spi.Persistent;
047: import org.netbeans.modules.cnd.repository.spi.PersistentFactory;
048:
049: /**
050: * Test object to store in a SingleFileStorage
051: * @author Vladimir Kvashin
052: */
053: public class TestObject implements Persistent {
054:
055: public Key key;
056: public String[] sData;
057: public int iData;
058: public long lData;
059:
060: public TestObject(DataInput in) throws IOException {
061: read(in);
062: }
063:
064: public TestObject(String key, String... data) {
065: this .key = doKey(key);
066: this .sData = data;
067: }
068:
069: Key getKey() {
070: return key;
071: }
072:
073: private Key doKey(final String key) {
074: return new TestKey(key);
075: }
076:
077: public void write(DataOutput out) throws IOException {
078: out.writeUTF(key.getAt(0).toString());
079: if (sData == null) {
080: out.writeInt(-1);
081: } else {
082: out.writeInt(sData.length);
083: for (int i = 0; i < sData.length; i++) {
084: out.writeUTF(sData[i]);
085: }
086: }
087: out.writeInt(iData);
088: out.writeLong(lData);
089: }
090:
091: private Persistent read(DataInput in) throws IOException {
092: key = doKey(in.readUTF());
093: int cnt = in.readInt();
094: if (cnt == -1) {
095: sData = null;
096: } else {
097: sData = new String[cnt];
098: for (int i = 0; i < sData.length; i++) {
099: sData[i] = in.readUTF();
100: }
101: }
102: iData = in.readInt();
103: lData = in.readLong();
104: return this ;
105: }
106:
107: public String toString() {
108: StringBuilder sb = new StringBuilder("TestOBject @"); // NOI18N
109: sb.append(hashCode());
110: sb.append(" key="); // NOI18N
111: sb.append(key);
112: sb.append(" sData="); // NOI18N
113: if (sData == null) {
114: sb.append("null"); // NOI18N
115: } else {
116: for (int i = 0; i < sData.length; i++) {
117: if (i == 0) {
118: sb.append('[');
119: } else {
120: sb.append(","); // NOI18N
121: }
122: sb.append(sData[i]);
123: }
124: }
125: sb.append("] iData="); // NOI18N
126: sb.append(iData);
127: sb.append(" lData="); // NOI18N
128: sb.append(lData);
129: return sb.toString();
130: }
131:
132: public int hashCode() {
133: int hash = iData + (int) lData + key.hashCode();
134: if (sData != null) {
135: for (int i = 0; i < sData.length; i++) {
136: hash += sData.hashCode();
137: }
138: }
139: return hash;
140: }
141:
142: public boolean equals(Object obj) {
143: if (obj == null) {
144: return false;
145: }
146: if (!obj.getClass().equals(TestObject.class)) {
147: return false;
148: }
149: TestObject other = (TestObject) obj;
150: return equals(this .key.getAt(0), other.key.getAt(0))
151: && equals(this .sData, other.sData)
152: && this .lData == other.lData
153: && this .iData == other.iData;
154: }
155:
156: private boolean equals(CharSequence s1, CharSequence s2) {
157: if (s1 == null) {
158: return s2 == null;
159: } else {
160: return s1.equals(s2);
161: }
162: }
163:
164: private boolean equals(CharSequence[] s1, CharSequence[] s2) {
165: if (s1 == null) {
166: return s2 == null;
167: } else if (s2 == null) {
168: return false;
169: } else {
170: if (s1.length != s2.length) {
171: return false;
172: } else {
173: for (int i = 0; i < s1.length; i++) {
174: if (!equals(s1[i], s2[i])) {
175: return false;
176: }
177: }
178: }
179: return true;
180: }
181: }
182:
183: public boolean canWrite(Persistent obj) {
184: return true;
185: }
186: }
|