001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test.reflect.self;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.query.*;
027: import com.db4o.reflect.self.*;
028: import com.db4o.test.*;
029:
030: public class Dog extends Animal {
031:
032: private transient static Vector dogs;
033:
034: // must be public for the time being due to setAccessible() check in Platform4
035: public int _age;
036:
037: public Dog[] _parents;
038:
039: public int[] _prices;
040:
041: public Dog() {
042: // require public no-args constructor
043: this (null, 0);
044: }
045:
046: public Dog(String name, int age) {
047: this (name, age, new Dog[0], new int[0]);
048: }
049:
050: public Dog(String name, int age, Dog[] parents, int[] prices) {
051: super (name);
052: _age = age;
053: _parents = parents;
054: _prices = prices;
055: }
056:
057: public void configure() {
058: Db4o.configure().reflectWith(
059: new SelfReflector(
060: new RegressionDogSelfReflectionRegistry()));
061: }
062:
063: public void store() {
064: dogs = new Vector();
065: Dog laika = new Dog("Laika", 7);
066: Dog lassie = new Dog("Lassie", 6);
067: dogs.addElement(laika);
068: dogs.addElement(lassie);
069: dogs.addElement(new Dog("Sharik", 100, new Dog[] { laika,
070: lassie }, new int[] { 3, 2, 1 }));
071: for (Enumeration e = dogs.elements(); e.hasMoreElements();) {
072: Test.store(e.nextElement());
073: }
074: }
075:
076: public void test() {
077: Query q = Test.query();
078: q.constrain(Dog.class);
079: ObjectSet res = q.execute();
080: Test.ensure(res.size() == dogs.size());
081: while (res.hasNext()) {
082: Dog dog = (Dog) res.next();
083: System.out.println(">>>" + dog._name);
084: Test.ensure(dogs.contains(dog));
085: }
086:
087: q = Test.query();
088: q.constrain(Dog.class);
089: q.descend("_name").constrain("Laika");
090: res = q.execute();
091: Test.ensure(res.size() == 1);
092: Dog laika = (Dog) res.next();
093: Test.ensure(laika._name.equals("Laika"));
094: }
095:
096: /* GENERATE */
097: public Object self_get(String fieldName) {
098: if (fieldName.equals("_age")) {
099: return new Integer(_age);
100: }
101: if (fieldName.equals("_parents")) {
102: return _parents;
103: }
104: if (fieldName.equals("_prices")) {
105: return _prices;
106: }
107: return super .self_get(fieldName);
108: }
109:
110: /* GENERATE */
111: public void self_set(String fieldName, Object value) {
112: if (fieldName.equals("_age")) {
113: _age = ((Integer) value).intValue();
114: return;
115: }
116: if (fieldName.equals("_parents")) {
117: _parents = (Dog[]) value;
118: return;
119: }
120: if (fieldName.equals("_prices")) {
121: _prices = (int[]) value;
122: return;
123: }
124: super .self_set(fieldName, value);
125: }
126:
127: public boolean equals(Object obj) {
128: if (this == obj) {
129: return true;
130: }
131: if (obj == null || getClass() != obj.getClass()) {
132: return false;
133: }
134: Dog dog = (Dog) obj;
135: boolean sameName = (_name == null ? dog._name == null : _name
136: .equals(dog._name));
137: boolean sameAge = _age == dog._age;
138: boolean sameParents = _parents.length == dog._parents.length;
139: if (sameParents) {
140: for (int i = 0; i < _parents.length; i++) {
141: if (!_parents[i].equals(dog._parents[i])) {
142: sameParents = false;
143: break;
144: }
145: }
146: }
147: boolean samePrices = _prices.length == dog._prices.length;
148: if (samePrices) {
149: for (int i = 0; i < _prices.length; i++) {
150: if (!(_prices[i] == dog._prices[i])) {
151: samePrices = false;
152: break;
153: }
154: }
155: }
156:
157: return sameName && sameAge && sameParents && samePrices;
158: }
159:
160: public int hashCode() {
161: int hash = _age;
162: hash = hash * 29 + (_name == null ? 0 : _name.hashCode());
163: hash = hash * 29 + _parents.length;
164: return hash;
165: }
166: }
|