    001    package cpw.mods.fml.common.discovery;
    002
    003    import java.util.List;
    004    import java.util.Map;
    005    import java.util.Set;
    006
    007    import com.google.common.base.Predicate;
    008    import com.google.common.collect.HashMultimap;
    009    import com.google.common.collect.ImmutableMap;
    010    import com.google.common.collect.ImmutableSetMultimap;
    011    import com.google.common.collect.Lists;
    012    import com.google.common.collect.Multimap;
    013    import com.google.common.collect.Multimaps;
    014    import com.google.common.collect.SetMultimap;
    015
    016    import cpw.mods.fml.common.ModContainer;
    017
    018    public class ASMDataTable
    019    {
    020        public static class ASMData
    021        {
    022            private ModCandidate candidate;
    023            private String annotationName;
    024            private String className;
    025            private String objectName;
    026            private Map<String,Object> annotationInfo;
    027            public ASMData(ModCandidate candidate, String annotationName, String className, String objectName, Map<String,Object> info)
    028            {
    029                this.candidate = candidate;
    030                this.annotationName = annotationName;
    031                this.className = className;
    032                this.objectName = objectName;
    033                this.annotationInfo = info;
    034            }
    035            public ModCandidate getCandidate()
    036            {
    037                return candidate;
    038            }
    039            public String getAnnotationName()
    040            {
    041                return annotationName;
    042            }
    043            public String getClassName()
    044            {
    045                return className;
    046            }
    047            public String getObjectName()
    048            {
    049                return objectName;
    050            }
    051            public Map<String, Object> getAnnotationInfo()
    052            {
    053                return annotationInfo;
    054            }
    055        }
    056
    057        private static class ModContainerPredicate implements Predicate<ASMData>
    058        {
    059            private ModContainer container;
    060            public ModContainerPredicate(ModContainer container)
    061            {
    062                this.container = container;
    063            }
    064            public boolean apply(ASMData data)
    065            {
    066                return container.getSource().equals(data.candidate.getModContainer());
    067            }
    068        }
    069        private SetMultimap<String, ASMData> globalAnnotationData = HashMultimap.create();
    070        private Map<ModContainer, SetMultimap<String,ASMData>> containerAnnotationData;
    071
    072        private List<ModContainer> containers = Lists.newArrayList();
    073
    074        public SetMultimap<String,ASMData> getAnnotationsFor(ModContainer container)
    075        {
    076            if (containerAnnotationData == null)
    077            {
    078                ImmutableMap.Builder<ModContainer, SetMultimap<String, ASMData>> mapBuilder = ImmutableMap.<ModContainer, SetMultimap<String,ASMData>>builder();
    079                for (ModContainer cont : containers)
    080                {
    081                    Multimap<String, ASMData> values = Multimaps.filterValues(globalAnnotationData, new ModContainerPredicate(cont));
    082                    mapBuilder.put(cont, ImmutableSetMultimap.copyOf(values));
    083                }
    084                containerAnnotationData = mapBuilder.build();
    085            }
    086            return containerAnnotationData.get(container);
    087        }
    088
    089        public Set<ASMData> getAll(String annotation)
    090        {
    091            return globalAnnotationData.get(annotation);
    092        }
    093
    094        public void addASMData(ModCandidate candidate, String annotation, String className, String objectName, Map<String,Object> annotationInfo)
    095        {
    096            globalAnnotationData.put(annotation, new ASMData(candidate, annotation, className, objectName, annotationInfo));
    097        }
    098
    099        public void addContainer(ModContainer container)
    100        {
    101            this.containers.add(container);
    102        }
    103    }