forked from opentripplanner/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a3ef6a
commit e035d5e
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
utils/src/main/java/org/opentripplanner/utils/collection/ImmutableEnumSet.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.opentripplanner.utils.collection; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.opentripplanner.utils.lang.BitSetUtils; | ||
|
||
public final class ImmutableEnumSet<T extends Enum<T>> { | ||
|
||
private final int bitset; | ||
private final Class<T> clazz; | ||
|
||
private ImmutableEnumSet(Class<T> clazz, int bitset) { | ||
this.bitset = bitset; | ||
this.clazz = clazz; | ||
} | ||
|
||
@SafeVarargs | ||
public static <E extends Enum<E>> ImmutableEnumSet<E> of(Class<E> clazz, E... elements) { | ||
checkClass(clazz); | ||
|
||
int bitset = 0; | ||
for(E e : elements) { | ||
bitset = BitSetUtils.set(bitset, e.ordinal(), true); | ||
} | ||
return new ImmutableEnumSet<>(clazz, bitset); | ||
} | ||
public static <E extends Enum<E>> ImmutableEnumSet<E> allOff(Class<E> clazz) { | ||
return of(clazz, clazz.getEnumConstants()); | ||
} | ||
|
||
private static <E extends Enum<E>> void checkClass(Class<E> clazz) { | ||
if(!clazz.isEnum()) { | ||
throw new IllegalArgumentException(String.format("Class %s is not an enum", clazz.getName())); | ||
} | ||
} | ||
|
||
public boolean isEmpty() { | ||
return bitset == 0; | ||
} | ||
|
||
public boolean contains(T value) { | ||
return BitSetUtils.get(bitset, value.ordinal()); | ||
} | ||
|
||
public Set<T> values() { | ||
var set = new HashSet<T>(); | ||
for(var e: clazz.getEnumConstants()){ | ||
if(BitSetUtils.get(bitset, e.ordinal())) { | ||
set.add(e); | ||
} | ||
} | ||
return set; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
var builder = new StringBuilder(); | ||
builder.append("["); | ||
for(var e: clazz.getEnumConstants()){ | ||
if(BitSetUtils.get(bitset, e.ordinal())) { | ||
builder.append(e.name()); | ||
builder.append(","); | ||
} | ||
} | ||
builder.append("]"); | ||
return builder.toString(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
utils/src/test/java/org/opentripplanner/utils/collection/ImmutableEnumSetTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.opentripplanner.utils.collection; | ||
|
||
import static java.time.DayOfWeek.FRIDAY; | ||
import static java.time.DayOfWeek.MONDAY; | ||
import static java.time.DayOfWeek.SATURDAY; | ||
import static java.time.DayOfWeek.SUNDAY; | ||
import static java.time.DayOfWeek.THURSDAY; | ||
import static java.time.DayOfWeek.TUESDAY; | ||
import static java.time.DayOfWeek.WEDNESDAY; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.time.DayOfWeek; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ImmutableEnumSetTest { | ||
|
||
@Test | ||
void contains(){ | ||
var set = ImmutableEnumSet.of(DayOfWeek.class, MONDAY, WEDNESDAY); | ||
assertFalse(set.isEmpty()); | ||
assertTrue(set.contains(MONDAY)); | ||
assertFalse(set.contains(TUESDAY)); | ||
assertTrue(set.contains(WEDNESDAY)); | ||
assertFalse(set.contains(THURSDAY)); | ||
assertFalse(set.contains(FRIDAY)); | ||
assertFalse(set.contains(SATURDAY)); | ||
assertFalse(set.contains(SUNDAY)); | ||
} | ||
|
||
@Test | ||
void allOf(){ | ||
var set = ImmutableEnumSet.allOff(DayOfWeek.class); | ||
assertFalse(set.isEmpty()); | ||
assertTrue(set.contains(MONDAY)); | ||
assertTrue(set.contains(TUESDAY)); | ||
assertTrue(set.contains(WEDNESDAY)); | ||
assertTrue(set.contains(THURSDAY)); | ||
assertTrue(set.contains(FRIDAY)); | ||
assertTrue(set.contains(SATURDAY)); | ||
assertTrue(set.contains(SUNDAY)); | ||
} | ||
|
||
@Test | ||
void empty(){ | ||
assertFalse(ImmutableEnumSet.of(DayOfWeek.class, SUNDAY).isEmpty()); | ||
assertTrue(ImmutableEnumSet.of(DayOfWeek.class).isEmpty()); | ||
} | ||
|
||
@Test | ||
void string(){ | ||
var set = ImmutableEnumSet.of(DayOfWeek.class, SUNDAY, MONDAY, THURSDAY); | ||
assertEquals("[MONDAY,THURSDAY,SUNDAY,]", set.toString()); | ||
} | ||
|
||
@Test | ||
void values(){ | ||
var set = ImmutableEnumSet.of(DayOfWeek.class, SUNDAY, THURSDAY, MONDAY).values(); | ||
assertEquals(Set.of(SUNDAY, MONDAY, THURSDAY), set); | ||
} | ||
} |