Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2015-2018 SoftIndex LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.datakernel.codec;
import io.datakernel.common.parse.ParseException;
import io.datakernel.common.tuple.*;
import java.lang.reflect.Type;
import java.util.*;
import java.util.function.Function;
import static io.datakernel.common.Preconditions.checkArgument;
import static io.datakernel.common.collection.CollectionUtils.map;
import static java.util.Arrays.asList;
import static java.util.Collections.*;
/**
* This class contains various primitive {@link StructuredCodec StructuredCodecs} and their combinators.
*/
@SuppressWarnings("unchecked")
public final class StructuredCodecs {
public static final StructuredCodec BOOLEAN_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Boolean value) {
out.writeBoolean(value);
}
@Override
public Boolean decode(StructuredInput in) throws ParseException {
return in.readBoolean();
}
};
public static final StructuredCodec CHARACTER_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Character value) {
out.writeString(value + "");
}
@Override
public Character decode(StructuredInput in) throws ParseException {
String v = in.readString();
if (v.length() == 1) {
return v.charAt(0);
}
throw new ParseException("Read a string with length != 1 while trying to read a character");
}
};
public static final StructuredCodec BYTE_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Byte value) {
out.writeInt(value & 0xFF);
}
@Override
public Byte decode(StructuredInput in) throws ParseException {
int v = in.readInt();
if (v >= 0 && v <= 0xFF) {
return (byte) v;
}
throw new ParseException("Read an int not in range [0, 255] while trying to read a byte");
}
};
public static final StructuredCodec SHORT_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Short value) {
out.writeInt(value);
}
@Override
public Short decode(StructuredInput in) throws ParseException {
int v = in.readInt();
if (v >= Short.MIN_VALUE && v <= Short.MAX_VALUE) {
return (short) v;
}
throw new ParseException("Read an int not in range [" + Short.MIN_VALUE + ", " + Short.MAX_VALUE + "] while trying to read a short");
}
};
public static final StructuredCodec INT_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Integer value) {
out.writeInt(value);
}
@Override
public Integer decode(StructuredInput in) throws ParseException {
return in.readInt();
}
};
public static final StructuredCodec LONG_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Long value) {
out.writeLong(value);
}
@Override
public Long decode(StructuredInput in) throws ParseException {
return in.readLong();
}
};
public static final StructuredCodec INT32_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Integer value) {
out.writeInt32(value);
}
@Override
public Integer decode(StructuredInput in) throws ParseException {
return in.readInt32();
}
};
public static final StructuredCodec LONG64_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Long value) {
out.writeLong64(value);
}
@Override
public Long decode(StructuredInput in) throws ParseException {
return in.readLong64();
}
};
public static final StructuredCodec FLOAT_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Float value) {
out.writeFloat(value);
}
@Override
public Float decode(StructuredInput in) throws ParseException {
return in.readFloat();
}
};
public static final StructuredCodec DOUBLE_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, Double value) {
out.writeDouble(value);
}
@Override
public Double decode(StructuredInput in) throws ParseException {
return in.readDouble();
}
};
public static final StructuredCodec STRING_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, String value) {
out.writeString(value);
}
@Override
public String decode(StructuredInput in) throws ParseException {
return in.readString();
}
};
public static final StructuredCodec BYTES_CODEC = new StructuredCodec() {
@Override
public void encode(StructuredOutput out, byte[] value) {
out.writeBytes(value);
}
@Override
public byte[] decode(StructuredInput in) throws ParseException {
return in.readBytes();
}
};
public static final StructuredCodec VOID_CODEC = new StructuredCodec() {
@Override
public Void decode(StructuredInput in) throws ParseException {
in.readNull();
return null;
}
@Override
public void encode(StructuredOutput out, Void item) {
out.writeNull();
}
};
public static > StructuredCodec ofEnum(Class enumType) {
return new StructuredCodec() {
@Override
public void encode(StructuredOutput out, E value) {
out.writeString(value.name());
}
@Override
public E decode(StructuredInput in) throws ParseException {
return Enum.valueOf(enumType, in.readString());
}
};
}
public static final StructuredCodec> CLASS_CODEC = new StructuredCodec>() {
@Override
public void encode(StructuredOutput out, Class> value) {
out.writeString(value.getName());
}
@Override
public Class> decode(StructuredInput in) throws ParseException {
try {
return Class.forName(in.readString());
} catch (ClassNotFoundException e) {
throw new ParseException(e);
}
}
};
public static StructuredCodec> ofClass() {
return (StructuredCodec) CLASS_CODEC;
}
static StructuredCodec ofCustomType(Class type) {
return ofCustomType((Type) type);
}
static StructuredCodec ofCustomType(Type type) {
return new StructuredCodec() {
@Override
public void encode(StructuredOutput out, T item) {
out.writeCustom(type, item);
}
@Override
public T decode(StructuredInput in) throws ParseException {
return in.readCustom(type);
}
};
}
/**
* Combinator codec that writes/reads Optional<T> as nullable T with given codec for T
*/
public static StructuredCodec> ofOptional(StructuredCodec codec) {
return new StructuredCodec>() {
@Override
public Optional decode(StructuredInput in) throws ParseException {
return Optional.ofNullable(in.readNullable(codec));
}
@Override
public void encode(StructuredOutput out, Optional item) {
out.writeNullable(codec, item.orElse(null));
}
};
}
/**
* Combinator codec that writes/reads a list of T with given codec for T
*/
public static StructuredCodec> ofList(StructuredCodec valueAdapters) {
return new StructuredCodec>() {
@Override
public List decode(StructuredInput in) throws ParseException {
return in.readList(valueAdapters);
}
@Override
public void encode(StructuredOutput out, List item) {
out.writeList(valueAdapters, item);
}
};
}
/**
* Combinator codec that writes/reads a set of T with given codec for T
*/
public static StructuredCodec> ofSet(StructuredCodec codec) {
return ofList(codec)
.transform(LinkedHashSet::new, ArrayList::new);
}
/**
* Combinator codec that writes/reads a heterogeneous fixed-size array ob objects with given codecs
*/
public static StructuredCodec