KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > reflect > self > Dog


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.reflect.self;
22
23 import java.util.*;
24
25 import com.db4o.*;
26 import com.db4o.query.*;
27 import com.db4o.reflect.self.*;
28 import com.db4o.test.*;
29
30
31 public class Dog extends Animal {
32
33     private transient static Vector dogs;
34     
35     // must be public for the time being due to setAccessible() check in Platform4
36
public int _age;
37     
38     public Dog[] _parents;
39     
40     public int[] _prices;
41     
42     public Dog() {
43         // require public no-args constructor
44
this(null,0);
45     }
46     
47     public Dog(String JavaDoc name,int age){
48         this(name,age,new Dog[0],new int[0]);
49     }
50
51     public Dog(String JavaDoc name,int age,Dog[] parents,int[] prices){
52         super(name);
53         _age = age;
54         _parents=parents;
55         _prices=prices;
56     }
57     
58     public void configure(){
59         Db4o.configure().reflectWith(new SelfReflector(new RegressionDogSelfReflectionRegistry()));
60     }
61     
62     public void store(){
63         dogs=new Vector();
64         Dog laika = new Dog("Laika",7);
65         Dog lassie = new Dog("Lassie",6);
66         dogs.addElement(laika);
67         dogs.addElement(lassie);
68         dogs.addElement(new Dog("Sharik",100, new Dog[]{laika,lassie},new int[]{3,2,1}));
69         for (Enumeration e = dogs.elements(); e.hasMoreElements();) {
70             Test.store(e.nextElement());
71         }
72     }
73     
74     public void test(){
75         Query q = Test.query();
76         q.constrain(Dog.class);
77         ObjectSet res = q.execute();
78         Test.ensure(res.size() == dogs.size());
79         while(res.hasNext()) {
80             Dog dog=(Dog)res.next();
81             System.out.println(">>>"+dog._name);
82             Test.ensure(dogs.contains(dog));
83         }
84                 
85         q = Test.query();
86         q.constrain(Dog.class);
87         q.descend("_name").constrain("Laika");
88         res = q.execute();
89         Test.ensure(res.size() == 1);
90         Dog laika = (Dog) res.next();
91         Test.ensure(laika._name.equals("Laika"));
92     }
93
94     /* GENERATE */
95     public Object JavaDoc self_get(String JavaDoc fieldName) {
96         if(fieldName.equals("_age")) {
97             return new Integer JavaDoc(_age);
98         }
99         if(fieldName.equals("_parents")) {
100             return _parents;
101         }
102         if(fieldName.equals("_prices")) {
103             return _prices;
104         }
105         return super.self_get(fieldName);
106     }
107
108     /* GENERATE */
109     public void self_set(String JavaDoc fieldName,Object JavaDoc value) {
110         if(fieldName.equals("_age")) {
111             _age=((Integer JavaDoc)value).intValue();
112             return;
113         }
114         if(fieldName.equals("_parents")) {
115             _parents=(Dog[])value;
116             return;
117         }
118         if(fieldName.equals("_prices")) {
119             _prices=(int[])value;
120             return;
121         }
122         super.self_set(fieldName,value);
123     }
124     
125     public boolean equals(Object JavaDoc obj) {
126         if(this==obj) {
127             return true;
128         }
129         if(obj==null||getClass()!=obj.getClass()) {
130             return false;
131         }
132         Dog dog=(Dog)obj;
133         boolean sameName=(_name==null ? dog._name==null : _name.equals(dog._name));
134         boolean sameAge=_age==dog._age;
135         boolean sameParents=_parents.length==dog._parents.length;
136         if(sameParents) {
137             for (int i = 0; i < _parents.length; i++) {
138                 if(!_parents[i].equals(dog._parents[i])) {
139                     sameParents=false;
140                     break;
141                 }
142             }
143         }
144         boolean samePrices=_prices.length==dog._prices.length;
145         if(samePrices) {
146             for (int i = 0; i < _prices.length; i++) {
147                 if(!(_prices[i]==dog._prices[i])) {
148                     samePrices=false;
149                     break;
150                 }
151             }
152         }
153         
154         return sameName&&sameAge&&sameParents&&samePrices;
155     }
156     
157     public int hashCode() {
158         int hash=_age;
159         hash=hash*29+(_name==null ? 0 : _name.hashCode());
160         hash=hash*29+_parents.length;
161         return hash;
162     }
163 }
164
Popular Tags