001: /**
002: * Copyright (C) 2006 NetMind Consulting Bt.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 3 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package hu.netmind.persistence;
018:
019: import java.util.Date;
020: import java.util.Random;
021: import java.util.Arrays;
022: import org.apache.log4j.Logger;
023:
024: /**
025: * A simple object with all supported primitive types.
026: * @author Brautigam Robert
027: * @version Revision: $Revision$
028: */
029: public class Primitives {
030: private static Logger logger = Logger.getLogger(Primitives.class);
031:
032: private int integerAttr;
033: private short shortAttr;
034: private long longAttr;
035: private char charAttr;
036: private byte byteAttr;
037: private boolean booleanAttr;
038: private double doubleAttr;
039: private float floatAttr;
040:
041: private byte[] byteArray;
042:
043: private Integer integerObjAttr;
044: private Short shortObjAttr;
045: private Long longObjAttr;
046: private Character charObjAttr;
047: private Byte byteObjAttr;
048: private Boolean booleanObjAttr;
049: private Double doubleObjAttr;
050: private Float floatObjAttr;
051:
052: private String stringAttr;
053: private Date dateAttr;
054:
055: public Primitives() {
056: }
057:
058: public void maximize() {
059: integerAttr = Integer.MAX_VALUE;
060: shortAttr = Short.MAX_VALUE;
061: longAttr = Long.MAX_VALUE;
062: byteAttr = Byte.MAX_VALUE;
063: // floating point is unreliable floatAttr = Float.MAX_VALUE;
064: // floating point is unreliable doubleAttr = Double.MAX_VALUE;
065:
066: byteArray = new byte[256];
067: for (int i = 0; i < 256; i++)
068: byteArray[i] = Byte.MAX_VALUE;
069:
070: integerObjAttr = new Integer(Integer.MAX_VALUE);
071: shortObjAttr = new Short(Short.MAX_VALUE);
072: longObjAttr = new Long(Long.MAX_VALUE);
073: byteObjAttr = new Byte(Byte.MAX_VALUE);
074: // floating point is unreliable floatObjAttr = new Float(Float.MAX_VALUE);
075: // floating point is unreliable doubleObjAttr = new Double(Double.MAX_VALUE);
076:
077: charAttr = 'A';
078: }
079:
080: public void nullize() {
081: integerObjAttr = null;
082: shortObjAttr = null;
083: longObjAttr = null;
084: charObjAttr = null;
085: byteObjAttr = null;
086: booleanObjAttr = null;
087: floatObjAttr = null;
088: doubleObjAttr = null;
089: stringAttr = null;
090: dateAttr = null;
091:
092: byteArray = null;
093:
094: charAttr = 'A';
095: }
096:
097: public void minimize() {
098: integerAttr = Integer.MIN_VALUE;
099: shortAttr = Short.MIN_VALUE;
100: longAttr = Long.MIN_VALUE;
101: byteAttr = Byte.MIN_VALUE;
102: // floating point is unreliable floatAttr = Float.MIN_VALUE;
103: // floating point is unreliable doubleAttr = Double.MIN_VALUE;
104:
105: byteArray = new byte[256];
106: for (int i = 0; i < 256; i++)
107: byteArray[i] = Byte.MIN_VALUE;
108:
109: integerObjAttr = new Integer(Integer.MIN_VALUE);
110: shortObjAttr = new Short(Short.MIN_VALUE);
111: longObjAttr = new Long(Long.MIN_VALUE);
112: byteObjAttr = new Byte(Byte.MIN_VALUE);
113: // floating point is unreliable floatObjAttr = new Float(Float.MIN_VALUE);
114: // floating point is unreliable doubleObjAttr = new Double(Double.MIN_VALUE);
115:
116: charAttr = 'A';
117: }
118:
119: public void randomize() {
120: Random rnd = new Random();
121:
122: integerAttr = rnd.nextInt();
123: shortAttr = (short) rnd.nextInt();
124: longAttr = rnd.nextLong();
125: charAttr = 'A';
126: byteAttr = (byte) rnd.nextInt();
127: booleanAttr = rnd.nextBoolean();
128: floatAttr = rnd.nextFloat();
129: doubleAttr = rnd.nextDouble();
130:
131: byteArray = new byte[rnd.nextInt(2048)];
132: for (int i = 0; i < byteArray.length; i++)
133: byteArray[i] = (byte) rnd.nextInt();
134:
135: integerObjAttr = new Integer(rnd.nextInt());
136: shortObjAttr = new Short((short) rnd.nextInt());
137: longObjAttr = new Long(rnd.nextLong());
138: charObjAttr = new Character('B');
139: byteObjAttr = new Byte((byte) rnd.nextInt());
140: booleanObjAttr = new Boolean(rnd.nextBoolean());
141: floatObjAttr = new Float(rnd.nextFloat());
142: doubleObjAttr = new Double(rnd.nextDouble());
143:
144: dateAttr = new Date();
145: stringAttr = "Test String";
146: }
147:
148: public boolean equals(Object obj) {
149: if (!(obj instanceof Primitives))
150: return false;
151: Primitives p = (Primitives) obj;
152: return ((integerAttr == p.integerAttr) || (notEqual(
153: "integerAttr", new Integer(integerAttr), new Integer(
154: p.integerAttr))))
155: && ((shortAttr == p.shortAttr) || (notEqual(
156: "shortAttr", new Short(shortAttr), new Short(
157: p.shortAttr))))
158: && ((longAttr == p.longAttr) || (notEqual("longAttr",
159: new Long(longAttr), new Long(p.longAttr))))
160: && ((charAttr == p.charAttr) || (notEqual("charAttr",
161: new Character(charAttr), new Character(
162: p.charAttr))))
163: && ((byteAttr == p.byteAttr) || (notEqual("byteAttr",
164: new Byte(byteAttr), new Byte(p.byteAttr))))
165: && ((booleanAttr == p.booleanAttr) || (notEqual(
166: "booleanAttr", new Boolean(booleanAttr),
167: new Boolean(p.booleanAttr))))
168: &&
169: // floating point is unstable ((floatAttr == p.floatAttr) || (notEqual("floatAttr",new Float(floatAttr),new Float(p.floatAttr)))) &&
170: // floating point is unstable ((doubleAttr == p.doubleAttr) || (notEqual("doubleAttr",new Double(doubleAttr),new Double(p.doubleAttr)))) &&
171:
172: ((((byteArray == null) && (p.byteArray == null)) || ((byteArray != null) && (Arrays
173: .equals(byteArray, p.byteArray)))) || (notEqual(
174: "byteArray", byteArray, p.byteArray)))
175: &&
176:
177: ((((integerObjAttr == null) && (p.integerObjAttr == null)) || ((integerObjAttr != null) && (integerObjAttr
178: .equals(p.integerObjAttr)))) || (notEqual(
179: "integerObjAttr", integerObjAttr,
180: p.integerObjAttr)))
181: && ((((shortObjAttr == null) && (p.shortObjAttr == null)) || ((shortObjAttr != null) && (shortObjAttr
182: .equals(p.shortObjAttr)))) || (notEqual(
183: "shortObjAttr", shortObjAttr, p.shortObjAttr)))
184: && ((((longObjAttr == null) && (p.longObjAttr == null)) || ((longObjAttr != null) && (longObjAttr
185: .equals(p.longObjAttr)))) || (notEqual(
186: "longObjAttr", longObjAttr, p.longObjAttr)))
187: && ((((charObjAttr == null) && (p.charObjAttr == null)) || ((charObjAttr != null) && (charObjAttr
188: .equals(p.charObjAttr)))) || (notEqual(
189: "charObjAttr", charObjAttr, p.charObjAttr)))
190: && ((((byteObjAttr == null) && (p.byteObjAttr == null)) || ((byteObjAttr != null) && (byteObjAttr
191: .equals(p.byteObjAttr)))) || (notEqual(
192: "byteObjAttr", byteObjAttr, p.byteObjAttr)))
193: && ((((booleanObjAttr == null) && (p.booleanObjAttr == null)) || ((booleanObjAttr != null) && (booleanObjAttr
194: .equals(p.booleanObjAttr)))) || (notEqual(
195: "booleanObjAttr", booleanObjAttr,
196: p.booleanObjAttr)))
197: &&
198: // floating point is unstable (( ((floatObjAttr==null) && (p.floatObjAttr==null)) || ((floatObjAttr!=null) && (floatObjAttr.equals(p.floatObjAttr))) ) || (notEqual("floatObjAttr",floatObjAttr,p.floatObjAttr))) &&
199: // floating point is unstable (( ((doubleObjAttr==null) && (p.doubleObjAttr==null)) || ((doubleObjAttr!=null) && (doubleObjAttr.equals(p.doubleObjAttr))) ) || (notEqual("doubleObjAttr",doubleObjAttr,p.doubleObjAttr))) &&
200:
201: ((((dateAttr == null) && (p.dateAttr == null)) || ((dateAttr != null) && (isSameDate(
202: dateAttr, p.dateAttr)))) || (notEqual(
203: "dateAttr", dateAttr, p.dateAttr)))
204: && ((((stringAttr == null) && (p.stringAttr == null)) || ((stringAttr != null) && (stringAttr
205: .equals(p.stringAttr)))) || (notEqual(
206: "stringAttr", stringAttr, p.stringAttr)));
207: }
208:
209: /**
210: * Two dates are considered the same, if they match to the second. Milliseconds
211: * are not required.
212: */
213: private boolean isSameDate(Date lhs, Date rhs) {
214: return Math.abs(lhs.getDate() - rhs.getDate()) < 1000;
215: }
216:
217: private boolean notEqual(String attribute, Object v1, Object v2) {
218: if (byte[].class.equals(v1.getClass())) {
219: logger
220: .debug("equality check failed, the two array lengths are: "
221: + ((byte[]) v1).length
222: + " vs. "
223: + ((byte[]) v2).length);
224: if (((byte[]) v1).length == ((byte[]) v2).length) {
225: StringBuffer buf = new StringBuffer("contents are:");
226: for (int i = 0; i < ((byte[]) v1).length; i++)
227: buf.append(" " + ((byte[]) v1)[i] + "|"
228: + ((byte[]) v2)[i]);
229: logger.debug(buf.toString());
230: }
231: }
232: throw new RuntimeException("attribute '" + attribute
233: + "' does not equals in primitives class (" + v1
234: + " expected, but was: " + v2 + ")");
235: }
236: }
|