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

com.cognitect.transit.impl.WriteHandlerMap Maven / Gradle / Ivy

// Copyright 2014 Cognitect. All Rights Reserved.
//
// 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 com.cognitect.transit.impl;

import com.cognitect.transit.WriteHandler;

import java.util.*;

public class WriteHandlerMap implements TagProvider, Map> {

    private final Map> handlers;
    private WriteHandlerMap verboseHandlerMap;

    public WriteHandlerMap(Map> customHandlers) {
        this.handlers = WriterFactory.defaultHandlers();
        if (customHandlers != null) {
            handlers.putAll(customHandlers);
        }
        setTagProvider(handlers);
    }

    public WriteHandlerMap verboseWriteHandlerMap() {
        if (verboseHandlerMap == null) {
            Map> verboseHandlers = new HashMap>(handlers.size());
            for (Map.Entry> entry : handlers.entrySet()) {
                WriteHandler verboseHandler = entry.getValue().getVerboseHandler();
                verboseHandlers.put(
                        entry.getKey(),
                        (verboseHandler == null) ? entry.getValue() : verboseHandler);
            }
            this.verboseHandlerMap = new WriteHandlerMap(verboseHandlers);
        }
        return this.verboseHandlerMap;
    }

    private void setTagProvider(Map> handlers) {
        Iterator> i = handlers.values().iterator();
        while(i.hasNext()) {
            WriteHandler h = i.next();
            if(h instanceof TagProviderAware)
                ((TagProviderAware)h).setTagProvider(this);
        }
    }

    private Map> getUnderlyingMap() {
        return handlers;
    }

    @Override
    public int size() {
        return handlers.size();
    }

    @Override
    public boolean isEmpty() {
        return handlers.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return handlers.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return handlers.containsValue(value);
    }

    @Override
    public WriteHandler get(Object key) {
        return handlers.get(key);
    }

    @Override
    public WriteHandler put(Class key, WriteHandler value) {
        throw new UnsupportedOperationException("WriteHandlerMap is read-only");
    }

    @Override
    public WriteHandler remove(Object key) {
        throw new UnsupportedOperationException("WriteHandlerMap is read-only");
    }

    @Override
    public void putAll(Map> m) {
        throw new UnsupportedOperationException("WriteHandlerMap is read-only");
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException("WriteHandlerMap is read-only");
    }

    @Override
    public Set keySet() {
        return handlers.keySet();
    }

    @Override
    public Collection> values() {
        return handlers.values();
    }

    @Override
    public Set>> entrySet() {
        return handlers.entrySet();
    }

    @Override
    public int hashCode() {
        return handlers.hashCode();
    }

    public boolean equals(Object other) {
        return (other instanceof WriteHandlerMap) && getUnderlyingMap().equals(((WriteHandlerMap)other).getUnderlyingMap());
    }

    public WriteHandler getHandler(Object o) {
        Class c = (o != null) ? o.getClass() : null;
        WriteHandler h = null;

        if(h == null) h = handlers.get(c);
        if(h == null) h = checkBaseClasses(c);
        if(h == null) h = checkBaseInterfaces(c);

        return (WriteHandler) h;
    }

    private WriteHandler checkBaseClasses(Class c) {
        for(Class base = c.getSuperclass(); base != Object.class; base = base.getSuperclass()) {
            WriteHandler h = handlers.get(base);
            if(h != null) {
                handlers.put(c, h);
                return h;
            }
        }
        return null;
    }

    private WriteHandler checkBaseInterfaces(Class c) {
        Map> possibles = new HashMap>();
        for (Class base = c; base != Object.class; base = base.getSuperclass()) {
            for (Class itf : base.getInterfaces()) {
                WriteHandler h = handlers.get(itf);
                if (h != null) possibles.put(itf, h);
            }
        }
        switch (possibles.size()) {
            case 0: return null;
            case 1: {
                WriteHandler h = possibles.values().iterator().next();
                handlers.put(c, h);
                return h;
            }
            default: throw new RuntimeException("More thane one match for " + c);
        }
    }

    @Override
    public String getTag(Object o) {
        WriteHandler h = getHandler(o);
        if (h == null) return null;
        return h.tag(o);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy