Example #1
0
 public List<T> list() {
   List<T> list = new ArrayList<T>();
   Iterator<T> itr = iterator();
   try {
     while (itr.hasNext()) list.add(itr.next());
     return list;
   } finally {
     ImplHelper.close(itr);
   }
 }
  /**
   * Return the available recursion depth via the given field for the given type.
   *
   * @param traverse whether we're traversing the field
   */
  private int getAvailableRecursionDepth(
      FieldMetaData fm, Class<?> type, String fromField, boolean traverse) {
    // see if there's a previous limit
    int avail = Integer.MIN_VALUE;
    for (FetchConfigurationImpl f = this; f != null; f = f._parent) {
      if (StringUtils.equals(f._fromField, fromField)
          && ImplHelper.isAssignable(f._fromType, type)) {
        avail = f._availableRecursion;
        if (traverse) avail = reduce(avail);
        break;
      }
    }
    if (avail == 0) return 0;

    // calculate fetch groups max
    ClassMetaData meta = fm.getDefiningMetaData();
    int max = Integer.MIN_VALUE;
    if (fm.isInDefaultFetchGroup())
      max = meta.getFetchGroup(FetchGroup.NAME_DEFAULT).getRecursionDepth(fm);
    String[] groups = fm.getCustomFetchGroups();
    int cur;
    for (int i = 0; max != FetchGroup.DEPTH_INFINITE && i < groups.length; i++) {
      // ignore custom groups that are inactive in this configuration
      if (!this.hasFetchGroup(groups[i])) continue;
      cur = meta.getFetchGroup(groups[i]).getRecursionDepth(fm);
      if (cur == FetchGroup.DEPTH_INFINITE || cur > max) max = cur;
    }
    // reduce max if we're traversing a self-type relation
    if (traverse
        && max != Integer.MIN_VALUE
        && ImplHelper.isAssignable(meta.getDescribedType(), type)) max = reduce(max);

    // take min/defined of previous avail and fetch group max
    if (avail == Integer.MIN_VALUE && max == Integer.MIN_VALUE) {
      int def = FetchGroup.RECURSION_DEPTH_DEFAULT;
      return (traverse && ImplHelper.isAssignable(meta.getDescribedType(), type)) ? def - 1 : def;
    }
    if (avail == Integer.MIN_VALUE || avail == FetchGroup.DEPTH_INFINITE) return max;
    if (max == Integer.MIN_VALUE || max == FetchGroup.DEPTH_INFINITE) return avail;
    return Math.min(max, avail);
  }