KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > petclinic > util > EntityUtils


1 package org.springframework.samples.petclinic.util;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import org.springframework.orm.ObjectRetrievalFailureException;
7 import org.springframework.samples.petclinic.Entity;
8
9 /**
10  * Utility methods for handling entities.
11  * Separate from the Entity class mainly because of dependency
12  * on the ORM-associated ObjectRetrievalFailureException.
13  *
14  * @author Juergen Hoeller
15  * @since 29.10.2003
16  * @see org.springframework.samples.petclinic.Entity
17  */

18 public abstract class EntityUtils {
19
20     /**
21      * Look up the entity of the given class with the given id
22      * in the given collection.
23      * @param entities the collection to search
24      * @param entityClass the entity class to look up
25      * @param entityId the entity id to look up
26      * @return the found entity
27      * @throws ObjectRetrievalFailureException if the entity was not found
28      */

29     public static Entity getById(Collection JavaDoc entities, Class JavaDoc entityClass, int entityId)
30         throws ObjectRetrievalFailureException {
31
32         for (Iterator JavaDoc it = entities.iterator(); it.hasNext();) {
33             Entity entity = (Entity) it.next();
34             if (entity.getId().intValue() == entityId && entityClass.isInstance(entity)) {
35                 return entity;
36             }
37         }
38         throw new ObjectRetrievalFailureException(entityClass, new Integer JavaDoc(entityId));
39     }
40
41 }
42
Popular Tags