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

org.reflections.Store Maven / Gradle / Ivy

There is a newer version: 1.5.7
Show newest version
package org.reflections;

import com.google.common.base.Supplier;
import com.google.common.collect.*;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * stores metadata information in multimaps
 * 

use the different query methods (getXXX) to query the metadata *

the query methods are string based, and does not cause the class loader to define the types *

use {@link org.reflections.Reflections#getStore()} to access this store */ public class Store { private transient boolean concurrent; private final Map> storeMap; //used via reflection @SuppressWarnings("UnusedDeclaration") protected Store() { storeMap = new HashMap>(); concurrent = false; } public Store(Configuration configuration) { storeMap = new HashMap>(); concurrent = configuration.getExecutorService() != null; } /** return all indices */ public Set keySet() { return storeMap.keySet(); } /** get or create the multimap object for the given {@code index} */ public Multimap getOrCreate(String index) { Multimap mmap = storeMap.get(index); if (mmap == null) { SetMultimap multimap = Multimaps.newSetMultimap(new HashMap>(), new Supplier>() { public Set get() { return Sets.newSetFromMap(new ConcurrentHashMap()); } }); mmap = concurrent ? Multimaps.synchronizedSetMultimap(multimap) : multimap; storeMap.put(index,mmap); } return mmap; } /** get the multimap object for the given {@code index}, otherwise throws a {@link org.reflections.ReflectionsException} */ public Multimap get(String index) { Multimap mmap = storeMap.get(index); if (mmap == null) { throw new ReflectionsException("Scanner " + index + " was not configured"); } return mmap; } /** get the values stored for the given {@code index} and {@code keys} */ public Iterable get(String index, String... keys) { return get(index, Arrays.asList(keys)); } /** get the values stored for the given {@code index} and {@code keys} */ public Iterable get(String index, Iterable keys) { Multimap mmap = get(index); IterableChain result = new IterableChain(); for (String key : keys) { result.addAll(mmap.get(key)); } return result; } /** recursively get the values stored for the given {@code index} and {@code keys}, including keys */ private Iterable getAllIncluding(String index, Iterable keys, IterableChain result) { result.addAll(keys); for (String key : keys) { Iterable values = get(index, key); if (values.iterator().hasNext()) { getAllIncluding(index, values, result); } } return result; } /** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */ public Iterable getAll(String index, String key) { return getAllIncluding(index, get(index, key), new IterableChain()); } /** recursively get the values stored for the given {@code index} and {@code keys}, not including keys */ public Iterable getAll(String index, Iterable keys) { return getAllIncluding(index, get(index, keys), new IterableChain()); } private static class IterableChain implements Iterable { private final List> chain = Lists.newArrayList(); private void addAll(Iterable iterable) { chain.add(iterable); } public Iterator iterator() { return Iterables.concat(chain).iterator(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy