mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 12:18:00 +00:00
Improved CountAll method implementation using Maps
This commit is contained in:
@@ -31,7 +31,9 @@ import com.google.common.base.Function;
|
|||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
|
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
import forge.item.InventoryItem;
|
import forge.item.InventoryItem;
|
||||||
|
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
@@ -137,30 +139,26 @@ public class ItemPool<T extends InventoryItem> implements Iterable<Entry<T, Inte
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final <U extends InventoryItem> int countAll(Predicate<U> condition){
|
public int countAll(Predicate<T> condition){
|
||||||
int count = 0;
|
int count = 0;
|
||||||
Iterable<T> matchingKeys = Iterables.filter(this.items.keySet(), new Predicate<T>() {
|
for (Integer v : Maps.filterKeys(this.items, condition).values())
|
||||||
@Override
|
count += v;
|
||||||
public boolean apply(T item) {
|
|
||||||
return condition.apply((U)item);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
for (T key : matchingKeys)
|
|
||||||
count += this.items.get(key);
|
|
||||||
return count;
|
return count;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public final <U extends InventoryItem> int countAll(Predicate<U> condition, Class<U> cls) {
|
public final <U extends InventoryItem> int countAll(Predicate<U> condition, Class<U> cls) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
Iterable<T> matchingKeys = Iterables.filter(this.items.keySet(), new Predicate<T>() {
|
Map<T, Integer> matchingKeys = Maps.filterKeys(this.items, new Predicate<T>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(T item) {
|
public boolean apply(T item) {
|
||||||
return cls.isInstance(item) && condition.apply((U)item);
|
return cls.isInstance(item) && (condition.apply((U)item));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (T key : matchingKeys)
|
for (Integer i : matchingKeys.values()) {
|
||||||
count += this.items.get(key);
|
count += i;
|
||||||
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user