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) 2020 ActiveJ 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.activej.codec;
import io.activej.common.api.DecoderFunction;
import io.activej.common.exception.MalformedDataException;
import io.activej.common.exception.UncheckedException;
import io.activej.common.tuple.*;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Type;
import java.util.*;
import java.util.function.Function;
import static io.activej.common.Checks.checkArgument;
import static io.activej.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 MalformedDataException {
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 MalformedDataException {
String v = in.readString();
if (v.length() == 1) {
return v.charAt(0);
}
throw new MalformedDataException("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 MalformedDataException {
int v = in.readInt();
if (v >= 0 && v <= 0xFF) {
return (byte) v;
}
throw new MalformedDataException("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 MalformedDataException {
int v = in.readInt();
if (v >= Short.MIN_VALUE && v <= Short.MAX_VALUE) {
return (short) v;
}
throw new MalformedDataException("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 MalformedDataException {
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 MalformedDataException {
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 MalformedDataException {
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 MalformedDataException {
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 MalformedDataException {
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 MalformedDataException {
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 MalformedDataException {
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 MalformedDataException {
return in.readBytes();
}
};
public static final StructuredCodec VOID_CODEC = new StructuredCodec() {
@Override
public Void decode(StructuredInput in) throws MalformedDataException {
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 MalformedDataException {
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 MalformedDataException {
try {
return Class.forName(in.readString());
} catch (ClassNotFoundException e) {
throw new MalformedDataException(e);
}
}
};
@SuppressWarnings("rawtypes")
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 MalformedDataException {
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 void encode(StructuredOutput out, Optional item) {
out.writeNullable(codec, item.orElse(null));
}
@Override
public Optional decode(StructuredInput in) throws MalformedDataException {
return Optional.ofNullable(in.readNullable(codec));
}
};
}
public static StructuredCodec<@Nullable T> ofNullable(StructuredCodec codec) {
return new StructuredCodec() {
@Override
public void encode(StructuredOutput out, T item) {
out.writeNullable(codec, item);
}
@Nullable
@Override
public T decode(StructuredInput in) throws MalformedDataException {
return in.readNullable(codec);
}
};
}
public static StructuredCodec transform(StructuredCodec codec, DecoderFunction reader, Function writer) {
return new StructuredCodec() {
@Override
public void encode(StructuredOutput out, R value) {
T result = writer.apply(value);
codec.encode(out, result);
}
@Override
public R decode(StructuredInput in) throws MalformedDataException {
T result = codec.decode(in);
try {
return reader.decode(result);
} catch (UncheckedException u) {
throw u.propagate(MalformedDataException.class);
}
}
};
}
/**
* 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 void encode(StructuredOutput out, List item) {
out.writeList(valueAdapters, item);
}
@Override
public List decode(StructuredInput in) throws MalformedDataException {
return in.readList(valueAdapters);
}
};
}
/**
* 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