[COLLECTIONS-726] Add lambdas function to provide default values in

MapUtils. Implementation based on the idea of PR #81. Closes #81.

- The implementation is much more concise than the PR.
- Sort methods.
- Line length 120.
- Close HTML tags in Javadoc.
This commit is contained in:
Gary Gregory 2019-08-14 15:11:56 -07:00
parent 77c4cee343
commit b1511c79aa
3 changed files with 2748 additions and 2439 deletions

View File

@ -24,6 +24,9 @@
<action issue="COLLECTIONS-724" dev="ggregory" type="updated." due-to="Eitan Adler">
Simplify two remove-if loops #77.
</action>
<action issue="COLLECTIONS-726" dev="ggregory" type="updated." due-to=" liuhaozzu, Gary Gregory">
Add lambdas function to provide default values in MapUtils #81.
</action>
</release>
<release version="4.4" date="2019-07-05" description="Maintenance release.">
<action issue="COLLECTIONS-710" dev="ggregory" type="fix" due-to="Yu Shi, Gary Gregory">

File diff suppressed because it is too large Load Diff

View File

@ -39,6 +39,7 @@ import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
import org.apache.commons.collections4.collection.TransformedCollectionTest;
import org.apache.commons.collections4.junit.AbstractAvailableLocalesTest;
@ -182,9 +183,9 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
// sub array
test = MapUtils.putAll(new HashMap<String, String>(), new String[][] {
{"RED", "#FF0000"},
{"GREEN", "#00FF00"},
{"BLUE", "#0000FF"}
{"RED", "#FF0000"},
{"GREEN", "#00FF00"},
{"BLUE", "#0000FF"}
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
@ -196,36 +197,36 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
try {
MapUtils.putAll(new HashMap<String, String>(), new String[][] {
{"RED", "#FF0000"},
null,
{"BLUE", "#0000FF"}
{"RED", "#FF0000"},
null,
{"BLUE", "#0000FF"}
});
fail();
} catch (final IllegalArgumentException ex) {}
try {
MapUtils.putAll(new HashMap<String, String>(), new String[][] {
{"RED", "#FF0000"},
{"GREEN"},
{"BLUE", "#0000FF"}
{"RED", "#FF0000"},
{"GREEN"},
{"BLUE", "#0000FF"}
});
fail();
} catch (final IllegalArgumentException ex) {}
try {
MapUtils.putAll(new HashMap<String, String>(), new String[][] {
{"RED", "#FF0000"},
{},
{"BLUE", "#0000FF"}
{"RED", "#FF0000"},
{},
{"BLUE", "#0000FF"}
});
fail();
} catch (final IllegalArgumentException ex) {}
// flat array
test = MapUtils.putAll(new HashMap<String, String>(), new String[] {
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF"
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF"
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
@ -236,10 +237,10 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals(3, test.size());
test = MapUtils.putAll(new HashMap<String, String>(), new String[] {
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF",
"PURPLE" // ignored
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF",
"PURPLE" // ignored
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
@ -254,9 +255,9 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
// map entry
test = MapUtils.putAll(new HashMap<String, String>(), new Object[] {
new DefaultMapEntry<>("RED", "#FF0000"),
new DefaultMapEntry<>("GREEN", "#00FF00"),
new DefaultMapEntry<>("BLUE", "#0000FF")
new DefaultMapEntry<>("RED", "#FF0000"),
new DefaultMapEntry<>("GREEN", "#00FF00"),
new DefaultMapEntry<>("BLUE", "#0000FF")
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
@ -268,9 +269,9 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
// key value
test = MapUtils.putAll(new HashMap<String, String>(), new Object[] {
new DefaultKeyValue<>("RED", "#FF0000"),
new DefaultKeyValue<>("GREEN", "#00FF00"),
new DefaultKeyValue<>("BLUE", "#0000FF")
new DefaultKeyValue<>("RED", "#FF0000"),
new DefaultKeyValue<>("GREEN", "#00FF00"),
new DefaultKeyValue<>("BLUE", "#0000FF")
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
@ -953,9 +954,17 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals(2.0, MapUtils.getDoubleValue(in,"key", 0.0), 0);
assertEquals(2.0, MapUtils.getDoubleValue(in,"key"), 0);
assertEquals(1.0, MapUtils.getDoubleValue(in,"noKey", 1.0), 0);
assertEquals(5.0, MapUtils.getDoubleValue(in,"noKey", (key)->{
//sometimes the default value need to be calculated,such as System.currentTimeMillis()
return 5.0D;
}),0);
assertEquals(0, MapUtils.getDoubleValue(in,"noKey"), 0);
assertEquals(2.0, MapUtils.getDouble(in,"key", 0.0), 0);
assertEquals(1.0, MapUtils.getDouble(in,"noKey", 1.0), 0);
assertEquals(1.0, MapUtils.getDouble(in,"noKey", (key)->{
return 1.0;
}), 0);
final Map<String, String> inStr = new HashMap<>();
@ -973,9 +982,15 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals(2.0, MapUtils.getFloatValue(in,"key", 0.0f), 0);
assertEquals(2.0, MapUtils.getFloatValue(in,"key"), 0);
assertEquals(1.0, MapUtils.getFloatValue(in,"noKey", 1.0f), 0);
assertEquals(1.0, MapUtils.getFloatValue(in,"noKey", (key)->{
return 1.0F;
}), 0);
assertEquals(0, MapUtils.getFloatValue(in,"noKey"), 0);
assertEquals(2.0, MapUtils.getFloat(in,"key", 0.0f), 0);
assertEquals(1.0, MapUtils.getFloat(in,"noKey", 1.0f), 0);
assertEquals(1.0, MapUtils.getFloat(in,"noKey", (key)->{
return 1.0F;
}), 0);
final Map<String, String> inStr = new HashMap<>();
final char decimalSeparator = getDecimalSeparator();
@ -992,9 +1007,15 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals(2.0, MapUtils.getLongValue(in,"key", 0L), 0);
assertEquals(2.0, MapUtils.getLongValue(in,"key"), 0);
assertEquals(1, MapUtils.getLongValue(in,"noKey", 1L), 0);
assertEquals(1, MapUtils.getLongValue(in,"noKey", (key)->{
return 1L;
}), 0);
assertEquals(0, MapUtils.getLongValue(in,"noKey"), 0);
assertEquals(2.0, MapUtils.getLong(in,"key", 0L), 0);
assertEquals(1, MapUtils.getLong(in,"noKey", 1L), 0);
assertEquals(1, MapUtils.getLong(in,"noKey", (key)->{
return 1L;
}), 0);
final Map<String, String> inStr = new HashMap<>();
inStr.put("str1", "2");
@ -1012,9 +1033,15 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals(2, MapUtils.getIntValue(in,"key", 0), 0);
assertEquals(2, MapUtils.getIntValue(in,"key"), 0);
assertEquals(0, MapUtils.getIntValue(in,"noKey", 0), 0);
assertEquals(0, MapUtils.getIntValue(in,"noKey", (key)->{
return 0;
}), 0);
assertEquals(0, MapUtils.getIntValue(in,"noKey"), 0);
assertEquals(2, MapUtils.getInteger(in,"key", 0), 0);
assertEquals(0, MapUtils.getInteger(in,"noKey", 0), 0);
assertEquals(0, MapUtils.getInteger(in,"noKey", (key)->{
return 0;
}), 0);
final Map<String, String> inStr = new HashMap<>();
inStr.put("str1", "2");
@ -1031,9 +1058,15 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals(val, MapUtils.getShortValue(in,"key", val), 0);
assertEquals(val, MapUtils.getShortValue(in,"key"), 0);
assertEquals(val, MapUtils.getShortValue(in,"noKey", val), 0);
assertEquals(val, MapUtils.getShortValue(in,"noKey", (key)->{
return val;
}), 0);
assertEquals(0, MapUtils.getShortValue(in,"noKey"), 0);
assertEquals(val, MapUtils.getShort(in,"key", val), 0);
assertEquals(val,MapUtils.getShort(in,"noKey", val), 0);
assertEquals(val,MapUtils.getShort(in,"noKey", (key)->{
return val;
}), 0);
final Map<String, String> inStr = new HashMap<>();
inStr.put("str1", "10");
@ -1050,9 +1083,15 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals(val, MapUtils.getByteValue(in,"key", val), 0);
assertEquals(val, MapUtils.getByteValue(in,"key"), 0);
assertEquals(val, MapUtils.getByteValue(in,"noKey", val), 0);
assertEquals(val, MapUtils.getByteValue(in,"noKey", (key)->{
return (byte)100;
}), 0);
assertEquals(0, MapUtils.getByteValue(in,"noKey"), 0);
assertEquals(val, MapUtils.getByte(in,"key", val), 0);
assertEquals(val, MapUtils.getByte(in,"noKey", val), 0);
assertEquals(val, MapUtils.getByte(in,"noKey", (key)->{
return val;
}), 0);
final Map<String, String> inStr = new HashMap<>();
@ -1069,6 +1108,13 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals(val.intValue(), MapUtils.getNumber(in,"key", val).intValue(), 0);
assertEquals(val.intValue(), MapUtils.getNumber(in,"noKey", val).intValue(), 0);
assertEquals(val.intValue(), MapUtils.getNumber(in,"noKey", (key)->{
if (true) {
return val;
} else {
return null;
}
}).intValue(), 0);
}
@ -1081,6 +1127,13 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals("str", MapUtils.getString(in,"key"));
assertEquals(null, MapUtils.getString(null,"key"));
assertEquals("default", MapUtils.getString(in,"noKey", "default"));
assertEquals("default", MapUtils.getString(in,"noKey", (key)->{
if ("noKey".equals(key)) {
return "default";
} else {
return "";
}
}));
assertEquals("default", MapUtils.getString(null,"noKey", "default"));
}
@ -1095,7 +1148,6 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertEquals(null, MapUtils.getObject(null,"key"));
assertEquals("default", MapUtils.getObject(in,"noKey", "default"));
assertEquals("default", MapUtils.getObject(null,"noKey", "default"));
}
@Test
@ -1106,9 +1158,19 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
assertTrue(MapUtils.getBooleanValue(in,"key", true));
assertTrue(MapUtils.getBooleanValue(in,"key"));
assertTrue(MapUtils.getBooleanValue(in,"noKey", true));
assertTrue(MapUtils.getBooleanValue(in,"noKey", (key)->{
return true;
}));
assertTrue(!MapUtils.getBooleanValue(in,"noKey"));
assertTrue(MapUtils.getBoolean(in,"key", true));
assertTrue(MapUtils.getBoolean(in,"noKey", true));
assertTrue(MapUtils.getBoolean(in,"noKey", (key)->{
if (System.currentTimeMillis() > 0) {
return true;
} else {
return false;
}
}));
assertEquals(null, MapUtils.getBoolean(null,"noKey"));
@ -1128,12 +1190,12 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
final Map<String, String> valMap = new HashMap<>();
valMap.put("key1", "value1");
in.put("key1", valMap);
final Map<?, ?> outValue = MapUtils.getMap(in,"key1", null);
final Map<?, ?> outValue = MapUtils.getMap(in,"key1", (Map<?, ?>) null);
assertEquals("value1", outValue.get("key1"));
assertEquals(null, outValue.get("key2"));
assertEquals(null, MapUtils.getMap(in,"key2", null));
assertEquals(null, MapUtils.getMap(null,"key2", null));
assertEquals(null, MapUtils.getMap(in, "key2", (Map<?, ?>) null));
assertEquals(null, MapUtils.getMap(null, "key2", (Map<?, ?>) null));
}
@Test
@ -1149,9 +1211,9 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest {
@Test
public void testOrderedMap() {
final Map<String, String> inMap = new HashMap<>();
inMap.put("key1", "value1");
inMap.put("key2", "value2");
final Map<String, String> inMap = new HashMap<>();
inMap.put("key1", "value1");
inMap.put("key2", "value2");
final Map<String, String> map = MapUtils.orderedMap(inMap);
assertTrue("returned object should be a OrderedMap", map instanceof OrderedMap);
}