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

io.microsphere.io.Serializers Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
/*
 * 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 io.microsphere.io;


import io.microsphere.util.PriorityComparator;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static io.microsphere.reflect.TypeUtils.resolveTypeArguments;
import static java.util.Collections.emptyList;
import static java.util.ServiceLoader.load;

/**
 * {@link Serializer} Utilities class
 *
 * @author Mercy
 * @since 1.0.0
 */
public class Serializers {

    private final Map, List> typedSerializers = new HashMap<>();

    private final ClassLoader classLoader;

    public Serializers(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    public Serializers() {
        this(Thread.currentThread().getContextClassLoader());
    }

    public void loadSPI() {
        for (Serializer serializer : load(Serializer.class)) {
            List> typeArguments = resolveTypeArguments(serializer.getClass());
            Class targetClass = typeArguments.isEmpty() ? Object.class : typeArguments.get(0);
            List serializers = typedSerializers.computeIfAbsent(targetClass, k -> new LinkedList());
            serializers.add(serializer);
            serializers.sort(PriorityComparator.INSTANCE);
        }
    }

    /**
     * Get the most compatible instance of {@link Serializer} by the specified deserialized type
     *
     * @param serializedType the type to be serialized
     * @return null if not found
     */
    public Serializer getMostCompatible(Class serializedType) {
        Serializer serializer = getHighestPriority(serializedType);
        if (serializer == null) {
            serializer = getLowestPriority(Object.class);
        }
        return serializer;
    }

    /**
     * Get the highest priority instance of {@link Serializer} by the specified serialized type
     *
     * @param serializedType the type to be serialized
     * @param             the type to be serialized
     * @return null if not found
     */
    public  Serializer getHighestPriority(Class serializedType) {
        List> serializers = get(serializedType);
        return serializers.isEmpty() ? null : serializers.get(0);
    }


    /**
     * Get the lowest priority instance of {@link Serializer} by the specified serialized type
     *
     * @param serializedType the type to be serialized
     * @param             the type to be serialized
     * @return null if not found
     */
    public  Serializer getLowestPriority(Class serializedType) {
        List> serializers = get(serializedType);
        return serializers.isEmpty() ? null : serializers.get(serializers.size() - 1);
    }

    /**
     * Get all instances of {@link Serializer} by the specified serialized type
     *
     * @param serializedType the type to be serialized
     * @param             the type to be serialized
     * @return non-null {@link List}
     */
    public  List> get(Class serializedType) {
        return (List) typedSerializers.getOrDefault(serializedType, emptyList());
    }
}