View Javadoc

1   package org.sourceforge.jemm.database.components;
2   
3   import org.sourceforge.jemm.collections.internal.ClearRequest;
4   import org.sourceforge.jemm.collections.internal.ContainsRequest;
5   import org.sourceforge.jemm.collections.internal.ContainsResponse;
6   import org.sourceforge.jemm.collections.internal.RemoveRequest;
7   import org.sourceforge.jemm.collections.internal.RemoveResponse;
8   import org.sourceforge.jemm.collections.internal.SizeRequest;
9   import org.sourceforge.jemm.collections.internal.SizeResponse;
10  import org.sourceforge.jemm.collections.internal.StoredValue;
11  import org.sourceforge.jemm.collections.internal.ToArrayRequest;
12  import org.sourceforge.jemm.collections.internal.ToArrayResponse;
13  import org.sourceforge.jemm.collections.internal.VoidResponse;
14  import org.sourceforge.jemm.collections.internal.list.ListAddAllRequest;
15  import org.sourceforge.jemm.collections.internal.list.ListAddAllResponse;
16  import org.sourceforge.jemm.collections.internal.list.ListAddRequest;
17  import org.sourceforge.jemm.collections.internal.list.ListAddResponse;
18  import org.sourceforge.jemm.collections.internal.list.ListGetRequest;
19  import org.sourceforge.jemm.collections.internal.list.ListGetResponse;
20  import org.sourceforge.jemm.collections.internal.list.ListIndexOfRequest;
21  import org.sourceforge.jemm.collections.internal.list.ListIndexOfResponse;
22  import org.sourceforge.jemm.collections.internal.list.ListRemoveIndexRequest;
23  import org.sourceforge.jemm.collections.internal.list.ListRemoveIndexResponse;
24  import org.sourceforge.jemm.collections.internal.list.ListSetRequest;
25  import org.sourceforge.jemm.collections.internal.list.ListSetResponse;
26  import org.sourceforge.jemm.collections.internal.list.SubListRequest;
27  import org.sourceforge.jemm.collections.internal.list.SubListResponse;
28  import org.sourceforge.jemm.database.ClassId;
29  import org.sourceforge.jemm.database.ClientId;
30  import org.sourceforge.jemm.database.ObjectAccessor;
31  import org.sourceforge.jemm.database.components.interfaces.DBClientRefHandler;
32  import org.sourceforge.jemm.database.components.types.StoredListObject;
33  import org.sourceforge.jemm.database.components.types.StoredObject;
34  import org.sourceforge.jemm.lifecycle.TypeRequest;
35  import org.sourceforge.jemm.lifecycle.TypeResponse;
36  import org.sourceforge.jemm.types.ID;
37  
38  /**
39   * Default JemmList type handler implementation.
40   *
41   * @author Rory Graves
42   */
43  public class DefaultDBListTypeHandler extends TypeHandler {
44  
45  	private final ObjectAccessor objectAccessor;
46  	
47  	public DefaultDBListTypeHandler(ClassId classId,ObjectAccessor objectAccessor,DBClientRefHandler clientRefHandler) {
48  		super(classId,clientRefHandler);
49  		this.objectAccessor = objectAccessor;
50  	}
51  	
52  	private StoredListObject getList(ID id) {
53  		return objectAccessor.getListObject(id);
54  	}
55  		
56  	public boolean add(ClientId clientId,ID id, StoredValue value) {
57  		StoredListObject slo = getList(id);
58  		try {
59  			return slo.add(value);
60  		}finally {
61  			objectAccessor.release(slo);
62  		}
63  	}
64  
65  	public void add(ClientId clientId,ID id, int index, StoredValue value) {
66  		StoredListObject slo = getList(id);
67  		try {
68  			slo.add(index,value);
69  		} finally {
70  			objectAccessor.release(slo);
71  		}
72  	}
73  
74  	public boolean addAll(ClientId clientId,ID id, StoredValue[] values) {
75  		StoredListObject slo = getList(id);
76  		try {
77  			return slo.addAll(values);
78  		} finally {
79  			objectAccessor.release(slo);
80  		}
81  	}
82  
83  	public boolean addAll(ClientId clientId,ID id, int index, StoredValue[] values) {
84  		StoredListObject slo = getList(id);
85  		try {
86  			return slo.addAll(index,values);
87  		} finally {
88  			objectAccessor.release(slo);
89  		}
90  	}
91  
92  	public void clear(ClientId clientId,ID id) {
93  		StoredListObject slo = getList(id);
94  		try {
95  			slo.clear();
96  		} finally {
97  			objectAccessor.release(slo);
98  		}
99  	}
100 
101 	public ContainsResponse contains(ClientId clientId,ID id, StoredValue element) {
102 		StoredListObject slo = getList(id);
103 		try {
104 			return slo.contains(element);
105 		} finally {
106 			objectAccessor.release(slo);
107 		}
108 	}
109 
110 	public Object get(ClientId clientId,ID id, int index) {
111 		StoredListObject slo = getList(id);
112 		try {
113 			Object result = slo.get(index);
114 			return recordReference(clientId, result);
115 		} finally {
116 			objectAccessor.release(slo);
117 		}
118 	}
119 
120 	public ListIndexOfResponse indexOf(ClientId clientId,ID id, StoredValue toFind) {
121 		StoredListObject slo = getList(id);
122 		try {
123 			return slo.indexOf(toFind);
124 		} finally {
125 			objectAccessor.release(slo);
126 		}
127 	}
128 
129 	public boolean isEmpty(ClientId clientId,ID id) {
130 		StoredListObject slo = getList(id);
131 		try {
132 			return slo.isEmpty();
133 		} finally {
134 			objectAccessor.release(slo);
135 		}
136 	}
137 
138 	public ListIndexOfResponse lastIndexOf(ClientId clientId,ID id, StoredValue toFind) {
139 		StoredListObject slo = getList(id);
140 		try {
141 			return slo.lastIndexOf(toFind);
142 		} finally {
143 			objectAccessor.release(slo);
144 		}
145 	}
146 
147 	public RemoveResponse remove(ClientId clientId,ID id, StoredValue element) {
148 		StoredListObject slo = getList(id);
149 		try {
150 			return slo.remove(element);
151 		} finally {
152 			objectAccessor.release(slo);
153 		}
154 	}
155 
156 	public Object remove(ClientId clientId,ID id, int index) {
157 		StoredListObject slo = getList(id);
158 		try {
159 			Object result = slo.remove(index);
160 			return recordReference(clientId, result);
161 		} finally {
162 			objectAccessor.release(slo);
163 		}
164 	}
165 
166 	public Object set(ClientId clientId,ID id, int index, StoredValue element) {
167 		StoredListObject slo = getList(id);
168 		try {
169 			Object result = slo.set(index,element);
170 			return recordReference(clientId, result);
171 		} finally {
172 			objectAccessor.release(slo);
173 		}
174 	}
175 
176 	public int size(ClientId clientId,ID id) {
177 		StoredListObject slo = getList(id);
178 		try {
179 			return slo.size();
180 		} finally {
181 			objectAccessor.release(slo);
182 		}
183 	}
184 
185 	public Object[] toArray(ClientId clientId,ID id) {
186 		StoredListObject slo = getList(id);
187 		
188 		try {
189 			Object[] values = slo.toArray();
190 			return recordReference(clientId, values);
191 		} finally {
192 			objectAccessor.release(slo);
193 		}
194 	}
195 
196 	private ID subList(ClientId clientId, ID id, int fromIndex,int toIndex) {
197 		StoredListObject slo = null;
198 		StoredListObject subList = null;
199 		try {					
200 			slo = getList(id);
201 			int listSize = slo.size();
202 			if(fromIndex < 0)
203 				throw new IndexOutOfBoundsException("fromIndex (" + fromIndex + ") must be >=0");
204 			if(toIndex > listSize)
205 				throw new IndexOutOfBoundsException("toIndex (" + toIndex 
206 						+ ") must be <= list size (" + listSize + ")");
207 			if(fromIndex > toIndex)
208 				throw new IndexOutOfBoundsException("fromIndex (" + fromIndex 
209 						+ ") must be <= to toIndex (" + toIndex + ")");
210 			
211 			ID subListId = objectAccessor.createObject(clientId, classId);
212 			subList = objectAccessor.getListObject(subListId);
213 				
214 			slo.fillSubList(subList,fromIndex,toIndex);
215 			return recordReference(clientId, subListId);
216 		}finally {
217 			if(subList != null)
218 				objectAccessor.release(subList);
219 			if(slo != null)
220 				objectAccessor.release(slo);			
221 		}
222 	}
223 
224 	@Override
225 	public StoredObject createStorageObject(ID id) {
226 		return new StoredListObject(id,classId);
227 	}
228 
229 	@Override
230 	public TypeResponse<?> processRequest(ClientId clientId, ClassId classId,
231 			ID id, TypeRequest<?> request) {
232 
233 		if(request instanceof ClearRequest) {
234 			clear(clientId, id);
235 			return new VoidResponse();
236 		} else if(request instanceof SizeRequest) {
237 			return new SizeResponse(size(clientId,id));
238 		} else if(request instanceof ListGetRequest) {
239 			ListGetRequest gRequest = (ListGetRequest) request;			
240 			return new ListGetResponse(get(clientId,id,gRequest.getIndex()));
241 		} else if(request instanceof SubListRequest) {
242 			SubListRequest sRequest = (SubListRequest) request;
243 			return new SubListResponse(subList(clientId,id,sRequest.getFromIndex(),sRequest.getToIndex()));
244 		} else if(request instanceof ToArrayRequest) {
245 			return new ToArrayResponse(toArray(clientId, id));
246 		} else if(request instanceof ContainsRequest) {
247 			StoredValue cValue = ((ContainsRequest) request).getValue();
248 			return contains(clientId,id,cValue);
249 		} else if(request instanceof RemoveRequest) {
250 			StoredValue rValue = ((RemoveRequest) request).getValue();
251 			return remove(clientId,id,rValue);
252 		} else if(request instanceof ListRemoveIndexRequest) {
253 			int index = ((ListRemoveIndexRequest) request).getIndex();
254 			return new ListRemoveIndexResponse(remove(clientId,id,index));
255 		} else if(request instanceof ListSetRequest) {
256 			int index = ((ListSetRequest) request).getIndex();
257 			StoredValue value = ((ListSetRequest) request).getNewValue();
258 			return new ListSetResponse(set(clientId,id,index,value));
259 		} else if(request instanceof ListIndexOfRequest) {
260 			ListIndexOfRequest liRequest = (ListIndexOfRequest) request;
261 			if(liRequest.isReverseSearch())
262 				return lastIndexOf(clientId, id, liRequest.getValue());
263 			else
264 				return indexOf(clientId, id, liRequest.getValue());
265 		} else if(request instanceof ListAddRequest) {
266 			ListAddRequest aaReq = (ListAddRequest) request;
267 			int index = aaReq.getIndex();
268 			StoredValue value = aaReq.getValue();
269 			if(index == -1)
270 				add(clientId, id, value);
271 			else
272 				add(clientId, id, index, value);
273 			return recordReferences(clientId, new ListAddResponse(true));
274 		} else if(request instanceof ListAddAllRequest) {
275 			ListAddAllRequest aaReq = (ListAddAllRequest) request;
276 			int index = aaReq.getIndex();
277 			StoredValue[] values = aaReq.getValues();
278 			boolean modified;
279 			if(index == -1)
280 				modified = addAll(clientId, id, values);
281 			else
282 				modified = addAll(clientId, id, index, values);
283 			return recordReferences(clientId,new ListAddAllResponse(modified));
284 		}
285 
286 		throw new IllegalStateException("Unhandled request type " + request.getClass());				
287 	}
288 }