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

org.jetlinks.rule.engine.api.RuleDataCodecs Maven / Gradle / Ivy

The newest version!
package org.jetlinks.rule.engine.api;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class RuleDataCodecs {

    private static final List suppliers = new CopyOnWriteArrayList<>();

    private static final Map> codecs = new ConcurrentHashMap<>();

    public static void register(RuleDataCodecSupplier supplier) {
        suppliers.add(supplier);

        suppliers.sort(Comparator.comparing(RuleDataCodecSupplier::getOrder));
    }

    public static  void register(Class type, RuleDataCodec codec) {
        codecs.put(type, codec);
    }

    @SuppressWarnings("all")
    public static  Optional> getCodec(Class type) {

        RuleDataCodec codec = (RuleDataCodec) codecs.get(type);
        if (null != codec) {
            return Optional.of(codec);
        }

        synchronized (type) {
            for (RuleDataCodecSupplier supplier : suppliers) {
                if (supplier.isSupport(type)) {
                    codecs.put(type, codec = supplier.getCodec());
                    return Optional.of(codec);
                }
            }
        }

        return Optional.empty();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy