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

guru.nidi.codeassert.dependency.DependencyMap Maven / Gradle / Ivy

There is a newer version: 0.9.15
Show newest version
/*
 * Copyright © 2015 Stefan Niederhauser ([email protected])
 *
 * 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 guru.nidi.codeassert.dependency;


import guru.nidi.codeassert.model.UsingElement;

import java.util.*;
import java.util.Map.Entry;

import static java.util.Collections.emptyMap;

public class DependencyMap {
    private final Map> map = new LinkedHashMap<>();

    public  void with(int specificity, UsingElement from, UsingElement to) {
        with(specificity, from.getName(), from.usedVia(to), to.getName());
    }

    DependencyMap with(int specificity, String from, Collection vias, String to) {
        final Map deps = map.computeIfAbsent(from, k -> new HashMap<>());
        final Info info = deps.get(to);
        if (info == null) {
            deps.put(to, new Info(vias, specificity));
        } else {
            //TODO specificity?
            info.getVias().addAll(vias);
        }
        return this;
    }

    DependencyMap with(String from, DependencyMap other) {
        final Map infos = other.getDependencies(from);
        for (final Entry entry : infos.entrySet()) {
            final Info info = entry.getValue();
            with(info.getSpecificity(), from, info.getVias(), entry.getKey());
        }
        return this;
    }

    public DependencyMap without(int specificity, String from, String to) {
        final Map deps = map.get(from);
        if (deps != null) {
            final Info info = deps.get(to);
            if (info != null && specificity > info.specificity) {
                deps.remove(to);
                if (deps.isEmpty()) {
                    map.remove(from);
                }
            }
        }
        return this;
    }

    public DependencyMap without(DependencyMap other) {
        for (final Entry> entry : other.map.entrySet()) {
            for (final Entry to : entry.getValue().entrySet()) {
                without(to.getValue().specificity, entry.getKey(), to.getKey());
            }
        }
        return this;
    }

    public void merge(DependencyMap deps) {
        for (final Entry> entry : deps.map.entrySet()) {
            final Map ds = map.get(entry.getKey());
            if (ds == null) {
                map.put(entry.getKey(), entry.getValue());
            } else {
                ds.putAll(entry.getValue());
            }
        }
    }

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

    public void clear() {
        map.clear();
    }

    public Set getElements() {
        return map.keySet();
    }

    /**
     * @param name the name of the element
     * @return A map with all dependencies of a given package.
     * Key: package, Value: A set of all classes importing the package
     */
    public Map getDependencies(String name) {
        return map.getOrDefault(name, emptyMap());
    }

    public Info getDependency(String from, String to) {
        return getDependencies(from).get(to);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        final DependencyMap that = (DependencyMap) o;

        return map.equals(that.map);

    }

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

    @Override
    public String toString() {
        return map.toString();
    }

    public static class Info {
        private final Set vias;
        private final int specificity;

        Info(Collection vias, int specificity) {
            this.vias = new HashSet<>(vias);
            this.specificity = specificity;
        }

        public Set getVias() {
            return vias;
        }

        public int getSpecificity() {
            return specificity;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            final Info info = (Info) o;
            return vias.equals(info.vias);
        }

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

        @Override
        public String toString() {
            return vias.toString();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy