/** * Chooses one of the given generators according to their frequency. * Only generators with positive frequencies ares used in returned * generator. * * @param generators A non-empty traversable of Tuples (frequency, generator) * @param <T> Type to be generated * @return A new T generator * @throws java.lang.NullPointerException if generators is null * @throws java.lang.IllegalArgumentException if generators doesn't contain any generator with positive frequency */ static <T> Gen<T> frequency(Iterable<Tuple2<Integer, Gen<T>>> generators) { Objects.requireNonNull(generators, "generators is null"); final Vector<Tuple2<Integer, Gen<T>>> filtered = Iterator.ofAll(generators) .filter(t -> t._1() > 0).toVector(); if (filtered.isEmpty()) { throw new IllegalArgumentException("no generator with positive weight"); } final int size = filtered.map(t -> t._1).sum().intValue(); return choose(1, size).flatMap(n -> GenModule.frequency(n, filtered.iterator())); }
/** * Sets the 2nd element of this tuple to the given {@code value}. * * @param value the new value * @return a copy of this tuple with a new value for the 2nd element of this Tuple. */ public Tuple2<T1, T2> update2(T2 value) { return new Tuple2<>(_1, value); }
@Override public R apply(T obj) { return pattern.apply(obj).apply(f); }
private static boolean isExpired(final Tuple2<Loan, Instant> p) { final Instant now = DateUtil.now(); final Instant expiration = p._2().plus(EVICT_AFTER); return expiration.isBefore(now); }
@Override public <U> TreeSet<Tuple2<T, U>> zipAll(Iterable<? extends U> that, T thisElem, U thatElem) { Objects.requireNonNull(that, "that is null"); final Comparator<Tuple2<T, U>> tuple2Comparator = Tuple2.comparator(tree.comparator(), Comparators.naturalComparator()); return TreeSet.ofAll(tuple2Comparator, iterator().zipAll(that, thisElem, thatElem)); }
@Override public int compareTo(Tuple2<T1, T2> that) { return Tuple2.compareTo(this, that); }
private static boolean isExpired(final Tuple2<Loan, Instant> p) { final Instant now = DateUtil.now(); final Instant expiration = p._2().plus(EVICT_AFTER); return expiration.isBefore(now); }
@Override default <U> TreeSet<Tuple2<T, U>> zipAll(Iterable<? extends U> that, T thisElem, U thatElem) { Objects.requireNonNull(that, "that is null"); final Comparator<Tuple2<T, U>> tuple2Comparator = Tuple2.comparator(comparator(), Comparators.naturalComparator()); return TreeSet.ofAll(tuple2Comparator, iterator().zipAll(that, thisElem, thatElem)); }
@Override public int compareTo(Tuple2<T1, T2> that) { return Tuple2.compareTo(this, that); }
public List<Set<Card>> dealHands(List<Card> shuffled, int hands, int cardsPerHand) { List<Set<Card>> list = List.empty(); for (int i = 0; i < hands; i++) { Tuple2<Set<Card>, List<Card>> tuple2 = this.deal(shuffled, cardsPerHand); shuffled = tuple2._2(); list = list.append(tuple2._1()); } return list; }
/** * Creates a tuple of two elements. * * @param <T1> type of the 1st element * @param <T2> type of the 2nd element * @param t1 the 1st element * @param t2 the 2nd element * @return a tuple of two elements. */ static <T1, T2> Tuple2<T1, T2> of(T1 t1, T2 t2) { return new Tuple2<>(t1, t2); }
Optional<Loan> getLoanFromCache(final int loanId) { final Tuple2<Loan, Instant> result = storage.get(loanId); if (result == null || isExpired(result)) { LOGGER.trace("Miss for loan #{}.", loanId); return Optional.empty(); } else { LOGGER.trace("Hit for loan #{}.", loanId); return Optional.of(result._1()); } }
return Iterator.ofAll((Iterable<Either<L, R>>) eithers) .partition(Either::isLeft) .apply((leftPartition, rightPartition) -> leftPartition.hasNext() ? Either.left(leftPartition.map(Either::getLeft).toVector()) : Either.right(rightPartition.map(Either::get).toVector())
@Override default <U> TreeSet<Tuple2<T, U>> zip(Iterable<? extends U> that) { Objects.requireNonNull(that, "that is null"); final Comparator<Tuple2<T, U>> tuple2Comparator = Tuple2.comparator(comparator(), Comparators.naturalComparator()); return TreeSet.ofAll(tuple2Comparator, iterator().zip(that)); }
public List<Set<Card>> dealHands( List<Card> shuffled, int hands, int cardsPerHand) { List<Set<Card>> list = List.empty(); for (int i = 0; i < hands; i++) { Tuple2<Set<Card>, ? extends List<Card>> tuple2 = this.deal(shuffled, cardsPerHand); shuffled = tuple2._2(); list = list.append(tuple2._1()); } return list; }
/** * Sets the 1st element of this tuple to the given {@code value}. * * @param value the new value * @return a copy of this tuple with a new value for the 1st element of this Tuple. */ public Tuple2<T1, T2> update1(T1 value) { return new Tuple2<>(value, _2); }
Optional<Loan> getLoan(final int loanId) { final Tuple2<Loan, Instant> result = callLocked(() -> cache.get().get(loanId)); if (result == null || isExpired(result)) { LOGGER.trace("Cache miss for loan #{}.", loanId); return Optional.empty(); } else { return Optional.of(result._1()); } }
@Override public R apply(T obj) { return pattern.apply(obj).apply(f); }