Skip to content

Commit

Permalink
added isEnumMapped() and isValueMapped() functions
Browse files Browse the repository at this point in the history
this fixes #5
  • Loading branch information
Martin committed Jun 5, 2017
1 parent 0b14dbc commit 835888e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
19 changes: 19 additions & 0 deletions enum-mapper-lib/src/main/java/com/tmtron/enums/EnumMapperBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,31 @@ public K getEnumOrNull(@Nonnull V value) {
for (K key : enumMap.keySet()) {
V lookupValue = enumMap.get(key);
if (value.equals(lookupValue)) {
assert isValueMapped(value);
return key;
}
}
return null;
}

/**
* @param value the mapped value
* @return {@code true} when the given value is mapped by one or more Enum constants, {@code false} otherwise
*/
public boolean isValueMapped(@Nonnull V value) {
checkNonnull(value, "value must not be null");
return enumMap.containsValue(value);
}

/**
* @param enumConst the enumeration constant
* @return {@code true} when the enumConst is mapped to a value, {@code false} otherwise
*/
public boolean isEnumMapped(@Nonnull K enumConst) {
checkNonnull(enumConst, "enumConst must not be null");
return enumMap.containsKey(enumConst);
}

public static <T> T checkNonnull(@Nullable T obj, @Nonnull String msg) {
if (obj == null) throw new IllegalArgumentException(msg);
return obj;
Expand Down
10 changes: 10 additions & 0 deletions enum-mapper-lib/src/main/java/com/tmtron/enums/EnumMapperFull.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ V getValue(@Nonnull K enumValue) {
return result;
}

/**
* @param enumConst the enumeration constant
* @return always returns {@code true}, because {@link EnumMapperFull} guarantees that all enum-constants are mapped
*/
@Override
public boolean isEnumMapped(@Nonnull K enumConst) {
assert super.isEnumMapped(enumConst);
return true;
}

public static class Builder<K extends Enum<K>, V> extends BuilderBase<K, V> {

private Builder(@Nonnull K key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import static com.tmtron.enums.TestEnumMapperFull.Nine.I8;
import static com.tmtron.enums.TestEnumMapperFull.Nine.I9;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class TestEnumMapperFull {

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

@Test(expected = IllegalArgumentException.class)
public void testMapperOfThrowsOnMissingValues2() {
EnumMapperFull.of(I1, 1, Nine.I2, 2);
EnumMapperFull<Nine, Integer> xx = EnumMapperFull.of(I1, 1, Nine.I2, 2);
}

@Test(expected = IllegalArgumentException.class)
Expand Down Expand Up @@ -94,4 +96,37 @@ public void testGetOrRaise() throws Exception {
fullMapper.getEnumOrRaise(77);
}

@Test
public void testIsEnumMapped() {
assertTrue(fullMapper.isEnumMapped(I1));
assertTrue(fullMapper.isEnumMapped(I2));
assertTrue(fullMapper.isEnumMapped(I3));
assertTrue(fullMapper.isEnumMapped(I4));
assertTrue(fullMapper.isEnumMapped(I5));
assertTrue(fullMapper.isEnumMapped(I6));
assertTrue(fullMapper.isEnumMapped(I7));
assertTrue(fullMapper.isEnumMapped(I8));
assertTrue(fullMapper.isEnumMapped(I9));
}

@Test
public void testIsValueMapped() {
assertTrue(fullMapper.isValueMapped(1));
assertTrue(fullMapper.isValueMapped(2));
assertTrue(fullMapper.isValueMapped(3));
assertTrue(fullMapper.isValueMapped(4));
assertTrue(fullMapper.isValueMapped(5));
assertTrue(fullMapper.isValueMapped(6));
assertTrue(fullMapper.isValueMapped(7));
assertTrue(fullMapper.isValueMapped(8));
assertTrue(fullMapper.isValueMapped(9));
}

@Test
public void testIsValueMapped_returns_false() {
assertFalse(fullMapper.isValueMapped(-1));
assertFalse(fullMapper.isValueMapped(0));
assertFalse(fullMapper.isValueMapped(10));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,32 @@
import static com.tmtron.enums.TestEnumMapperPartial.Nine.I8;
import static com.tmtron.enums.TestEnumMapperPartial.Nine.I9;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class TestEnumMapperPartial {

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

final EnumMapperFull<Nine, Integer> fullMapper = EnumMapperFull.of(I1, 1, Nine.I2, 2, I3, 3
, I4, 4, I5, 5, I6, 6, I7, 7, I8, 8, I9, 9);

private void assertMapping(EnumMapperPartial<Nine, Integer> mapper, int value, boolean expectedToBeOkay) {
Nine enumResult = mapper.getEnumOrNull(value);
if (expectedToBeOkay) {
assertNotNull(enumResult);
assertEquals(Integer.valueOf(value), mapper.getValueOrNull(enumResult));
assertTrue(mapper.isEnumMapped(enumResult));
assertTrue(mapper.isValueMapped(value));

} else {
assertNull(enumResult);
assertEquals(I9, mapper.getEnumOrDefault(value, I9));
assertFalse(mapper.isValueMapped(value));
Nine nonMappedEnum = fullMapper.getEnumOrRaise(value);
assertFalse(mapper.isEnumMapped(nonMappedEnum));
}
}

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

@Test
public void testMapperof() {
assertMapper(2, EnumMapperPartial.of(I1, 1, Nine.I2, 2));
assertMapper(3, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3));
assertMapper(4, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4));
assertMapper(5, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
assertMapper(2, EnumMapperPartial.of(I1, 1, I2, 2));
assertMapper(3, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3));
assertMapper(4, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4));
assertMapper(5, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
, I5, 5));
assertMapper(6, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
assertMapper(6, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
, I5, 5, I6, 6));
assertMapper(7, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
assertMapper(7, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
, I5, 5, I6, 6, I7, 7));
assertMapper(8, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
assertMapper(8, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
, I5, 5, I6, 6, I7, 7, I8, 8));
assertMapper(9, EnumMapperPartial.of(I1, 1, Nine.I2, 2, I3, 3, I4, 4
assertMapper(9, EnumMapperPartial.of(I1, 1, I2, 2, I3, 3, I4, 4
, I5, 5, I6, 6, I7, 7, I8, 8, I9, 9));
}

Expand Down

0 comments on commit 835888e

Please sign in to comment.