All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.bucket4j.distributed.serialization.PrimitiveSerializationHandles Maven / Gradle / Ivy

The newest version!
/*-
 * ========================LICENSE_START=================================
 * Bucket4j
 * %%
 * Copyright (C) 2015 - 2020 Vladimir Bukhtoyarov
 * %%
 * 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.
 * =========================LICENSE_END==================================
 */
package io.github.bucket4j.distributed.serialization;

import io.github.bucket4j.Nothing;
import io.github.bucket4j.distributed.versioning.Version;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PrimitiveSerializationHandles {

    public static final SerializationHandle NULL_HANDLE = new SerializationHandle<>() {

        @Override
        public  Nothing deserialize(DeserializationAdapter adapter, I input) {
            return null;
        }

        @Override
        public  void serialize(SerializationAdapter adapter, O output, Nothing serializableObject, Version backwardCompatibilityVersion, Scope scope) {

        }

        @Override
        public int getTypeId() {
            return 0;
        }

        @Override
        public Class getSerializedType() {
            return Nothing.class;
        }

        @Override
        public Nothing fromJsonCompatibleSnapshot(Map snapshot) {
            return Nothing.INSTANCE;
        }

        @Override
        public Map toJsonCompatibleSnapshot(Nothing serializableObject, Version backwardCompatibilityVersion, Scope scope) {
            return new HashMap<>();
        }

        @Override
        public String getTypeName() {
            return "Nothing";
        }
    };

    public static final SerializationHandle LONG_HANDLE = new SerializationHandle<>() {
        @Override
        public  Long deserialize(DeserializationAdapter adapter, I input) throws IOException {
            return adapter.readLong(input);
        }

        @Override
        public  void serialize(SerializationAdapter adapter, O output, Long value, Version backwardCompatibilityVersion, Scope scope) throws IOException {
            adapter.writeLong(output, value);
        }

        @Override
        public int getTypeId() {
            return -1;
        }

        @Override
        public Class getSerializedType() {
            return Long.TYPE;
        }

        @Override
        public Long fromJsonCompatibleSnapshot(Map snapshot) {
            return readLongValue(snapshot, "value");
        }

        @Override
        public Map toJsonCompatibleSnapshot(Long serializableObject, Version backwardCompatibilityVersion, Scope scope) {
            Map result = new HashMap<>();
            result.put("value", serializableObject);
            return result;
        }

        @Override
        public String getTypeName() {
            return "Long";
        }
    };

    public static final SerializationHandle BOOLEAN_HANDLE = new SerializationHandle<>() {
        @Override
        public  Boolean deserialize(DeserializationAdapter adapter, I input) throws IOException {
            return adapter.readBoolean(input);
        }

        @Override
        public  void serialize(SerializationAdapter adapter, O output, Boolean value, Version backwardCompatibilityVersion, Scope scope) throws IOException {
            adapter.writeBoolean(output, value);
        }

        @Override
        public int getTypeId() {
            return -2;
        }

        @Override
        public Class getSerializedType() {
            return Boolean.TYPE;
        }

        @Override
        public Boolean fromJsonCompatibleSnapshot(Map snapshot) {
            return (Boolean) snapshot.get("value");
        }

        @Override
        public Map toJsonCompatibleSnapshot(Boolean serializableObject, Version backwardCompatibilityVersion, Scope scope) {
            Map result = new HashMap<>();
            result.put("value", serializableObject);
            return result;
        }

        @Override
        public String getTypeName() {
            return "Boolean";
        }
    };

    public static final SerializationHandle[] primitiveHandlesById = new SerializationHandle[] {
            NULL_HANDLE,
            LONG_HANDLE,
            BOOLEAN_HANDLE
    };

}