Skip to content

Commit 835888e

Browse files
author
Martin
committed
added isEnumMapped() and isValueMapped() functions
this fixes #5
1 parent 0b14dbc commit 835888e

File tree

4 files changed

+84
-9
lines changed

4 files changed

+84
-9
lines changed

enum-mapper-lib/src/main/java/com/tmtron/enums/EnumMapperBase.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,31 @@ public K getEnumOrNull(@Nonnull V value) {
6868
for (K key : enumMap.keySet()) {
6969
V lookupValue = enumMap.get(key);
7070
if (value.equals(lookupValue)) {
71+
assert isValueMapped(value);
7172
return key;
7273
}
7374
}
7475
return null;
7576
}
7677

78+
/**
79+
* @param value the mapped value
80+
* @return {@code true} when the given value is mapped by one or more Enum constants, {@code false} otherwise
81+
*/
82+
public boolean isValueMapped(@Nonnull V value) {
83+
checkNonnull(value, "value must not be null");
84+
return enumMap.containsValue(value);
85+
}
86+
87+
/**
88+
* @param enumConst the enumeration constant
89+
* @return {@code true} when the enumConst is mapped to a value, {@code false} otherwise
90+
*/
91+
public boolean isEnumMapped(@Nonnull K enumConst) {
92+
checkNonnull(enumConst, "enumConst must not be null");
93+
return enumMap.containsKey(enumConst);
94+
}
95+
7796
public static <T> T checkNonnull(@Nullable T obj, @Nonnull String msg) {
7897
if (obj == null) throw new IllegalArgumentException(msg);
7998
return obj;

enum-mapper-lib/src/main/java/com/tmtron/enums/EnumMapperFull.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ V getValue(@Nonnull K enumValue) {
219219
return result;
220220
}
221221

222+
/**
223+
* @param enumConst the enumeration constant
224+
* @return always returns {@code true}, because {@link EnumMapperFull} guarantees that all enum-constants are mapped
225+
*/
226+
@Override
227+
public boolean isEnumMapped(@Nonnull K enumConst) {
228+
assert super.isEnumMapped(enumConst);
229+
return true;
230+
}
231+
222232
public static class Builder<K extends Enum<K>, V> extends BuilderBase<K, V> {
223233

224234
private Builder(@Nonnull K key) {

enum-mapper-lib/src/test/java/com/tmtron/enums/TestEnumMapperFull.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import static com.tmtron.enums.TestEnumMapperFull.Nine.I8;
2828
import static com.tmtron.enums.TestEnumMapperFull.Nine.I9;
2929
import static org.junit.Assert.assertEquals;
30+
import static org.junit.Assert.assertFalse;
3031
import static org.junit.Assert.assertNull;
32+
import static org.junit.Assert.assertTrue;
3133

3234
public class TestEnumMapperFull {
3335

@@ -38,7 +40,7 @@ enum Nine {I1, I2, I3, I4, I5, I6, I7, I8, I9}
3840

3941
@Test(expected = IllegalArgumentException.class)
4042
public void testMapperOfThrowsOnMissingValues2() {
41-
EnumMapperFull.of(I1, 1, Nine.I2, 2);
43+
EnumMapperFull<Nine, Integer> xx = EnumMapperFull.of(I1, 1, Nine.I2, 2);
4244
}
4345

4446
@Test(expected = IllegalArgumentException.class)
@@ -94,4 +96,37 @@ public void testGetOrRaise() throws Exception {
9496
fullMapper.getEnumOrRaise(77);
9597
}
9698

99+
@Test
100+
public void testIsEnumMapped() {
101+
assertTrue(fullMapper.isEnumMapped(I1));
102+
assertTrue(fullMapper.isEnumMapped(I2));
103+
assertTrue(fullMapper.isEnumMapped(I3));
104+
assertTrue(fullMapper.isEnumMapped(I4));
105+
assertTrue(fullMapper.isEnumMapped(I5));
106+
assertTrue(fullMapper.isEnumMapped(I6));
107+
assertTrue(fullMapper.isEnumMapped(I7));
108+
assertTrue(fullMapper.isEnumMapped(I8));
109+
assertTrue(fullMapper.isEnumMapped(I9));
110+
}
111+
112+
@Test
113+
public void testIsValueMapped() {
114+
assertTrue(fullMapper.isValueMapped(1));
115+
assertTrue(fullMapper.isValueMapped(2));
116+
assertTrue(fullMapper.isValueMapped(3));
117+
assertTrue(fullMapper.isValueMapped(4));
118+
assertTrue(fullMapper.isValueMapped(5));
119+
assertTrue(fullMapper.isValueMapped(6));
120+
assertTrue(fullMapper.isValueMapped(7));
121+
assertTrue(fullMapper.isValueMapped(8));
122+
assertTrue(fullMapper.isValueMapped(9));
123+
}
124+
125+
@Test
126+
public void testIsValueMapped_returns_false() {
127+
assertFalse(fullMapper.isValueMapped(-1));
128+
assertFalse(fullMapper.isValueMapped(0));
129+
assertFalse(fullMapper.isValueMapped(10));
130+
}
131+
97132
}

enum-mapper-lib/src/test/java/com/tmtron/enums/TestEnumMapperPartial.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,32 @@
2727
import static com.tmtron.enums.TestEnumMapperPartial.Nine.I8;
2828
import static com.tmtron.enums.TestEnumMapperPartial.Nine.I9;
2929
import static org.junit.Assert.assertEquals;
30+
import static org.junit.Assert.assertFalse;
3031
import static org.junit.Assert.assertNotNull;
3132
import static org.junit.Assert.assertNull;
33+
import static org.junit.Assert.assertTrue;
3234

3335
public class TestEnumMapperPartial {
3436

3537
enum Nine {I1, I2, I3, I4, I5, I6, I7, I8, I9}
3638

39+
final EnumMapperFull<Nine, Integer> fullMapper = EnumMapperFull.of(I1, 1, Nine.I2, 2, I3, 3
40+
, I4, 4, I5, 5, I6, 6, I7, 7, I8, 8, I9, 9);
41+
3742
private void assertMapping(EnumMapperPartial<Nine, Integer> mapper, int value, boolean expectedToBeOkay) {
3843
Nine enumResult = mapper.getEnumOrNull(value);
3944
if (expectedToBeOkay) {
4045
assertNotNull(enumResult);
4146
assertEquals(Integer.valueOf(value), mapper.getValueOrNull(enumResult));
47+
assertTrue(mapper.isEnumMapped(enumResult));
48+
assertTrue(mapper.isValueMapped(value));
49+
4250
} else {
4351
assertNull(enumResult);
4452
assertEquals(I9, mapper.getEnumOrDefault(value, I9));
53+
assertFalse(mapper.isValueMapped(value));
54+
Nine nonMappedEnum = fullMapper.getEnumOrRaise(value);
55+
assertFalse(mapper.isEnumMapped(nonMappedEnum));
4556
}
4657
}
4758

@@ -54,18 +65,18 @@ private void assertMapper(int upperLimitIncluded, EnumMapperPartial<Nine, Intege
5465

5566
@Test
5667
public void testMapperof() {
57-
assertMapper(2, EnumMapperPartial.of(I1, 1, Nine.I2, 2));
58-
assertMapper(3, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3));
59-
assertMapper(4, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4));
60-
assertMapper(5, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
68+
assertMapper(2, EnumMapperPartial.of(I1, 1, I2, 2));
69+
assertMapper(3, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3));
70+
assertMapper(4, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4));
71+
assertMapper(5, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
6172
, I5, 5));
62-
assertMapper(6, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
73+
assertMapper(6, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
6374
, I5, 5, I6, 6));
64-
assertMapper(7, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
75+
assertMapper(7, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
6576
, I5, 5, I6, 6, I7, 7));
66-
assertMapper(8, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
77+
assertMapper(8, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
6778
, I5, 5, I6, 6, I7, 7, I8, 8));
68-
assertMapper(9, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
79+
assertMapper(9, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
6980
, I5, 5, I6, 6, I7, 7, I8, 8, I9, 9));
7081
}
7182

0 commit comments

Comments
 (0)