io.permazen.encoding.Concat2Encoding Maven / Gradle / Ivy
Show all versions of permazen-encoding Show documentation
/*
* Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
*/
package io.permazen.encoding;
import com.google.common.base.Converter;
import com.google.common.base.Preconditions;
import io.permazen.tuple.Tuple2;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* Support superclass for non-null {@link Encoding}s of values that can be decomposed into two component values.
*
*
* Null values are not supported by this class and there is no default value.
*
* @param this encoding's value type
* @param first tuple value type
* @param second tuple value type
*/
public abstract class Concat2Encoding extends ConvertedEncoding> {
private static final long serialVersionUID = -7395218884659436173L;
/**
* Constructor.
*
* @param type Java type for this encoding's values
* @param encoding1 first value encoding
* @param encoding2 second value encoding
* @param splitter1 first value splitter
* @param splitter2 second value splitter
* @param joiner value joiner from tuple
* @throws IllegalArgumentException if any parameter is null
*/
protected Concat2Encoding(Class type,
Encoding encoding1, Encoding encoding2,
Function super T, ? extends V1> splitter1, Function super T, ? extends V2> splitter2,
BiFunction super V1, ? super V2, ? extends T> joiner) {
super(null, type, new Tuple2Encoding<>(encoding1, encoding2),
Converter.from(
value -> new Tuple2<>(splitter1.apply(value), splitter2.apply(value)),
tuple -> joiner.apply(tuple.getValue1(), tuple.getValue2())));
Preconditions.checkArgument(splitter1 != null, "null splitter1");
Preconditions.checkArgument(splitter2 != null, "null splitter2");
Preconditions.checkArgument(joiner != null, "null joiner");
}
@SuppressWarnings("unchecked")
public Tuple2Encoding getTuple2Encoding() {
return (Tuple2Encoding)this.delegate;
}
}