/**
  * このベクトルに対して二項演算を行う。
  *
  * <p>デフォルトの実装では、渡された演算をそれぞれの次元に適用し、得られた値を{@link #set(Object, Object, Object, Object)}に委譲します。
  *
  * @param operator 現在の設定値を受け取り、新しい設定値を返す関数。
  * @param x 右辺となるx成分の値。
  * @param y 右辺となるy成分の値。
  * @param z 右辺となるz成分の値。
  * @param w 右辺となるw成分の値。
  */
 default void calculate(BinaryOperator<T> operator, T x, T y, T z, T w) {
   set(
       operator.apply(getX(), x),
       operator.apply(getY(), y),
       operator.apply(getZ(), z),
       operator.apply(getW(), w));
 }
示例#2
0
 private T evaluate(int level) {
   Deque<T> q = elements[level];
   BinaryOperator<T> op = operations[level];
   T result = q.pollFirst();
   while (!q.isEmpty()) {
     result = op.apply(result, q.pollFirst());
   }
   return result;
 }
 // l + <first|acc|last> + r
 private R connectOne(T first, R acc, T last) {
   synchronized (root) {
     Connector<T, R> l = left;
     if (l == null) {
       return pushRight(acc, last);
     }
     if (l.acc == NONE) {
       l.acc = acc;
       l.left = first;
       l.right = last;
       return connectEmpty();
     }
     T laright = l.right;
     if (mergeable.test(laright, first)) {
       l.acc = combiner.apply(l.acc, acc);
       l.right = last;
       return connectEmpty();
     }
     left = null;
     l.rhs = null;
     l.right = none();
     if (l.left != NONE) {
       return pushRight(acc, last);
     }
     acc = pushRight(acc, last);
     if (acc != NONE) left = new Connector<>(null, acc, this);
     return l.drain();
   }
 }
 // <?|acc|last> + r
 private R pushRight(R acc, T last) {
   cur = none();
   if (right == null) return acc;
   synchronized (root) {
     Connector<T, R> r = right;
     if (r == null) return acc;
     right = null;
     r.lhs = null;
     T raleft = r.left;
     r.left = none();
     if (r.acc == NONE) {
       if (acc == NONE) {
         r.drain();
       } else {
         r.acc = acc;
         r.right = last;
       }
       return none();
     }
     if (acc == NONE) {
       return r.drainRight();
     }
     if (mergeable.test(last, raleft)) {
       r.acc = combiner.apply(acc, r.acc);
       return r.drainRight();
     }
     if (r.right == NONE) right = new Connector<>(this, r.drain(), null);
     return acc;
   }
 }
示例#5
0
  default U reduce(U identity, BinaryOperator<U> accumulator) {
    if (getLastActive().isSequential()) {
      Object[] result = {identity};
      forEach(
          r -> {
            if (result[0] == null) result[0] = r;
            else {
              result[0] = accumulator.apply((U) result[0], r);
            }
          });
      return (U) result[0];
    }
    Function<FastFuture, U> safeJoin =
        (FastFuture cf) -> (U) BlockingStreamHelper.getSafe(cf, getErrorHandler());
    IncrementalReducer<U> collector =
        new IncrementalReducer(
            this.getLazyCollector().get().withResults(new ArrayList<>()),
            this,
            getParallelReduction());
    Object[] result = {identity};
    try {
      this.getLastActive()
          .injectFutures()
          .forEach(
              next -> {
                collector.getConsumer().accept(next);

                result[0] = collector.reduce(safeJoin, (U) result[0], accumulator);
              });
    } catch (SimpleReactProcessingException e) {

    }
    return collector.reduceResults(
        collector.getConsumer().getAllResults(), safeJoin, (U) result[0], accumulator);
  }
 // l + r
 private R connectEmpty() {
   synchronized (root) {
     Connector<T, R> l = left, r = right;
     if (l == null) {
       return pushRight(none(), none());
     }
     left = right = null;
     l.rhs = null;
     T laright = l.right;
     l.right = none();
     if (l.acc == NONE) {
       if (r == null) l.drain();
       else {
         if (l.lhs != null) {
           l.lhs.right = r;
           r.lhs = l.lhs;
         }
       }
       return none();
     }
     if (r == null) {
       return l.drainLeft();
     }
     r.lhs = null;
     if (r.acc == NONE) {
       if (r.rhs != null) {
         r.rhs.left = l;
         l.rhs = r.rhs;
         l.right = laright;
       }
       return none();
     }
     T raleft = r.left;
     r.left = none();
     if (mergeable.test(laright, raleft)) {
       R acc = combiner.apply(l.acc, r.acc);
       if (l.left == NONE && r.right == NONE) {
         l.drain();
         r.drain();
         return acc;
       }
       l.acc = acc;
       l.right = r.right;
       if (r.rhs != null) {
         r.rhs.left = l;
         l.rhs = r.rhs;
       }
       return none();
     }
     if (l.left == NONE) {
       if (r.right == NONE) right = new Connector<>(this, r.drain(), null);
       return l.drain();
     }
     return r.drainRight();
   }
 }
示例#7
0
 @Test
 public void binary_operatior_test() throws Exception {
   BinaryOperator.maxBy(
           new Comparator<String>() {
             @Override
             public int compare(String o1, String o2) {
               return 0; // Does not matter.
             }
           })
       .apply("hello", "world");
 }
 @Override
 public Set<Integer> onIntermediateResult(Address address, R results) {
   if (results != null) {
     synchronized (this) {
       if (currentValue != null) {
         currentValue = binaryOperator.apply(currentValue, results);
       } else {
         currentValue = results;
       }
     }
   }
   return null;
 }
