io.scalecube.services.transport.api.DataCodec Maven / Gradle / Ivy
package io.scalecube.services.transport.api;
import io.scalecube.utils.ServiceLoaderUtil;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public interface DataCodec {
Map INSTANCES =
ServiceLoaderUtil.findAll(DataCodec.class)
.collect(Collectors.toMap(DataCodec::contentType, Function.identity()));
static Collection getAllInstances() {
return INSTANCES.values();
}
static Set getAllContentTypes() {
return getAllInstances().stream().map(DataCodec::contentType).collect(Collectors.toSet());
}
/**
* Returns {@link DataCodec} by given {@code contentType}.
*
* @param contentType contentType (required)
* @return {@link DataCodec} by given {@code contentType} (or throws IllegalArgumentException is
* thrown if not exist)
*/
static DataCodec getInstance(String contentType) {
Objects.requireNonNull(contentType, "[getInstance] contentType");
DataCodec dataCodec = INSTANCES.get(contentType);
Objects.requireNonNull(
dataCodec, "[getInstance] dataCodec not found for '" + contentType + "'");
return dataCodec;
}
String contentType();
void encode(OutputStream stream, Object value) throws IOException;
Object decode(InputStream stream, Type type) throws IOException;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy