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

org.gradle.internal.serialize.BaseSerializerFactory Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * Copyright 2014 the original author or authors.
 *
 * 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 org.gradle.internal.serialize;

import com.google.common.collect.ImmutableMap;

import java.io.File;
import java.util.Map;

public class BaseSerializerFactory {
    public static final Serializer STRING_SERIALIZER = new StringSerializer();
    public static final Serializer BOOLEAN_SERIALIZER = new BooleanSerializer();
    public static final Serializer LONG_SERIALIZER = new LongSerializer();
    public static final Serializer FILE_SERIALIZER = new FileSerializer();
    public static final Serializer BYTE_ARRAY_SERIALIZER = new ByteArraySerializer();
    public static final Serializer> NO_NULL_STRING_MAP_SERIALIZER = new StringMapSerializer();
    public static final Serializer THROWABLE_SERIALIZER = new ThrowableSerializer();

    public  Serializer getSerializerFor(Class type) {
        if (type.equals(String.class)) {
            return (Serializer) STRING_SERIALIZER;
        }
        if (type.equals(Long.class)) {
            return (Serializer) LONG_SERIALIZER;
        }
        if (type.equals(File.class)) {
            return (Serializer) FILE_SERIALIZER;
        }
        if (type.equals(byte[].class)) {
            return (Serializer) BYTE_ARRAY_SERIALIZER;
        }
        if (type.isEnum()) {
            return new EnumSerializer(type);
        }
        if (type.equals(Boolean.class)) {
            return (Serializer) BOOLEAN_SERIALIZER;
        }
        if (Throwable.class.isAssignableFrom(type)) {
            return (Serializer) THROWABLE_SERIALIZER;
        }
        return new DefaultSerializer(type.getClassLoader());
    }

    private static class EnumSerializer implements Serializer {
        private final Class type;

        private EnumSerializer(Class type) {
            this.type = type;
        }

        public T read(Decoder decoder) throws Exception {
            return type.getEnumConstants()[decoder.readSmallInt()];
        }

        public void write(Encoder encoder, T value) throws Exception {
            encoder.writeSmallInt((byte) value.ordinal());
        }
    }

    private static class LongSerializer implements Serializer {
        public Long read(Decoder decoder) throws Exception {
            return decoder.readLong();
        }

        public void write(Encoder encoder, Long value) throws Exception {
            encoder.writeLong(value);
        }
    }

    private static class StringSerializer implements Serializer {
        public String read(Decoder decoder) throws Exception {
            return decoder.readString();
        }

        public void write(Encoder encoder, String value) throws Exception {
            encoder.writeString(value);
        }
    }

    private static class FileSerializer implements Serializer {
        public File read(Decoder decoder) throws Exception {
            return new File(decoder.readString());
        }

        public void write(Encoder encoder, File value) throws Exception {
            encoder.writeString(value.getPath());
        }
    }

    private static class ByteArraySerializer implements Serializer {
        public byte[] read(Decoder decoder) throws Exception {
            return decoder.readBinary();
        }

        public void write(Encoder encoder, byte[] value) throws Exception {
            encoder.writeBinary(value);
        }
    }

    private static class StringMapSerializer implements Serializer> {
        public Map read(Decoder decoder) throws Exception {
            int pairs = decoder.readSmallInt();
            ImmutableMap.Builder builder = ImmutableMap.builder();
            for (int i = 0; i < pairs; ++i) {
                builder.put(decoder.readString(), decoder.readString());
            }
            return builder.build();
        }

        public void write(Encoder encoder, Map value) throws Exception {
            encoder.writeSmallInt(value.size());
            for (Map.Entry entry : value.entrySet()) {
                encoder.writeString(entry.getKey());
                encoder.writeString(entry.getValue());
            }
        }
    }

    private static class BooleanSerializer implements Serializer {
        @Override
        public Boolean read(Decoder decoder) throws Exception {
            return decoder.readBoolean();
        }

        @Override
        public void write(Encoder encoder, Boolean value) throws Exception {
            encoder.writeBoolean(value);
        }
    }

    private static class ThrowableSerializer implements Serializer {
        public Throwable read(Decoder decoder) throws Exception {
            return (Throwable) Message.receive(decoder.getInputStream(), getClass().getClassLoader());
        }

        public void write(Encoder encoder, Throwable value) throws Exception {
            Message.send(value, encoder.getOutputStream());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy