DependencySorter.java

net.minecraft.util.DependencySorter

信息

  • 全限定名:net.minecraft.util.DependencySorter
  • 类型:public class
  • 包:net.minecraft.util
  • 源码路径:src/main/java/net/minecraft/util/DependencySorter.java
  • 起始行号:L13
  • 职责:

    TODO

字段/常量

  • contents
    • 类型: Map<K,V>
    • 修饰符: private final
    • 源码定位: L14
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.util.DependencySorter.Entry
    • 类型: interface
    • 修饰符: public
    • 源码定位: L50
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public DependencySorter<K,V> addEntry(K id, V value) @ L16

  • 方法名:addEntry
  • 源码定位:L16
  • 返回类型:DependencySorter<K,V>
  • 修饰符:public

参数:

  • id: K
  • value: V

说明:

TODO

private void visitDependenciesAndElement(Multimap<K,K> dependencies, Set<K> alreadyVisited, K id, BiConsumer<K,V> output) @ L21

  • 方法名:visitDependenciesAndElement
  • 源码定位:L21
  • 返回类型:void
  • 修饰符:private

参数:

  • dependencies: Multimap<K,K>
  • alreadyVisited: Set
  • id: K
  • output: BiConsumer<K,V>

说明:

TODO

private static <K> boolean isCyclic(Multimap<K,K> directDependencies, K from, K to) @ L31

  • 方法名:isCyclic
  • 源码定位:L31
  • 返回类型: boolean
  • 修饰符:private static

参数:

  • directDependencies: Multimap<K,K>
  • from: K
  • to: K

说明:

TODO

private static <K> void addDependencyIfNotCyclic(Multimap<K,K> directDependencies, K from, K to) @ L36

  • 方法名:addDependencyIfNotCyclic
  • 源码定位:L36
  • 返回类型: void
  • 修饰符:private static

参数:

  • directDependencies: Multimap<K,K>
  • from: K
  • to: K

说明:

TODO

public void orderByDependencies(BiConsumer<K,V> output) @ L42

  • 方法名:orderByDependencies
  • 源码定位:L42
  • 返回类型:void
  • 修饰符:public

参数:

  • output: BiConsumer<K,V>

说明:

TODO

代码

public class DependencySorter<K, V extends DependencySorter.Entry<K>> {
    private final Map<K, V> contents = new HashMap<>();
 
    public DependencySorter<K, V> addEntry(K id, V value) {
        this.contents.put(id, value);
        return this;
    }
 
    private void visitDependenciesAndElement(Multimap<K, K> dependencies, Set<K> alreadyVisited, K id, BiConsumer<K, V> output) {
        if (alreadyVisited.add(id)) {
            dependencies.get(id).forEach(dependency -> this.visitDependenciesAndElement(dependencies, alreadyVisited, (K)dependency, output));
            V current = this.contents.get(id);
            if (current != null) {
                output.accept(id, current);
            }
        }
    }
 
    private static <K> boolean isCyclic(Multimap<K, K> directDependencies, K from, K to) {
        Collection<K> dependencies = directDependencies.get(to);
        return dependencies.contains(from) ? true : dependencies.stream().anyMatch(dep -> isCyclic(directDependencies, from, (K)dep));
    }
 
    private static <K> void addDependencyIfNotCyclic(Multimap<K, K> directDependencies, K from, K to) {
        if (!isCyclic(directDependencies, from, to)) {
            directDependencies.put(from, to);
        }
    }
 
    public void orderByDependencies(BiConsumer<K, V> output) {
        Multimap<K, K> directDependencies = HashMultimap.create();
        this.contents.forEach((id, value) -> value.visitRequiredDependencies(dep -> addDependencyIfNotCyclic(directDependencies, (K)id, dep)));
        this.contents.forEach((id, value) -> value.visitOptionalDependencies(dep -> addDependencyIfNotCyclic(directDependencies, (K)id, dep)));
        Set<K> alreadyVisited = new HashSet<>();
        this.contents.keySet().forEach(topId -> this.visitDependenciesAndElement(directDependencies, alreadyVisited, (K)topId, output));
    }
 
    public interface Entry<K> {
        void visitRequiredDependencies(final Consumer<K> output);
 
        void visitOptionalDependencies(final Consumer<K> output);
    }
}

引用的其他类