Пример #1
0
 /**
  * Retrieves an entity of the given type from the graph randomly.
  *
  * @param <E>
  * @param entityType
  * @return The randomly selected entity from the graph.
  */
 protected final <E extends IEntity> E getRandomEntity(Class<E> entityType) {
   final Set<E> set = graph.getEntitiesByType(entityType);
   if (set == null || set.size() == 0) {
     throw new IllegalStateException("No entities of the given type yet exist.");
   } else if (set.size() == 1) {
     return set.iterator().next();
   }
   return getNthEntity(entityType, randomInt(set.size()) + 1);
 }
Пример #2
0
 /**
  * Attempts to retrive the Nth (1-based) entity of the given type.
  *
  * @param <E>
  * @param entityType
  * @param name the entity name (entity type must be implement {@link INamedEntity}).
  * @return the matching entity or <code>null</code> if not found
  * @throws IllegalArgumentException When the given entity type does not implement {@link
  *     INamedEntity}
  */
 protected final <E extends IEntity> E getEntityByName(Class<E> entityType, String name)
     throws IllegalArgumentException {
   if (!INamedEntity.class.isAssignableFrom(entityType)) {
     throw new IllegalArgumentException(
         "Entity type: " + entityType + " is not a named entity type");
   }
   final Set<E> set = graph.getEntitiesByType(entityType);
   if (set != null) {
     for (final E e : set) {
       final String ename = ((INamedEntity) e).getName();
       if (ename != null && ename.equals(name)) return e;
     }
   }
   // not found
   return null;
 }
Пример #3
0
 /**
  * Attempts to retrive the Nth (1-based) entity of the given type.
  *
  * @param <E>
  * @param entityType
  * @param n 1-based
  * @return the Nth entity or <code>null</code> if n is greater than the number of entities of the
  *     given type.
  */
 protected final <E extends IEntity> E getNthEntity(Class<E> entityType, int n) {
   final Set<E> set = graph.getEntitiesByType(entityType);
   final int size = set == null ? 0 : set.size();
   if (set != null && size >= n) {
     int i = 0;
     for (final E e : set) {
       if (++i == n) {
         return e;
       }
     }
   }
   throw new IllegalStateException(
       size
           + " entities exist of type "
           + entityType
           + " exist but number "
           + n
           + " was requested.");
 }