Last active
April 22, 2020 23:37
-
-
Save AustinLMayes/c3f234684495d12bb2dba658bac9c358 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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