org.javatuples.Tuple Java Examples

The following examples show how to use org.javatuples.Tuple. 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: 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 #2
Source File: Context.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
<T extends Tuple, D> Assembler<T, D> tupleAssemblerOf(Class<? extends AggregateRoot<?>>[] aggregateRootTuple,
        Class<D> dto) {
    if (assemblerQualifierClass != null) {
        return assemblerRegistry.getTupleAssembler(aggregateRootTuple, dto, assemblerQualifierClass);
    } else if (assemblerQualifier != null) {
        return assemblerRegistry.getTupleAssembler(aggregateRootTuple, dto, assemblerQualifier);
    }
    return assemblerRegistry.getTupleAssembler(aggregateRootTuple, dto);
}
 
Example #3
Source File: AssembleSingleProviderImplTest.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testToDtoWithTuple() {
    Tuple tuple = Tuples.create(new Order("lightsaber"), new Customer("luke"));

    underTest = new AssembleSingleImpl<>(context, null, tuple);
    OrderDto orderDto = underTest.to(OrderDto.class);

    Assertions.assertThat(orderDto)
            .isNotNull();
    Assertions.assertThat(orderDto.getProduct())
            .isEqualTo("lightsaber");
    Assertions.assertThat(orderDto.getCustomerName())
            .isEqualTo("luke");
}
 
Example #4
Source File: TuplesTest.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testCreateTupleFromList() {
    List<?> classes = Lists.<Class<?>>newArrayList(String.class, Long.class);
    Tuple tuple = Tuples.create(classes);

    Assertions.assertThat((Iterable<?>) tuple)
            .isInstanceOf(Pair.class);
    Assertions.assertThat(tuple.containsAll(String.class, Long.class))
            .isTrue();
    Assertions.assertThat(tuple.getSize())
            .isEqualTo(2);
}
 
Example #5
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 #6
Source File: Tuples.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Builds a tuple from an {@link Iterable}, optionally limiting the number of items.
 *
 * @param objects the iterable of objects (size must be less or equal than 10).
 * @param limit   the item number limit (-1 means no limit).
 * @param <T>     the tuple type.
 * @return the constructed tuple.
 */
@SuppressWarnings("unchecked")
public static <T extends Tuple> T create(Iterable<?> objects, int limit) {
    List<Object> list = new ArrayList<>();
    int index = 0;
    for (Object object : objects) {
        if (limit != -1 && ++index > limit) {
            break;
        }
        list.add(object);
    }
    return create(list);
}
 
Example #7
Source File: Tuples.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Builds a tuple from an array of objects.
 *
 * @param objects the collection of objects (size must be less or equal than 10).
 * @param <T>     the tuple type.
 * @return the constructed tuple.
 */
@SuppressWarnings("unchecked")
public static <T extends Tuple> T create(Object... objects) {
    Class<? extends Tuple> tupleClass = classOfTuple(objects.length);
    try {
        return (T) tupleClass.getMethod("fromArray", Object[].class)
                .invoke(null, new Object[]{objects});
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw BusinessException.wrap(e, BusinessErrorCode.UNABLE_TO_CREATE_TUPLE);
    }
}
 
Example #8
Source File: Tuples.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Builds a tuple from a collection of objects.
 *
 * @param objects the collection of objects (size must be less or equal than 10).
 * @param <T>     the tuple type.
 * @return the constructed tuple.
 */
@SuppressWarnings("unchecked")
public static <T extends Tuple> T create(Collection<?> objects) {
    Class<? extends Tuple> tupleClass = classOfTuple(objects.size());
    try {
        return (T) tupleClass.getMethod("fromCollection", Collection.class)
                .invoke(null, objects);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw BusinessException.wrap(e, BusinessErrorCode.UNABLE_TO_CREATE_TUPLE);
    }
}
 
Example #9
Source File: ComputeWeightVertexProgram.java    From janusgraph_tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Execute this marvelous code, going from the Content to Users.
 *
 * Internally uses the RecommendationForNewUser class to build the recommendation as we want, pretty and
 * simple.
 *
 * @param vertex
 * @param messenger
 * @param memory
 */
