Example #1
0
  /**
   * Convert an arithmetic expression in infix notation (e.g 1+2) to postfix notation (e.g 12+) Only
   * simple expressions not containing brackets are supported. <br>
   * A description of the algorithm used to accomplish this task can be found at:
   * http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm <br>
   * Basically the list of {@code Tokens} is being scanned from left to right, all operands
   * (numbers) are pushed onto a {@code Stack}. If the {@code Token} is an operator it is compared
   * to the operator on top of the {@code Stack}. If the operator on top of the {@code Stack} has a
   * higher or equal weight than the current operator, the top of the {@code Stack} is pushed to the
   * postfix list. Therefore the operators with higher weight are going to be executed first when
   * the expression is evaluated. This process continues until the top of the {@code Stack} has a
   * lower weight than the current operator or the {@code Stack} is empty. Then the current operator
   * is pushed onto the {@code Stack}.
   *
   * @param tokenizer The {@code Tokenizer} which will be converted to postfix. It must not be
   *     {@code null}.
   * @return The resulting {@code List} of {@code Token}s in postfix order.
   */
  private static List<Token> postfix(Tokenizer tokenizer) {
    Stack<Token> stack = new Stack<>();
    List<Token> postfix = new LinkedList<>();
    BiPredicate<Token, Token> hasHigherOrEqualWeight =
        (t1, t2) ->
            Operators.getOpWeight(t1.getOperator()) - Operators.getOpWeight(t2.getOperator()) >= 0;

    while (tokenizer.hasNext()) {
      Token t = tokenizer.next();
      switch (t.getType()) {
        case NUMBER:
          postfix.add(t);
          break;
        case OPERATOR:
          if (!stack.isEmpty() && hasHigherOrEqualWeight.test(stack.peek(), t)) {
            while (!stack.isEmpty() && hasHigherOrEqualWeight.test(stack.peek(), t)) {
              postfix.add(stack.pop());
            }
            stack.push(t);
          } else {
            stack.push(t);
          }
          break;
        case OPENING_BRACKET:
        case CLOSING_BRACKET:
          throw new IllegalArgumentException(
              "Cannot create postfix expression if the source contains brackets.");
      }
    }

    while (!stack.isEmpty()) postfix.add(stack.pop());

    return postfix;
  }
  private boolean compatible(Bet bet1, Bet bet2) {

    Map<BetType, BiPredicate<Bet, Bet>> subMap;
    BiPredicate<Bet, Bet> check;

    if ((subMap = COMPATIBLE_CHECKS.getOrDefault(bet1.getType(), null)) != null
        && (check = subMap.getOrDefault(bet2.getType(), null)) != null)
      return check.test(bet1, bet2);
    else if ((subMap = COMPATIBLE_CHECKS.getOrDefault(bet2.getType(), null)) != null
        && (check = subMap.getOrDefault(bet1.getType(), null)) != null)
      return check.test(bet2, bet1);
    else return true;
  }
 // 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;
   }
 }
 // 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();
   }
 }
Example #6
0
  private <E extends Exception> void assertBiPredicate(
      BiPredicate<Object, Object> test, Class<E> type) {
    assertNotNull(test);

    try {
      test.test(null, null);
      fail();
    } catch (Exception e) {
      assertException(type, e, "null:null");
    }
  }
Example #7
0
 final boolean forEachWhile(BiPredicate<? super K, ? super V> predicate) {
   for (long a,
           tail,
           allocations = (a = ~bitSet) << (tail = Long.numberOfLeadingZeros(a)),
           allocIndex = 64 - tail;
       allocations != 0;
       allocations <<= 1, allocIndex--) {
     if (allocations < 0
         && !predicate.test(this.<K>readKey(allocIndex), this.<V>readValue(allocIndex))) {
       return false;
     }
   }
   return true;
 }
Example #8
0
  @Override
  public ImmutableMap<K, V> filtered(BiPredicate<? super K, ? super V> condition) {
    Object[] newValues = this.values.clone();
    int newSize = this.size;
    int len = this.values.length;

    for (int i = 0; i < len; i++) {
      Object value = this.values[i];
      if (value != null) {
        if (!condition.test(this.keys[i], (V) value)) {
          newValues[i] = null;
          newSize--;
        }
      }
    }
    return new EnumMap(this.type, this.keys, newValues, newSize);
  }
Example #9
0
 final int removeIf(
     SmoothieMap<K, V> map, BiPredicate<? super K, ? super V> filter, int modCount) {
   if (isEmpty()) return modCount;
   long startSlot = 0;
   while (readSlot(startSlot) != 0) {
     startSlot++;
   }
   for (long slotIndex = startSlot + 1, slot, allocIndex;
       slotIndex != startSlot;
       slotIndex = nextSlotIndex(slotIndex)) {
     if ((slot = readSlot(slotIndex)) != 0) {
       if (filter.test(readKey((allocIndex = allocIndex(slot))), readValue(allocIndex))) {
         if (remove(map, slotIndex, allocIndex)) slotIndex = prevSlotIndex(slotIndex);
         modCount++;
       }
     }
   }
   return modCount;
 }
Example #10
0
 public boolean test(Path path, BasicFileAttributes attrs) {
   visited.add(path);
   return pred.test(path, attrs);
 }
Example #11
0
 @Test
 public void canAdaptPredicate() {
   final Predicate<Pair<O, O>> predicate = new Always<>();
   final BiPredicate<O, O> adapted = Tuples.Pairs.untupled(predicate);
   Assert.assertTrue(adapted.test(O.IGNORED, O.IGNORED));
 }
Example #12
0
 private Function<Object[], Boolean> normalize(BiPredicate<Object[], Object[]> source) {
   return pp -> source.test(pp, pp);
 }