private String describeTypeMismatches(Object[] values) { return getIndices() .filter(i -> !slots[i].accepts(values[i])) .mapToObj(i -> String.format("Slot (%s) does not accept value <%s>", slots[i], values[i])) .collect(joining(", ")); }
String format(Object[] values) { return name + getIndices() .mapToObj(i -> slots[i].getName() + "=" + values[i]) .collect(joining(", ", "{", "}")); }
<V> Map<String, V> serialise(Function<Object, V> serialiser, Object[] values) { return getIndices() .collect(LinkedHashMap::new, (m, i) -> m.put(slots[i].getName(), serialiser.apply(values[i])), Map::putAll); }
Map<String, Object> toMap(Object[] values) { return getIndices() .collect(LinkedHashMap::new, (m, i) -> m.put(slots[i].getName(), values[i]), Map::putAll); }
/** * Create a tuple from a map of name/value pairs. * @param values The values to put in the tuple. * @return The created tuple. */ public Tuple make(Map<String, Object> values) { if (values == null) throw new IllegalArgumentException("values must not be null"); checkMatchingKeys(values); Object[] valueArray = new Object[slots.length]; getIndices().forEach(i -> valueArray[i] = values.get(slots[i].getName())); return make(valueArray); }
/** * Create a tuple using the supplied deserialiser, out of a map of serialised values. * @param deserialiser The deserialiser to use to deserialise values from the map. * @param values A map of serialised tuple values. * @param <V> The type to which tuple value have been serialised, e.g. String. * @return The created tuple. */ public <V> Tuple deserialise(BiFunction<V, Type, Object> deserialiser, Map<String, V> values) { if (deserialiser == null) throw new IllegalArgumentException("deserialiser must not be null"); if (values == null) throw new IllegalArgumentException("values must not be null"); checkMatchingKeys(values); Object[] valueArray = new Object[slots.length]; getIndices().forEach(i -> valueArray[i] = slots[i].deserialise(deserialiser, values)); return make(valueArray); }
private boolean typesMatch(Object[] values) { return getIndices().allMatch(i -> slots[i].accepts(values[i])); }