KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > prevayler > ObjectPrevayler


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.buchuki.ensmer.prevayler;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.*;
20 import javax.vecmath.Matrix4f;
21 import com.buchuki.annotations.*;
22
23 import com.buchuki.ensmer.object.Backend;
24
25 /**
26  * This class is responsible for managing prevayled objects related to the
27  * Backhoe and Area. At its root, it manages a list of all objects currently in
28  * the world.
29  *
30  * @author Dusty Phillips [dusty@buchuki.com]
31  */

32 public class ObjectPrevayler implements Serializable JavaDoc {
33
34     /**
35      * Give it a SerialVersionUID so it can be reinstantiated by later versions
36      * of the class
37      */

38     static final long serialVersionUID = 20050328L;
39     
40     /**
41      * Retrieve the next available number to use as an Objech Identifier. This
42      * method could cause problems if accessed by two running threads. However,
43      * because Prevayler serializes transactions, it does not need to be
44      * synchronized.
45      *
46      * @return the next available area identifier
47      */

48     public Long JavaDoc nextObjectIdentifier() {
49         return ++lastObjectIdentifier;
50     }
51     
52     
53     /**
54      * Add an object to the list of objects based on an ID and the class of the
55      * object to be added.
56      *
57      * @param id the identifier of the object being added
58      * @param backendClass the class of the backend being added
59      */

60     public void addObject(Long JavaDoc id, Class JavaDoc backendClass) {
61         PrevayledObject object = new PrevayledObject(backendClass);
62         objectMap.put(id, object);
63     }
64     
65     /**
66      * Remove an object from the list of objects. Once removed, the object is gone
67      * forever and the ID could theoretically be reused.
68      *
69      * @param id the identifier of the object being removed
70      */

71     public void removeObject(Long JavaDoc id) {
72         PrevayledObject obj = objectMap.remove(id);
73         if (obj != null) {
74             Long JavaDoc areaID = obj.getAreaId();
75             if (areaID != null) {
76                 areaMap.get(areaID).remove(id);
77             }
78         }
79     }
80     
81     /**
82      * Obtain a list of identifiers for currently active objects. return array of
83      * enabled object identifiers
84      *
85      * @return All active object identifiers
86      */

87     public Long JavaDoc[] getObjectIdentifiers() {
88         Set objectList = objectMap.keySet();
89         Long JavaDoc objects[] = new Long JavaDoc[objectList.size()];
90         Iterator it = objectList.iterator();
91         int count = 0;
92         while (it.hasNext()) {
93             objects[count] = (Long JavaDoc) it.next();
94             count++;
95         }
96         return objects;
97     }
98     
99     /**
100      * Obtain a list of identifiers for a specific area.
101      *
102      * @param areaID the Area to retrieve Identifiers for
103      * @return all object identifiers active in that Area
104      */

105     public List<Long JavaDoc> getObjectIdentifiers(Long JavaDoc areaID) {
106         if (areaMap.get(areaID) == null) {
107             return new ArrayList<Long JavaDoc>();
108         }
109         return new ArrayList<Long JavaDoc>(areaMap.get(areaID));
110     }
111     
112     /**
113      * Gets the location of the object with the specified identifier
114      *
115      * @param id The identifier to get a location for
116      * @return The location for that identifier, or null if the object hasn't
117      * been located yet (or the ID doesn't exist).
118      */

119     public Matrix4f getLocation(Long JavaDoc id) {
120         PrevayledObject object = objectMap.get(id);
121         if (object != null) {
122             return object.getLocation();
123         }
124         return null;
125     }
126     
127     /**
128      * Gets the data for the object with the specified identifier
129      *
130      * @param id The identifier to get data for
131      * @return The data for that identifier, or null if no data has been stored
132      * yet.
133      */

134     public Serializable JavaDoc getData(Long JavaDoc id) {
135         PrevayledObject object = objectMap.get(id);
136         if (object != null) {
137             return object.getData();
138         }
139         return null;
140     }
141     
142     /**
143      * Gets the area identifier for the object with the specified id
144      *
145      * @param id The identifier to get a location for
146      * @return The area identifier for that object
147      */

148     public Long JavaDoc getAreaId(Long JavaDoc id) {
149         PrevayledObject object = objectMap.get(id);
150         if (object != null) {
151             return object.getAreaId();
152         }
153         return null;
154     }
155     
156     /**
157      * Gets the class of the backend for the specified ID
158      *
159      * @param id the id to retrieve a backend class for
160      * @return The class of the backend for that id, or null if that id doesn't
161      * exist.
162      */

163     public Class JavaDoc getBackendClass(Long JavaDoc id) {
164         PrevayledObject object = objectMap.get(id);
165         if (object != null) {
166             return object.getBackendClass();
167         }
168         return null;
169     }
170     
171     /**
172      * Sets the location of the object with the given identifier
173      *
174      * @param id The identifier to set a location for
175      * @param location The new location value
176      */

177     public void setLocation(Long JavaDoc id, Matrix4f location) {
178         PrevayledObject object = objectMap.get(id);
179         if (object != null) {
180             object.setLocation(location);
181         }
182     }
183     
184     /**
185      * Sets the data of the object with the given identifier
186      *
187      * @param id The identifier to set data for
188      * @param data The new data value
189      */

190     public void setData(Long JavaDoc id, Serializable JavaDoc data) {
191         PrevayledObject object = objectMap.get(id);
192         if (object != null) {
193             object.setData(data);
194         }
195     }
196     
197     /**
198      * Sets the area identifier for the object with the given identifier
199      *
200      * @param id The identifier to set area identifier for
201      * @param areaID The new area identifier value
202      */

203     public void setAreaId(Long JavaDoc id, Long JavaDoc areaID) {
204         PrevayledObject object = objectMap.get(id);
205         if (object != null) {
206             Long JavaDoc oldAreaID = object.getAreaId();
207             if (oldAreaID != null) {
208                 areaMap.get(oldAreaID).remove(id);
209             }
210             object.setAreaId(areaID);
211             if (!areaMap.containsKey(areaID)) {
212                 areaMap.put(areaID, new ArrayList<Long JavaDoc>());
213             }
214             areaMap.get(areaID).add(id);
215         }
216     }
217     
218     /**
219      * An indicator of the next valid Object Identifier.
220      */

221     private long lastObjectIdentifier = 0;
222     
223     /**
224      * Map of <code>Long</code> Object Identifiers to
225      * <code>PrevayledObject</code> s.
226      */

227     private Map<Long JavaDoc, PrevayledObject> objectMap =
228         new HashMap<Long JavaDoc, PrevayledObject>();
229     
230     /**
231      * Map of <code>Long</code> Area identifiers to lists of
232      * object identifiers
233      */

234     private Map<Long JavaDoc, List<Long JavaDoc>> areaMap = new
235         HashMap<Long JavaDoc, List<Long JavaDoc>>();
236 }
237
238
Popular Tags