示例#9
0
  public static void main(String[] args) {
    final List<Person> people =
        Arrays.asList(
            new Person("Robert", 49),
            new Person("John", 20),
            new Person("Sara", 21),
            new Person("Chloe", 18),
            new Person("Jane", 21),
            new Person("Greg", 35));

    List<Person> olderThan20_a = new ArrayList<>();
    people.stream().filter(person -> person.age > 20).forEach(person -> olderThan20_a.add(person));
    printPeople("don't trust olderThan20_a", olderThan20_a);

    List<Person> olderThan20_b =
        people
            .stream()
            .filter(person -> person.age > 20)
            .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    printPeople("don't trust olderThan20_b", olderThan20_b);

    List<Person> olderThan20_c =
        people.stream().filter(person -> person.age > 20).collect(Collectors.toList());
    printPeople("don't trust olderThan20_c", olderThan20_c);

    Map<Integer, List<Person>> peopleByAge =
        people.stream().collect(Collectors.groupingBy(person -> person.age));
    System.out.println("Grouped by age: " + peopleByAge);

    Map<Integer, List<String>> nameOfPeopleByAge =
        people
            .stream()
            .collect(
                Collectors.groupingBy(
                    person -> person.age,
                    Collectors.mapping(person -> person.name, Collectors.toList())));
    System.out.println("People grouped by age: " + nameOfPeopleByAge);

    // Group the names by their first char and then get the oldest person in
    // each group
    Comparator<Person> byAge = Comparator.comparing(person -> person.age);
    Map<Character, Optional<Person>> oldestPersonOfEachLetter =
        people
            .stream()
            .collect(
                Collectors.groupingBy(
                    person -> person.name.charAt(0),
                    Collectors.reducing(BinaryOperator.maxBy(byAge))));
    System.out.println("Oldest person of each letter:");
    System.out.println(oldestPersonOfEachLetter);
  }
示例#10
0
  public static void main(String[] args) {
    final List<Person> people =
        Arrays.asList(
            new Person("John", 20),
            new Person("Sara", 21),
            new Person("Jane", 21),
            new Person("Greg", 35));

    List<Person> olderThan20 =
        people
            .stream()
            .filter(person -> person.getAge() > 20)
            .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
    System.out.println("People older than 20: " + olderThan20);

    List<Person> olderThan20_1 =
        people.stream().filter(person -> person.getAge() > 20).collect(Collectors.toList());
    System.out.println("People older than 20: " + olderThan20_1);

    Map<Integer, List<Person>> peopleByAge =
        people.stream().collect(Collectors.groupingBy(Person::getAge));
    System.out.println("Grouped by age: " + peopleByAge);

    Map<Integer, List<String>> nameOfPeopleByAge =
        people
            .stream()
            .collect(
                Collectors.groupingBy(
                    Person::getAge, Collectors.mapping(Person::getName, Collectors.toList())));
    System.out.println("People name grouped by age: " + nameOfPeopleByAge);

    Comparator<Person> byAge = Comparator.comparing(Person::getAge);
    Map<Character, Optional<Person>> oldestPersonOfEachCharacter =
        people
            .stream()
            .collect(
                Collectors.groupingBy(
                    person -> person.getName().charAt(0),
                    Collectors.reducing(BinaryOperator.maxBy(byAge))));
    System.out.println("Oldest person of each letter: " + oldestPersonOfEachCharacter);
  }
示例#11
0
  default Optional<U> reduce(BinaryOperator<U> accumulator) {
    if (getLastActive().isSequential()) {
      Object[] result = {null};
      forEach(
          r -> {
            if (result[0] == null) result[0] = r;
            else {
              result[0] = accumulator.apply((U) result[0], r);
            }
          });
      return (Optional) Optional.ofNullable(result[0]);
    }
    Function<FastFuture, U> safeJoin =
        (FastFuture cf) -> (U) BlockingStreamHelper.getSafe(cf, getErrorHandler());
    IncrementalReducer<U> collector =
        new IncrementalReducer(
            this.getLazyCollector().get().withResults(new ArrayList<>()),
            this,
            getParallelReduction());
    Optional[] result = {Optional.empty()};
    try {
      this.getLastActive()
          .injectFutures()
          .forEach(
              next -> {
                collector.getConsumer().accept(next);
                if (!result[0].isPresent()) result[0] = collector.reduce(safeJoin, accumulator);
                else result[0] = result[0].map(v -> collector.reduce(safeJoin, (U) v, accumulator));
              });
    } catch (SimpleReactProcessingException e) {

    }

    if (result[0].isPresent())
      return result[0].map(
          v ->
              collector.reduceResults(
                  collector.getConsumer().getAllResults(), safeJoin, (U) v, accumulator));

    return collector.reduceResults(collector.getConsumer().getAllResults(), safeJoin, accumulator);
  }
示例#12
0
 public static <T> Aggregator<T, T, ?> minBy(Comparator<? super T> comparator) {
   return reducing(BinaryOperator.minBy(comparator));
 }
示例#13
0
 public void test() {
   BinaryOperator<Long> operator = (l1, l2) -> l1 + l2;
   System.out.println(operator.apply(1L, 2L));
 }