
org.reflections.Store Maven / Gradle / Ivy
package org.reflections;
import repacked.com.google.common.collect.Iterables;
import repacked.com.google.common.collect.Multimap;
import repacked.com.google.common.collect.Multimaps;
import java.util.*;
/**
* 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 final Map> storeMap;
private transient boolean concurrent;
//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) {
Multimap multimap =
Multimaps.newSetMultimap(new HashMap>(), HashSet::new);
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 = new ArrayList<>();
private void addAll(Iterable iterable) {
chain.add(iterable);
}
public Iterator iterator() {
return Iterables.concat(chain).iterator();
}
}
}