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.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.kafka.common.serialization;
import org.apache.kafka.common.utils.Bytes;
import java.nio.ByteBuffer;
import java.util.Map;
import java.util.UUID;
/**
* Factory for creating serializers / deserializers.
*/
public class Serdes {
static public class WrapperSerde implements Serde {
final private Serializer serializer;
final private Deserializer deserializer;
public WrapperSerde(Serializer serializer, Deserializer deserializer) {
this.serializer = serializer;
this.deserializer = deserializer;
}
@Override
public void configure(Map configs, boolean isKey) {
serializer.configure(configs, isKey);
deserializer.configure(configs, isKey);
}
@Override
public void close() {
serializer.close();
deserializer.close();
}
@Override
public Serializer serializer() {
return serializer;
}
@Override
public Deserializer deserializer() {
return deserializer;
}
}
static public final class VoidSerde extends WrapperSerde {
public VoidSerde() {
super(new VoidSerializer(), new VoidDeserializer());
}
}
static public final class LongSerde extends WrapperSerde {
public LongSerde() {
super(new LongSerializer(), new LongDeserializer());
}
}
static public final class IntegerSerde extends WrapperSerde {
public IntegerSerde() {
super(new IntegerSerializer(), new IntegerDeserializer());
}
}
static public final class ShortSerde extends WrapperSerde {
public ShortSerde() {
super(new ShortSerializer(), new ShortDeserializer());
}
}
static public final class FloatSerde extends WrapperSerde {
public FloatSerde() {
super(new FloatSerializer(), new FloatDeserializer());
}
}
static public final class DoubleSerde extends WrapperSerde {
public DoubleSerde() {
super(new DoubleSerializer(), new DoubleDeserializer());
}
}
static public final class StringSerde extends WrapperSerde {
public StringSerde() {
super(new StringSerializer(), new StringDeserializer());
}
}
static public final class ByteBufferSerde extends WrapperSerde {
public ByteBufferSerde() {
super(new ByteBufferSerializer(), new ByteBufferDeserializer());
}
}
static public final class BytesSerde extends WrapperSerde {
public BytesSerde() {
super(new BytesSerializer(), new BytesDeserializer());
}
}
static public final class ByteArraySerde extends WrapperSerde {
public ByteArraySerde() {
super(new ByteArraySerializer(), new ByteArrayDeserializer());
}
}
static public final class UUIDSerde extends WrapperSerde {
public UUIDSerde() {
super(new UUIDSerializer(), new UUIDDeserializer());
}
}
@SuppressWarnings("unchecked")
static public Serde serdeFrom(Class type) {
if (String.class.isAssignableFrom(type)) {
return (Serde) String();
}
if (Short.class.isAssignableFrom(type)) {
return (Serde) Short();
}
if (Integer.class.isAssignableFrom(type)) {
return (Serde) Integer();
}
if (Long.class.isAssignableFrom(type)) {
return (Serde) Long();
}
if (Float.class.isAssignableFrom(type)) {
return (Serde) Float();
}
if (Double.class.isAssignableFrom(type)) {
return (Serde) Double();
}
if (byte[].class.isAssignableFrom(type)) {
return (Serde) ByteArray();
}
if (ByteBuffer.class.isAssignableFrom(type)) {
return (Serde) ByteBuffer();
}
if (Bytes.class.isAssignableFrom(type)) {
return (Serde) Bytes();
}
if (UUID.class.isAssignableFrom(type)) {
return (Serde) UUID();
}
// TODO: we can also serializes objects of type T using generic Java serialization by default
throw new IllegalArgumentException("Unknown class for built-in serializer. Supported types are: " +
"String, Short, Integer, Long, Float, Double, ByteArray, ByteBuffer, Bytes, UUID");
}
/**
* Construct a serde object from separate serializer and deserializer
*
* @param serializer must not be null.
* @param deserializer must not be null.
*/
static public Serde serdeFrom(final Serializer serializer, final Deserializer deserializer) {
if (serializer == null) {
throw new IllegalArgumentException("serializer must not be null");
}
if (deserializer == null) {
throw new IllegalArgumentException("deserializer must not be null");
}
return new WrapperSerde<>(serializer, deserializer);
}
/*
* A serde for nullable {@code Long} type.
*/
static public Serde Long() {
return new LongSerde();
}
/*
* A serde for nullable {@code Integer} type.
*/
static public Serde Integer() {
return new IntegerSerde();
}
/*
* A serde for nullable {@code Short} type.
*/
static public Serde Short() {
return new ShortSerde();
}
/*
* A serde for nullable {@code Float} type.
*/
static public Serde Float() {
return new FloatSerde();
}
/*
* A serde for nullable {@code Double} type.
*/
static public Serde Double() {
return new DoubleSerde();
}
/*
* A serde for nullable {@code String} type.
*/
static public Serde String() {
return new StringSerde();
}
/*
* A serde for nullable {@code ByteBuffer} type.
*/
static public Serde ByteBuffer() {
return new ByteBufferSerde();
}
/*
* A serde for nullable {@code Bytes} type.
*/
static public Serde Bytes() {
return new BytesSerde();
}
/*
* A serde for nullable {@code UUID} type
*/
static public Serde UUID() {
return new UUIDSerde();
}
/*
* A serde for nullable {@code byte[]} type.
*/
static public Serde ByteArray() {
return new ByteArraySerde();
}
/*
* A serde for {@code Void} type.
*/
static public Serde Void() {
return new VoidSerde();
}
}