@Override
public void execute(Vertex vertex, Messenger<Tuple> messenger, Memory memory) {
  try {
    HadoopQueryRunner runner = new HadoopQueryRunner(g, vertex.value(Schema.USER_NAME));
    GraphTraversal<Vertex, Edge> t = g.V(vertex.id()).inE(Schema.FOLLOWS);

    while(t.hasNext()) {
      Edge followsEdge = t.next();

      long commonFollowedUsers = runner.countCommonFollowedUsers(followsEdge.outVertex());
      long postsPerDaySince = runner.countPostsPerDaySince(sevenDaysAgo);
      long weight = (3 * commonFollowedUsers + postsPerDaySince) / 4;
      if(min == -10 || min > weight) {
        min = (int) weight;
      }
      if(max < weight) {
        max = (int) weight;
      }
      count++;

      followsEdge.property(CreateWeightIndex.WEIGHT, weight);
    }
  } catch (Exception e){
    e.printStackTrace();
    LOGGER.error("while processing " + vertex.id() + ": " + e.getClass().toString() + "(" + e.getMessage() + ")");
    return;
  }
}
 
Example #10
Source File: ComputeWeightVertexProgram.java    From janusgraph_tutorial with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"CloneDoesntDeclareCloneNotSupportedException", "CloneDoesntCallSuperClone"})
public VertexProgram<Tuple> clone() {
  LOGGER.info("clone");
  try {
    return (ComputeWeightVertexProgram) super.clone();
  } catch (final CloneNotSupportedException e) {
    throw new IllegalStateException(e.getMessage(), e);
  }
}
 
Example #11
Source File: HBaseVertex.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
public HBaseVertex(HBaseGraph graph, Object id, String label, Long createdAt, Long updatedAt,
                   Map<String, Object> properties, boolean propertiesFullyLoaded) {
    super(graph, id, label, createdAt, updatedAt, properties, propertiesFullyLoaded);

    if (graph != null) {
        this.edgeCache = CacheBuilder.<Tuple, List<Edge>>newBuilder()
                .maximumSize(graph.configuration().getRelationshipCacheMaxSize())
                .expireAfterAccess(graph.configuration().getRelationshipCacheTtlSecs(), TimeUnit.SECONDS)
                .build();
    }
}
 
Example #12
Source File: FluentAssemblerImpl.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple> AssemblePageWithQualifier assembleTuples(Page<T> page) {
    return new AssemblePageImpl<>(context, null, page);
}
 
Example #13
Source File: HBaseVertex.java    From hgraphdb with Apache License 2.0 4 votes vote down vote up
public Iterator<Edge> getEdgesFromCache(Tuple cacheKey) {
    if (edgeCache == null || !isCached()) return null;
    List<Edge> edges = edgeCache.getIfPresent(cacheKey);
    return edges != null ? IteratorUtils.filter(edges.iterator(), edge -> !((HBaseEdge) edge).isDeleted()) : null;
}
 
Example #14
Source File: HBaseVertex.java    From hgraphdb with Apache License 2.0 4 votes vote down vote up
public void cacheEdges(Tuple cacheKey, List<Edge> edges) {
    if (edgeCache == null || !isCached()) return;
    edgeCache.put(cacheKey, edges);
}
 
Example #15
Source File: CloneVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(final Vertex sourceVertex, final Messenger<Tuple> messenger, final Memory memory) {
}
 
Example #16
Source File: CloneVertexProgram.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"CloneDoesntDeclareCloneNotSupportedException", "CloneDoesntCallSuperClone"})
@Override
public VertexProgram<Tuple> clone() {
    return this;
}
 
Example #17
Source File: FluentAssemblerAdapter.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple> AssembleSingleWithQualifier assembleTuple(
        T tuple) {
    return fluentAssembler.assembleTuple(tuple);
}
 
Example #18
Source File: AssemblerRegistryImpl.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple, D> Assembler<T, D> getTupleAssembler(
        Class<? extends AggregateRoot<?>>[] aggregateRootClasses, Class<D> dtoClass,
        @Nullable Class<? extends Annotation> qualifier) {
    return findAssemblerOf(classesToTupleType(aggregateRootClasses), dtoClass, null, qualifier);
}
 
Example #19
Source File: AssemblerRegistryImpl.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple, D> Assembler<T, D> getTupleAssembler(
        Class<? extends AggregateRoot<?>>[] aggregateRootClasses, Class<D> dtoClass, Annotation qualifier) {
    return findAssemblerOf(classesToTupleType(aggregateRootClasses), dtoClass, qualifier, null);
}
 
Example #20
Source File: AssemblerRegistryImpl.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple, D> Assembler<T, D> getTupleAssembler(
        Class<? extends AggregateRoot<?>>[] aggregateRootClasses, Class<D> dtoClass) {
    return findAssemblerOf(classesToTupleType(aggregateRootClasses), dtoClass, null, null);
}
 
Example #21
Source File: FluentAssemblerAdapter.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple> AssembleMultipleWithQualifier assembleTuples(
        Iterable<T> iterable) {
    return fluentAssembler.assembleTuples(iterable);
}
 
Example #22
Source File: FluentAssemblerImpl.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple> AssembleMultipleWithQualifier assembleTuples(Iterable<T> iterable) {
    return new AssembleMultipleImpl<>(context, null, StreamSupport.stream(iterable.spliterator(), false));
}
 
Example #23
Source File: FluentAssemblerImpl.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple> AssembleMultipleWithQualifier assembleTuples(Stream<T> stream) {
    return new AssembleMultipleImpl<>(context, null, stream);
}
 
Example #24
Source File: FluentAssemblerImpl.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple> AssembleSingleWithQualifier assembleTuple(T tuple) {
    return new AssembleSingleImpl<>(context, null, tuple);
}
 
Example #25
Source File: Context.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
<D, T extends Tuple> void mergeDtoIntoTuple(D dto, T tuple, Class<? extends AggregateRoot<?>>[] aggregateClasses) {
    Assembler<Tuple, D> tupleAssembler = tupleAssemblerOf(aggregateClasses, (Class<D>) dto.getClass());
    tupleAssembler.mergeDtoIntoAggregate(dto, tuple);
}
 
Example #26
Source File: FluentAssemblerAdapter.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple> AssembleMultipleWithQualifier assembleTuples(
        Stream<T> stream) {
    return fluentAssembler.assembleTuples(stream);
}
 
Example #27
Source File: FluentAssemblerAdapter.java    From business with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public <T extends Tuple> AssemblePageWithQualifier assembleTuples(
        Page<T> page) {
    return fluentAssembler.assembleTuples(page);
}
 
Example #28
Source File: AssemblerRegistry.java    From business with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns the Assembler matching the given list of aggregate root classes and the dto class for
 * the specified qualifier.
 *
 * @param <T>                  the type of the tuple.
 * @param <D>                  the type of the DTO.
 * @param aggregateRootClasses an array of aggregate root classes.
 * @param dtoClass             the dto class.
 * @param qualifier            the assembler qualifier.
 * @return an assembler instance.
 */
<T extends Tuple, D> Assembler<T, D> getTupleAssembler(Class<? extends AggregateRoot<?>>[] aggregateRootClasses,
        Class<D> dtoClass, @Nullable Class<? extends Annotation> qualifier);
 
Example #29
Source File: AssemblerRegistry.java    From business with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns the Assembler matching the given list of aggregate root classes and the dto class for
 * the specified qualifier.
 *
 * @param <T>                  the type of the tuple.
 * @param <D>                  the type of the DTO.
 * @param aggregateRootClasses an array of aggregate root classes.
 * @param dtoClass             the dto class.
 * @param qualifier            the assembler qualifier.
 * @return an assembler instance.
 */
<T extends Tuple, D> Assembler<T, D> getTupleAssembler(Class<? extends AggregateRoot<?>>[] aggregateRootClasses,
        Class<D> dtoClass, @Nullable Annotation qualifier);
 
Example #30
Source File: AssemblerRegistry.java    From business with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Returns the Assembler matching the given list of aggregate root classes and the dto class.
 *
 * @param <T>                  the type of the tuple.
 * @param <D>                  the type of the DTO.
 * @param aggregateRootClasses an array of aggregate root classes.
 * @param dtoClass             the dto class.
 * @return an assembler instance.
 */
<T extends Tuple, D> Assembler<T, D> getTupleAssembler(Class<? extends AggregateRoot<?>>[] aggregateRootClasses,
        Class<D> dtoClass);