eu.mais_h.mathsync.serialize.StringSerializer Maven / Gradle / Ivy
The newest version!
package eu.mais_h.mathsync.serialize;
import java.io.UnsupportedEncodingException;
import eu.mais_h.mathsync.util.Function;
/**
* Serializer going through a string representation of items.
*
* {@link StringDeserializer} should be used as the corresponding {@link Deserializer}.
*/
public class StringSerializer implements Serializer {
private final Function toString;
private StringSerializer(Function toString) {
this.toString = toString;
}
@Override
public byte[] serialize(T item) {
String stringified = toString.apply(item);
byte[] result;
try {
result = stringified.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError("JVM does not support UTF-8 encoding");
}
return result;
}
/**
* Retrieves an instance of this serializer.
*
* @param toString the function to convert items to string as an intermediate representation.
* @return an instance of this serializer kind.
*/
public static Serializer create(Function toString) {
return new StringSerializer<>(toString);
}
/**
* Retrieves an instance of this serializer directly serializing strings.
*
* @return an instance of this serializer kind.
*/
public static Serializer create() {
return create(new Function() {
@Override
public String apply(String t) {
return t;
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy