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

io.ultreia.java4all.classmapping.ImmutableClassMap Maven / Gradle / Ivy

package io.ultreia.java4all.classmapping;

/*-
 * #%L
 * Class Mapping tools
 * %%
 * Copyright (C) 2017 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.collect.ImmutableMap;
import java.util.Collection;
import java.util.Set;
import java.util.TreeMap;

/**
 * Created by tchemit on 20/09/17.
 *
 * @author Tony Chemit - [email protected]
 */
public abstract class ImmutableClassMap {

    public interface KeyFunction {
        String key(Class type);

        String key(String type);
    }

    protected static abstract class BuilderSupport> {

        protected final KeyFunction keyFunction;
        protected final TreeMap data;
        protected final ImmutableMap.Builder types;

        protected abstract M build(KeyFunction keyFunction, ImmutableMap data, ImmutableMap types);

        protected BuilderSupport(KeyFunction keyFunction) {
            this.keyFunction = keyFunction;
            this.data = new TreeMap<>();
            this.types = ImmutableMap.builder();
        }

        public BuilderSupport put(Class type, V value) {
            String key = keyFunction.key(type);
            types.put(key,type);
            data.put(key, value);
            return this;
        }

        public M build() {
            return build(this.keyFunction, ImmutableMap.copyOf(data), types.build());
        }
    }

    protected final KeyFunction keyFunction;
    protected final ImmutableMap data;
    protected final ImmutableMap types;

    protected ImmutableClassMap(ImmutableClassMap dtoMap) {
        this.keyFunction = dtoMap.keyFunction;
        this.data = dtoMap.data;
        this.types = dtoMap.types;
    }

    protected ImmutableClassMap(KeyFunction keyFunction, ImmutableMap data, ImmutableMap types) {
        this.keyFunction = keyFunction;
        this.data = data;
        this.types = types;
    }

    public int size() {
        return data.size();
    }

    public boolean isEmpty() {
        return data.isEmpty();
    }

    public V get(Class key) {
        return data.get(key0(key));
    }

    public V get(String key) {
        return data.get(key0(key));
    }

    public Set keySet() {
        return data.keySet();
    }

    public Collection values() {
        return data.values();
    }

    private String key0(Class type) {
        return keyFunction.key(type);
    }

    private String key0(String type) {
        return keyFunction.key(type);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy