org.javatuples.Triplet Java Examples

The following examples show how to use org.javatuples.Triplet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaTuplesUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenAddNewElement_thenCreateNewTuple() {
    Pair<String, Integer> pair1 = Pair.with("john", 32);
    Triplet<String, Integer, String> triplet1 = pair1.add("1051 SW");
    assertThat(triplet1.contains("john"));
    assertThat(triplet1.contains(32));
    assertThat(triplet1.contains("1051 SW"));

    Pair<String, Integer> pair2 = Pair.with("alex", 45);
    Quartet<String, Integer, String, Integer> quartet2 = pair1.add(pair2);
    assertThat(quartet2.containsAll(pair1));
    assertThat(quartet2.containsAll(pair2));

    Quartet<String, Integer, String, Integer> quartet1 = pair1.add("alex", 45);
    assertThat(quartet1.containsAll("alex", "john", 32, 45));

    Triplet<String, String, Integer> triplet2 = pair1.addAt1("1051 SW");
    assertThat(triplet2.indexOf("john")).isEqualTo(0);
    assertThat(triplet2.indexOf("1051 SW")).isEqualTo(1);
    assertThat(triplet2.indexOf(32)).isEqualTo(2);

    Unit<Integer> unit = pair1.removeFrom0();
    assertThat(unit.contains(32));
}
 
Example #2
Source File: TuplesTest.java    From business with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testCreateTupleFromVarArgs() {

    Pair<Integer, String> pair = Tuples.create(10, "foo");
    Assertions.assertThat((Iterable<?>) pair)
            .isEqualTo(new Pair<>(10, "foo"));

    Tuple tuple = Tuples.create(String.class, Long.class);
    Assertions.assertThat((Iterable<?>) tuple)
            .isInstanceOf(Pair.class);
    Assertions.assertThat(tuple.containsAll(String.class, Long.class))
            .isTrue();
    Assertions.assertThat(tuple.getSize())
            .isEqualTo(2);

    tuple = Tuples.create(String.class, Long.class, Float.class);
    Assertions.assertThat((Iterable<?>) tuple)
            .isInstanceOf(Triplet.class);
}
 
Example #3
Source File: ShortestPathVertexProgram.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
private void processEdges(final Vertex vertex, final Path currentPath, final Number currentDistance,
                          final Messenger<Triplet<Path, Edge, Number>> messenger) {

    final Traversal.Admin<Vertex, Edge> edgeTraversal = this.edgeTraversal.getPure();
    edgeTraversal.addStart(edgeTraversal.getTraverserGenerator().generate(vertex, edgeTraversal.getStartStep(), 1));

    while (edgeTraversal.hasNext()) {
        final Edge edge = edgeTraversal.next();
        final Number distance = getDistance(edge);

        Vertex otherV = edge.inVertex();
        if (otherV.equals(vertex))
            otherV = edge.outVertex();

        // only send message if the adjacent vertex is not yet part of the current path
        if (!currentPath.objects().contains(otherV)) {
            messenger.sendMessage(MessageScope.Global.of(otherV),
                    Triplet.with(currentPath, this.includeEdges ? edge : null,
                            NumberHelper.add(currentDistance, distance)));
        }
    }
}
 
Example #4
Source File: SideEffectStrategy.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public static <A> void addSideEffect(final TraversalStrategies traversalStrategies, final String key, final A value, final BinaryOperator<A> reducer) {
    SideEffectStrategy strategy = traversalStrategies.getStrategy(SideEffectStrategy.class).orElse(null);
    if (null == strategy) {
        strategy = new SideEffectStrategy();
        traversalStrategies.addStrategies(strategy);
    } else {
        final SideEffectStrategy cloneStrategy = new SideEffectStrategy();
        cloneStrategy.sideEffects.addAll(strategy.sideEffects);
        strategy = cloneStrategy;
        traversalStrategies.addStrategies(strategy);
    }
    strategy.sideEffects.add(new Triplet<>(key, null == value ? null : value instanceof Supplier ? (Supplier) value : new ConstantSupplier<>(value), reducer));
}
 
Example #5
Source File: JavaTuplesUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@SuppressWarnings("unused")
@Test
public void whenCreatingTuples_thenCreateTuples() {
    Pair<String, Integer> pair = new Pair<String, Integer>("This is a pair", 55);
    Triplet<String, Integer, Double> triplet = Triplet.with("hello", 23, 33.2);

    List<String> collectionOfNames = Arrays.asList("john", "doe", "anne", "alex");
    Quartet<String, String, String, String> quartet = Quartet.fromCollection(collectionOfNames);

    Pair<String, String> pairFromList = Pair.fromIterable(collectionOfNames, 2);

    String[] names = new String[] { "john", "doe", "anne" };
    Triplet<String, String, String> triplet2 = Triplet.fromArray(names);
}
 
Example #6
Source File: Tuples.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the tuple class corresponding to the specified cardinality.
 *
 * @param cardinality the cardinality (must be less or equal than 10).
 * @return the corresponding tuple class.
 */
public static Class<? extends Tuple> classOfTuple(int cardinality) {
    switch (cardinality) {
        case 1:
            return Unit.class;
        case 2:
            return Pair.class;
        case 3:
            return Triplet.class;
        case 4:
            return Quartet.class;
        case 5:
            return Quintet.class;
        case 6:
            return Sextet.class;
        case 7:
            return Septet.class;
        case 8:
            return Octet.class;
        case 9:
            return Ennead.class;
        case 10:
            return Decade.class;
        default:
            throw new IllegalArgumentException("Cannot create a tuple with " + cardinality + " element(s)");
    }
}
 
Example #7
Source File: MergeMultipleImpl.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public <A0 extends AggregateRoot<?>,
        A1 extends AggregateRoot<?>,
        A2 extends AggregateRoot<?>> MergeFromRepository<MergeAs<Triplet<A0, A1, A2>>> into(Class<A0> first,
        Class<A1> second, Class<A2> third) {
    return new MergeMultipleTuplesFromRepositoryImpl<>(context, dtoStream, first, second, third);
}
 
Example #8
Source File: MergeSingleImpl.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public <A0 extends AggregateRoot<?>,
        A1 extends AggregateRoot<?>,
        A2 extends AggregateRoot<?>> MergeFromRepository<Triplet<A0, A1, A2>> into(Class<A0> first,
        Class<A1> second, Class<A2> third) {
    return new MergeSingleTupleFromRepositoryImpl<>(context, dto, first, second, third);
}
 
Example #9
Source File: GraphSONSerializersV3d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public TraversalExplanation deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final Map<String, Object> explainData = deserializationContext.readValue(jsonParser, Map.class);
    final String originalTraversal = explainData.get(GraphSONTokens.ORIGINAL).toString();
    final List<Triplet<String, String, String>> intermediates = new ArrayList<>();
    final List<Map<String,Object>> listMap = (List<Map<String,Object>>) explainData.get(GraphSONTokens.INTERMEDIATE);
    for (Map<String,Object> m : listMap) {
        intermediates.add(Triplet.with(m.get(GraphSONTokens.STRATEGY).toString(),
                m.get(GraphSONTokens.CATEGORY).toString(),
                m.get(GraphSONTokens.TRAVERSAL).toString()));
    }

    return new ImmutableExplanation(originalTraversal, intermediates);
}
 
Example #10
Source File: GraphSONSerializersV2d0.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public TraversalExplanation deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    final Map<String, Object> explainData = deserializationContext.readValue(jsonParser, Map.class);
    final String originalTraversal = explainData.get(GraphSONTokens.ORIGINAL).toString();
    final List<Triplet<String, String, String>> intermediates = new ArrayList<>();
    final List<Map<String,Object>> listMap = (List<Map<String,Object>>) explainData.get(GraphSONTokens.INTERMEDIATE);
    for (Map<String,Object> m : listMap) {
        intermediates.add(Triplet.with(m.get(GraphSONTokens.STRATEGY).toString(),
                m.get(GraphSONTokens.CATEGORY).toString(),
                m.get(GraphSONTokens.TRAVERSAL).toString()));
    }

    return new ImmutableExplanation(originalTraversal, intermediates);
}
 
Example #11
Source File: MergeMultiple.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
<A0 extends AggregateRoot<?>,
A1 extends AggregateRoot<?>,
A2 extends AggregateRoot<?>> MergeFromRepository<MergeAs<Triplet<A0, A1, A2>>> into(Class<A0> first,
Class<A1> second, Class<A2> third);
 
Example #12
Source File: MainActivity.java    From Reactive-Android-Programming with MIT License 4 votes vote down vote up
private void demo4() {
    Observable.just("UserID1", "UserID2", "UserID3")
            .map(id -> Triplet.with(id, id + "-access-token", "third-value"))
            .subscribe(triplet -> log(triplet.getValue0(), triplet.getValue1() + triplet.getValue2()));
}
 
