01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.compass.sample.petclinic.util;
18:
19: import java.util.Collection;
20: import java.util.Iterator;
21:
22: import org.compass.sample.petclinic.Entity;
23: import org.springframework.orm.ObjectRetrievalFailureException;
24:
25: /**
26: * Utility methods for handling entities.
27: * Separate from the Entity class mainly because of dependency
28: * on the ORM-associated ObjectRetrievalFailureException.
29: *
30: * @author Juergen Hoeller
31: * @since 29.10.2003
32: * @see org.springframework.samples.petclinic.Entity
33: */
34: public abstract class EntityUtils {
35:
36: /**
37: * Look up the entity of the given class with the given id
38: * in the given collection.
39: * @param entities the collection to search
40: * @param entityClass the entity class to look up
41: * @param entityId the entity id to look up
42: * @return the found entity
43: * @throws ObjectRetrievalFailureException if the entity was not found
44: */
45: public static Entity getById(Collection entities,
46: Class entityClass, int entityId)
47: throws ObjectRetrievalFailureException {
48:
49: for (Iterator it = entities.iterator(); it.hasNext();) {
50: Entity entity = (Entity) it.next();
51: if (entity.getId().intValue() == entityId
52: && entityClass.isInstance(entity)) {
53: return entity;
54: }
55: }
56: throw new ObjectRetrievalFailureException(entityClass,
57: new Integer(entityId));
58: }
59:
60: }
|