com.vladsch.flexmark.util.collection.SubClassingBag Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flexmark-util-collection Show documentation
Show all versions of flexmark-util-collection Show documentation
flexmark-java collection utility classes
The newest version!
package com.vladsch.flexmark.util.collection;
import com.vladsch.flexmark.util.collection.iteration.ReversibleIterable;
import java.util.BitSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
public class SubClassingBag {
private final @NotNull ClassificationBag, T> items;
private final @NotNull HashMap, BitSet> subClassMap;
public SubClassingBag(
@NotNull ClassificationBag, T> items,
Map, @NotNull List>> subClassMap) {
this.items = items;
this.subClassMap = new HashMap<>();
for (Class> clazz : subClassMap.keySet()) {
List> classList = subClassMap.get(clazz);
BitSet bitSet = this.items.categoriesBitSet(classList);
if (!bitSet.isEmpty()) {
this.subClassMap.put(clazz, bitSet);
}
}
}
public final @NotNull ReversibleIterable itemsOfType(
@NotNull Class xClass, @NotNull Class>... categories) {
return items.getCategoryItems(xClass, typeBitSet(xClass, categories));
}
public final @NotNull ReversibleIterable itemsOfType(
@NotNull Class xClass, @NotNull Collection> categories) {
return items.getCategoryItems(xClass, typeBitSet(xClass, categories));
}
public final @NotNull ReversibleIterable reversedItemsOfType(
@NotNull Class xClass, @NotNull Class>... categories) {
return items.getCategoryItemsReversed(xClass, typeBitSet(xClass, categories));
}
public final @NotNull ReversibleIterable reversedItemsOfType(
@NotNull Class xClass, @NotNull Collection> categories) {
return items.getCategoryItemsReversed(xClass, typeBitSet(xClass, categories));
}
public final @NotNull BitSet typeBitSet(
@NotNull Class> xClass, @NotNull Class>... categories) {
BitSet bitSet = new BitSet();
for (Class> category : categories) {
if (xClass.isAssignableFrom(category) && subClassMap.containsKey(category)) {
bitSet.or(subClassMap.get(category));
}
}
return bitSet;
}
public final @NotNull BitSet typeBitSet(
@NotNull Class> xClass, @NotNull Collection> categories) {
BitSet bitSet = new BitSet();
for (Class> category : categories) {
if (xClass.isAssignableFrom(category) && subClassMap.containsKey(category)) {
bitSet.or(subClassMap.get(category));
}
}
return bitSet;
}
}