-
Notifications
You must be signed in to change notification settings - Fork 2
Select enum constants from boolean[] or bit mask #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
31cddeb
95de1c3
6bff53c
6407f71
cd52ab5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (C) Posten Norge AS | ||
| * Copyright (C) Posten Bring AS | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
|
|
@@ -15,6 +15,7 @@ | |
| */ | ||
| package no.digipost; | ||
|
|
||
| import java.util.BitSet; | ||
| import java.util.Collection; | ||
| import java.util.function.Function; | ||
| import java.util.function.Predicate; | ||
|
|
@@ -23,6 +24,7 @@ | |
|
|
||
| import static java.util.Arrays.asList; | ||
| import static java.util.Arrays.stream; | ||
| import static java.util.Objects.requireNonNull; | ||
| import static java.util.stream.Collectors.joining; | ||
|
|
||
| /** | ||
|
|
@@ -94,6 +96,57 @@ public static <E extends Enum<E>> Stream<E> fromEnumsString(String enumsString, | |
| } | ||
|
|
||
|
|
||
| /** | ||
| * Select enum constants by mapping <em>indexes</em> of elements in a boolean array which are {@code true} to <em>ordinals</em> | ||
| * of the enum type. | ||
| * <p> | ||
| * It is valid that the enum contains either more or less constants than the given boolean array, | ||
| * and the resolving will discard any exhaustive elements in either cases. E.g. specifying two booleans | ||
| * will at most consider two first constants of a given enum type, as well as if the enum only has one | ||
| * constant, only the first boolean will be processed. | ||
| * | ||
| * @param includedOrdinals the boolean array where the <em>indexes</em> of {@code true} elements are mapped | ||
| * to any existing ordinals of the given {@code enumType} | ||
| * @param enumType the enum type to resolve constants from | ||
| * | ||
| * @return the resolved {@code enum} constants | ||
| */ | ||
| public static <E extends Enum<E>> Stream<E> selectByIndexAsOrdinals(boolean[] includedOrdinals, Class<E> enumType) { | ||
| BitSet mask = new BitSet(includedOrdinals.length); | ||
| for (int i = 0; i < includedOrdinals.length; i++) { | ||
| mask.set(i, includedOrdinals[i]); | ||
| } | ||
| return selectByBitmask(mask, enumType); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Select enum constants by overlaying a bitmask ({@code long}) and selecting the enum <em>by ordinals</em> | ||
| * which align with the indices of the set bits, mapping the first index to the first enum constant and so | ||
| * forth. A bitmask of {@code 10110001} (177) will select the first, fifth, sixth, and eight enum constant. | ||
| * <p> | ||
| * It is valid both for the enum to contain more constants than the length of the mask, | ||
| * or less constants than the highest index of a <em>set</em> bit in the given mask, | ||
| * and the resolving will discard any exhaustive elements in either cases. E.g. a set bit at | ||
| * index 2 will be ignored for enums with only one constant. | ||
| * | ||
| * @param mask the bit mask used for selecting enum constants, the method may mutate this mask | ||
| * @param enumType the enum type to resolve constants from | ||
| * | ||
| * @return the resolved {@code enum} constants | ||
| */ | ||
| public static <E extends Enum<E>> Stream<E> selectByBitmask(BitSet mask, Class<E> enumType) { | ||
| if (mask.cardinality() == 0) { | ||
| return Stream.empty(); | ||
| } | ||
| E[] candidates = requireNonNull(enumType.getEnumConstants(), enumType + " is not an enum"); | ||
| if (candidates.length == 0) { | ||
| return Stream.empty(); | ||
| } | ||
| return mask.get(0, candidates.length).stream().mapToObj(selectedOrdinal -> candidates[selectedOrdinal]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jeg skjønner åpenbart ikke hvordan dette virker… vil ikke
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ja, den er sneaky 😅 BitSet.stream() gir ikke hvert av elementene i bit-sekvensen, altså en strøm med 0-er og 1-ere og som typisk ville vært ekvivalent til å kalle Edit: Det som kanskje ikke er åpenbart her heller, er at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Da gir det straks mer mening! 💡 |
||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Join several {@code enum} constants to a comma separated string of their {@link Enum#name() names}. | ||
| * | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.