EntityUtils.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 1k
Category:

Java Develop

Development Platform:

Java

  1. package org.springframework.samples.petclinic.util;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4. import org.springframework.orm.ObjectRetrievalFailureException;
  5. import org.springframework.samples.petclinic.Entity;
  6. /**
  7.  * Utility methods for handling entities.
  8.  * Separate from the Entity class mainly because of dependency
  9.  * on the ORM-associated ObjectRetrievalFailureException.
  10.  *
  11.  * @author Juergen Hoeller
  12.  * @since 29.10.2003
  13.  * @see org.springframework.samples.petclinic.Entity
  14.  */
  15. public abstract class EntityUtils {
  16. /**
  17.  * Look up the entity of the given class with the given id
  18.  * in the given collection.
  19.  * @param entities the collection to search
  20.  * @param entityClass the entity class to look up
  21.  * @param entityId the entity id to look up
  22.  * @return the found entity
  23.  * @throws ObjectRetrievalFailureException if the entity was not found
  24.  */
  25. public static Entity getById(Collection entities, Class entityClass, int entityId)
  26.     throws ObjectRetrievalFailureException {
  27. for (Iterator it = entities.iterator(); it.hasNext();) {
  28. Entity entity = (Entity) it.next();
  29. if (entity.getId() == entityId && entityClass.isInstance(entity)) {
  30. return entity;
  31. }
  32. }
  33. throw new ObjectRetrievalFailureException(entityClass, new Integer(entityId));
  34. }
  35. }