/**
   * Executes example.
   *
   * @param args Command line arguments, none required.
   */
  public static void main(String[] args) {
    // Typedefs:
    // ---------
    // G -> GridFactory
    // CI1 -> GridInClosure
    // CO -> GridOutClosure
    // CA -> GridAbsClosure
    // F -> GridFunc

    // Data initialisation.
    Random rand = new Random();

    final int size = 20;

    Collection<Integer> nums = new ArrayList<Integer>(size);

    // Generate list of random integers.
    for (int i = 0; i < size; i++) {
      nums.add(rand.nextInt(size));
    }

    // Print generated list.
    X.println("Generated list:");

    F.forEach(nums, F.<Integer>print("", " "));

    // Retain all elements which value low than half generated list size.
    Collection<Integer> res =
        F.retain(
            nums,
            true,
            new P1<Integer>() {
              @Override
              public boolean apply(Integer i) {
                return i < size / 2;
              }
            });

    // Print result.
    X.println("\nResult list:");

    F.forEach(res, F.<Integer>print("", " "));

    // Retain first half of result list.
    F.retain(res, false, res.size() / 2);

    // Print result.
    X.println("\nResult list:");

    F.forEach(res, F.<Integer>print("", " "));
  }
示例#2
0
  /**
   * Executes example.
   *
   * @param args Command line arguments, none required.
   */
  public static void main(String[] args) {
    // Typedefs:
    // ---------
    // G -> GridFactory
    // CI1 -> GridInClosure
    // CO -> GridOutClosure
    // CA -> GridAbsClosure
    // F -> GridFunc

    // Data initialisation.
    Random rand = new Random();

    final int size = 20;

    Collection<Integer> nums = new ArrayList<Integer>(size);

    // Generate list of random integers.
    for (int i = 0; i < size; i++) {
      nums.add(rand.nextInt(size));
    }

    // Print generated list.
    X.println("Generated list:");

    F.forEach(nums, F.<Integer>print("", " "));

    // Get new unmodifiable collection with elements which value low than half generated list size.
    Collection<Integer> view =
        F.view(
            nums,
            new P1<Integer>() {
              @Override
              public boolean apply(Integer i) {
                return i < size / 2;
              }
            });

    // Print result.
    X.println("\nResult list:");

    F.forEach(view, F.<Integer>print("", " "));

    // Check for read only.
    try {
      view.add(12);
    } catch (Exception ignored) {
      X.println("\nView is read only.");
    }
  }