Example #13
Source File: UtilSerializers.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final Triplet triplet) {
    kryo.writeClassAndObject(output, triplet.getValue0());
    kryo.writeClassAndObject(output, triplet.getValue1());
    kryo.writeClassAndObject(output, triplet.getValue2());
}
 
Example #14
Source File: TuplesTest.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Test
public void testClassOfTuple() {
    Class<?> type = Tuples.classOfTuple(String.class);
    Assertions.assertThat(type)
            .isEqualTo(Unit.class);

    type = Tuples.classOfTuple(String.class, Integer.class);
    Assertions.assertThat(type)
            .isEqualTo(Pair.class);

    type = Tuples.classOfTuple(String.class, Integer.class, Long.class);
    Assertions.assertThat(type)
            .isEqualTo(Triplet.class);

    type = Tuples.classOfTuple(String.class, Integer.class, Long.class, Float.class);
    Assertions.assertThat(type)
            .isEqualTo(Quartet.class);

    type = Tuples.classOfTuple(String.class, Integer.class, Long.class, Float.class, Boolean.class);
    Assertions.assertThat(type)
            .isEqualTo(Quintet.class);

    type = Tuples.classOfTuple(String.class, Integer.class, Long.class, Float.class, Boolean.class, Byte.class);
    Assertions.assertThat(type)
            .isEqualTo(Sextet.class);

    type = Tuples.classOfTuple(String.class, Integer.class, Long.class, Float.class, Boolean.class, Byte.class,
            Short.class);
    Assertions.assertThat(type)
            .isEqualTo(Septet.class);

    type = Tuples.classOfTuple(String.class, Integer.class, Long.class, Float.class, Boolean.class, Byte.class,
            Short.class, Double.class);
    Assertions.assertThat(type)
            .isEqualTo(Octet.class);

    type = Tuples.classOfTuple(String.class, Integer.class, Long.class, Float.class, Boolean.class, Byte.class,
            Short.class, Double.class, Number.class);
    Assertions.assertThat(type)
            .isEqualTo(Ennead.class);

    type = Tuples.classOfTuple(String.class, Integer.class, Long.class, Float.class, Boolean.class, Byte.class,
            Short.class, Double.class, Number.class, Character.class);
    Assertions.assertThat(type)
            .isEqualTo(Decade.class);
}
 
Example #15
Source File: UtilSerializers.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <I extends InputShim> Triplet read(final KryoShim<I, ?> kryo, final I input, final Class<Triplet> tripletClass) {
    return Triplet.with(kryo.readClassAndObject(input), kryo.readClassAndObject(input), kryo.readClassAndObject(input));
}
 
Example #16
Source File: MergeSingleImpl.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <A0 extends AggregateRoot<?>,
        A1 extends AggregateRoot<?>,
        A2 extends AggregateRoot<?>> void into(A0 first, A1 second, A2 third) {
    into(Triplet.with(first, second, third));
}
 
Example #17
Source File: MergeSingleImpl.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <A0 extends AggregateRoot<?>, A1 extends AggregateRoot<?>,
        A2 extends AggregateRoot<?>> void into(Triplet<A0, A1, A2> triplet) {
    context.tupleAssemblerOf(Tuples.itemClasses(triplet), dtoClass)
            .mergeDtoIntoAggregate(dto, triplet);
}
 
Example #18
Source File: TraversalExplanation.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Stream<Triplet<String, String, String>> getIntermediates() {
    return this.strategyTraversals.stream().map( p -> Triplet.with(p.getValue0().toString(),
            p.getValue0().getTraversalCategory().getSimpleName(), p.getValue1().toString()));
}
 
Example #19
Source File: ImmutableExplanation.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Stream<String> getStrategyTraversalsAsString() {
    return getIntermediates().map(Triplet::getValue0);
}
 
Example #20
Source File: MergeSingle.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
<A0 extends AggregateRoot<?>,
A1 extends AggregateRoot<?>,
A2 extends AggregateRoot<?>> void into(Triplet<A0, A1, A2> triplet);
 
Example #21
Source File: MergeSingle.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
<A0 extends AggregateRoot<?>,
A1 extends AggregateRoot<?>,
A2 extends AggregateRoot<?>> MergeFromRepository<Triplet<A0, A1, A2>> into(Class<A0> first,
Class<A1> second, Class<A2> third);
 
