| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterators;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import com.google.common.collect.Sets;
|
| 7 | import java.util.AbstractSet;
|
| 8 | import java.util.Iterator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Set;
|
| 12 |
|
| 13 | public class ClassInheritanceMultiMap<T> extends AbstractSet<T>
|
| 14 | {
|
| 15 | private static final Set < Class<? >> field_181158_a = Sets. < Class<? >> newHashSet();
|
| 16 | private final Map < Class<?>, List<T >> map = Maps. < Class<?>, List<T >> newHashMap();
|
| 17 | private final Set < Class<? >> knownKeys = Sets. < Class<? >> newIdentityHashSet();
|
| 18 | private final Class<T> baseClass;
|
| 19 | private final List<T> values = Lists.<T>newArrayList();
|
| 20 |
|
| 21 | public ClassInheritanceMultiMap(Class<T> baseClassIn)
|
| 22 | {
|
| 23 | this.baseClass = baseClassIn;
|
| 24 | this.knownKeys.add(baseClassIn);
|
| 25 | this.map.put(baseClassIn, this.values);
|
| 26 |
|
| 27 | for (Class<?> oclass : field_181158_a)
|
| 28 | {
|
| 29 | this.createLookup(oclass);
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | protected void createLookup(Class<?> clazz)
|
| 34 | {
|
| 35 | field_181158_a.add(clazz);
|
| 36 |
|
| 37 | for (T t : this.values)
|
| 38 | {
|
| 39 | if (clazz.isAssignableFrom(t.getClass()))
|
| 40 | {
|
| 41 | this.addForClass(t, clazz);
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | this.knownKeys.add(clazz);
|
| 46 | }
|
| 47 |
|
| 48 | protected Class<?> initializeClassLookup(Class<?> clazz)
|
| 49 | {
|
| 50 | if (this.baseClass.isAssignableFrom(clazz))
|
| 51 | {
|
| 52 | if (!this.knownKeys.contains(clazz))
|
| 53 | {
|
| 54 | this.createLookup(clazz);
|
| 55 | }
|
| 56 |
|
| 57 | return clazz;
|
| 58 | }
|
| 59 | else
|
| 60 | {
|
| 61 | throw new IllegalArgumentException("Don\'t know how to search for " + clazz);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | public boolean add(T p_add_1_)
|
| 66 | {
|
| 67 | for (Class<?> oclass : this.knownKeys)
|
| 68 | {
|
| 69 | if (oclass.isAssignableFrom(p_add_1_.getClass()))
|
| 70 | {
|
| 71 | this.addForClass(p_add_1_, oclass);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return true;
|
| 76 | }
|
| 77 |
|
| 78 | private void addForClass(T value, Class<?> parentClass)
|
| 79 | {
|
| 80 | List<T> list = (List)this.map.get(parentClass);
|
| 81 |
|
| 82 | if (list == null)
|
| 83 | {
|
| 84 | this.map.put(parentClass, Lists.newArrayList(value));
|
| 85 | }
|
| 86 | else
|
| 87 | {
|
| 88 | list.add(value);
|
| 89 | }
|
| 90 | }
|
| 91 |
|
| 92 | public boolean remove(Object p_remove_1_)
|
| 93 | {
|
| 94 | T t = (T)p_remove_1_;
|
| 95 | boolean flag = false;
|
| 96 |
|
| 97 | for (Class<?> oclass : this.knownKeys)
|
| 98 | {
|
| 99 | if (oclass.isAssignableFrom(t.getClass()))
|
| 100 | {
|
| 101 | List<T> list = (List)this.map.get(oclass);
|
| 102 |
|
| 103 | if (list != null && list.remove(t))
|
| 104 | {
|
| 105 | flag = true;
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | return flag;
|
| 111 | }
|
| 112 |
|
| 113 | public boolean contains(Object p_contains_1_)
|
| 114 | {
|
| 115 | return Iterators.contains(this.getByClass(p_contains_1_.getClass()).iterator(), p_contains_1_);
|
| 116 | }
|
| 117 |
|
| 118 | public <S> Iterable<S> getByClass(final Class<S> clazz)
|
| 119 | {
|
| 120 | return new Iterable<S>()
|
| 121 | {
|
| 122 | public Iterator<S> iterator()
|
| 123 | {
|
| 124 | List<T> list = (List)ClassInheritanceMultiMap.this.map.get(ClassInheritanceMultiMap.this.initializeClassLookup(clazz));
|
| 125 |
|
| 126 | if (list == null)
|
| 127 | {
|
| 128 | return Iterators.<S>emptyIterator();
|
| 129 | }
|
| 130 | else
|
| 131 | {
|
| 132 | Iterator<T> iterator = list.iterator();
|
| 133 | return Iterators.filter(iterator, clazz);
|
| 134 | }
|
| 135 | }
|
| 136 | };
|
| 137 | }
|
| 138 |
|
| 139 | public Iterator<T> iterator()
|
| 140 | {
|
| 141 | return this.values.isEmpty() ? Iterators.<T>emptyIterator() : Iterators.unmodifiableIterator(this.values.iterator());
|
| 142 | }
|
| 143 |
|
| 144 | public int size()
|
| 145 | {
|
| 146 | return this.values.size();
|
| 147 | }
|
| 148 | }
|
| 149 |
|