Пример #1
0
  /**
   * This flattens a list.
   *
   * @param o object that might be a list
   * @param list list to add o to or all of o's items to.
   * @return an object or a list
   */
  public static Object unifyListOrArray(Object o, List list) {

    if (o == null) {
      return null;
    }

    boolean isArray = o.getClass().isArray();

    if (list == null && !isArray && !(o instanceof Iterable)) {
      return o;
    }

    if (list == null) {
      list = new LinkedList();
    }

    if (isArray) {
      int length = Array.getLength(o);

      for (int index = 0; index < length; index++) {

        Object o1 = Array.get(o, index);
        if (o1 instanceof Iterable || o.getClass().isArray()) {
          unifyListOrArray(o1, list);
        } else {
          list.add(o1);
        }
      }
    } else if (o instanceof Collection) {

      Collection i = ((Collection) o);

      for (Object item : i) {

        if (item instanceof Iterable || o.getClass().isArray()) {
          unifyListOrArray(item, list);
        } else {
          list.add(item);
        }
      }

    } else {

      list.add(o);
    }

    return list;
  }
Пример #2
0
  /**
   * This flattens a list.
   *
   * @param o object that might be a list
   * @param list list to add o to or all of o's items to.
   * @return an object or a list
   */
  public static Object unifyList(Object o, List list) {

    if (o == null) {
      return null;
    }

    if (list == null) {
      list = new ArrayList();
    }

    if (o instanceof Iterable) {
      Iterable i = ((Iterable) o);

      for (Object item : i) {

        unifyListOrArray(item, list);
      }

    } else {
      list.add(o);
    }

    return list;
  }