Example #22
Source File: ImmutableExplanation.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
public ImmutableExplanation(final String originalTraversal,
                            final List<Triplet<String, String, String>> intermediates) {
    this.originalTraversal = originalTraversal;
    this.intermediates = intermediates;
}
 
Example #23
Source File: ChainableWait.java    From NoraUi with GNU Affero General Public License v3.0 4 votes vote down vote up
public <A, B, C, O> ChainableWait<O> wait(TriFunction<A, B, C, ExpectedCondition<O>> condition, Function<T, Triplet<A, B, C>> func) {
    Triplet<A, B, C> triplet = func.apply(chainedValue);
    return new ChainableWait<>(webDriverWait, webDriverWait.until(condition.apply(triplet.getValue0(), triplet.getValue1(), triplet.getValue2())));
}
 
Example #24
Source File: AbstractExplanation.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
/**
 * A pretty-print representation of the traversal explanation.
 *
 * @return a {@link String} representation of the traversal explanation
 */
public String prettyPrint(final int maxLineLength) {
    final String originalTraversal = "Original Traversal";
    final String finalTraversal = "Final Traversal";
    final int maxStrategyColumnLength = getStrategyTraversalsAsString()
            .map(String::length)
            .max(Comparator.naturalOrder())
            .orElse(15);
    final int newLineIndent = maxStrategyColumnLength + 10;
    final int maxTraversalColumn = maxLineLength - newLineIndent;
    if (maxTraversalColumn < 1)
        throw new IllegalArgumentException("The maximum line length is too small to present the " + TraversalExplanation.class.getSimpleName() + ": " + maxLineLength);
    int largestTraversalColumn = getTraversalStepsAsString()
            .map(s -> wordWrap(s, maxTraversalColumn, newLineIndent))
            .flatMap(s -> Stream.of(s.split("\n")))
            .map(String::trim)
            .map(s -> s.trim().startsWith("[") ? s : "   " + s) // 3 indent on new lines
            .map(String::length)
            .max(Comparator.naturalOrder())
            .get();

    final StringBuilder builder = new StringBuilder("Traversal Explanation\n");
    for (int i = 0; i < (maxStrategyColumnLength + 7 + largestTraversalColumn); i++) {
        builder.append("=");
    }

    spacing(originalTraversal, maxStrategyColumnLength, builder);

    builder.append(wordWrap(getOriginalTraversalAsString(), maxTraversalColumn, newLineIndent));
    builder.append("\n\n");

    final List<Triplet<String,String,String>> intermediates = this.getIntermediates().collect(Collectors.toList());
    for (Triplet<String,String,String> t : intermediates) {
        builder.append(t.getValue0());
        int spacesToAdd = maxStrategyColumnLength - t.getValue0().length() + 1;
        for (int i = 0; i < spacesToAdd; i++) {
            builder.append(" ");
        }
        builder.append("[").append(t.getValue1().substring(0, 1)).append("]");
        for (int i = 0; i < 3; i++) {
            builder.append(" ");
        }
        builder.append(wordWrap(t.getValue2(), maxTraversalColumn, newLineIndent)).append("\n");
    }

    spacing(finalTraversal, maxStrategyColumnLength, builder);

    builder.append(wordWrap((intermediates.size() > 0 ?
            intermediates.get(intermediates.size() - 1).getValue2() :
            getOriginalTraversalAsString()), maxTraversalColumn, newLineIndent));
    return builder.toString();
}
 
Example #25
Source File: AbstractExplanation.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
protected Stream<String> getTraversalStepsAsString() {
    return Stream.concat(Stream.of(this.getOriginalTraversalAsString()), getIntermediates().map(Triplet::getValue2));
}
 
Example #26
Source File: ImmutableExplanation.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Stream<Triplet<String, String, String>> getIntermediates() {
    return intermediates.stream();
}
 
Example #27
Source File: ImmutableExplanation.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
protected Stream<String> getTraversalStepsAsString() {
    return Stream.concat(Stream.of(this.originalTraversal), getIntermediates().map(Triplet::getValue2));
}
 
Example #28
Source File: AbstractExplanation.java    From tinkerpop with Apache License 2.0 2 votes vote down vote up
/**
 * First string is the traversal strategy, the second is the category and the third is the traversal
 * representation at that point.
 */
protected abstract Stream<Triplet<String,String,String>> getIntermediates();