Skip to content

Instantly share code, notes, and snippets.

@AustinLMayes
Last active April 22, 2020 23:37
Show Gist options
  • Save AustinLMayes/c3f234684495d12bb2dba658bac9c358 to your computer and use it in GitHub Desktop.
Save AustinLMayes/c3f234684495d12bb2dba658bac9c358 to your computer and use it in GitHub Desktop.
public class CollectionUtils {
@Nullable
public static <C> C highestNumberObject(List<C> list, Class<? extends C> clazz, Function<C, Number> refMethod) {
C highest = list
.stream().filter(v -> v.getClass().equals(clazz))
.max((a,b) -> Integer.max(refMethod.apply((C) a).intValue(), refMethod.apply((C) b).intValue())).orElse(null);
return highest;
}
@Nullable
public static <N, C> Multiset.Entry<N> mostCommonAttributeEntry(List<C> list, Class<? extends C> clazz, Function<C, N> refMethod) {
Multiset<N> commons = HashMultiset.create();
list.stream().filter(action -> action.getClass().equals(clazz)).forEach(action -> commons.add(refMethod.apply((C) action)));
return commons.entrySet()
.stream()
.max(Comparator.comparing(Multiset.Entry::getCount)).orElse(null);
}
@Nullable
public static <C> Number highestNumber(List<C> list, Class<? extends C> clazz, Function<C, Number> refMethod) {
C highest = highestNumberObject(list, clazz, refMethod);
if (highest != null)
return refMethod.apply(highest);
return null;
}
@Nullable
public static <N, C> N mostCommonAttribute(List<C> list, Class<? extends C> clazz, Function<C, N> refMethod) {
return Optional.ofNullable(mostCommonAttributeEntry(list, clazz, refMethod)).map(Multiset.Entry::getElement).orElse(null);
}
@Nullable
@SuppressWarnings("unchecked")
public static <A, B> List<A> allOfType(List<B> list, Class<? extends A> clazz) {
return (List<A>) list.stream().filter(action -> action.getClass().equals(clazz)).collect(Collectors.toList());
}
// Stuff from other class I pasted here cus relation
@Nullable
public <N, C extends Action> N mostCommonAttribute(Class<? extends C> actionClass, Function<C, N> refMethod) {
Multiset<N> commons = HashMultiset.create();
this.getPlayerLifetimes().values().stream()
.flatMap(listContainer -> listContainer.getActions().stream())
.collect(Collectors.toList())
.stream().filter(act -> act.getClass().equals(actionClass)).forEach(action -> commons.add(refMethod.apply((C) action)));
return commons.entrySet()
.stream()
.max(Comparator.comparing(Multiset.Entry::getCount)).map(Multiset.Entry::getElement).orElse(null);
}
@Nullable
public <N, C extends Action> N mostCommonAttribute(UUID actor, Class<? extends C> actionClass, Function<C, N> refMethod) {
Multiset<N> commons = HashMultiset.create();
this.getPlayerLifetimes().get(actor).stream()
.flatMap(listContainer -> listContainer.getActions().stream())
.collect(Collectors.toList())
.stream().filter(act -> act.getClass().equals(actionClass)).forEach(action -> commons.add(refMethod.apply((C) action)));
return commons.entrySet()
.stream()
.max(Comparator.comparing(Multiset.Entry::getCount)).map(Multiset.Entry::getElement).orElse(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment