@Test public void attributeIn() { MutableList<String> upperList = Lists.fixedSize.of("A", "B"); Assert.assertTrue( Predicates2.attributeIn(StringFunctions.toUpperCase()).accept("a", upperList)); Assert.assertFalse( Predicates2.attributeIn(StringFunctions.toUpperCase()).accept("c", upperList)); MutableList<String> lowerList = Lists.fixedSize.of("a", "c"); MutableList<String> newList = ListIterate.filterWith( lowerList, Predicates2.attributeIn(StringFunctions.toUpperCase()), upperList); Assert.assertEquals(FastList.newListWith("a"), newList); }
@Test public void attributeNotIn() { Function<String, String> function = StringFunctions.toLowerCase(); MutableList<String> lowerList = Lists.fixedSize.of("a", "b"); Assert.assertFalse(Predicates2.attributeNotIn(function).accept("A", lowerList)); Assert.assertTrue(Predicates2.attributeNotIn(function).accept("C", lowerList)); MutableList<String> upperList = Lists.fixedSize.of("A", "C"); MutableList<String> newList = ListIterate.filterNotWith(upperList, Predicates2.attributeNotIn(function), lowerList); Assert.assertEquals(FastList.newListWith("A"), newList); }
/** * Converts a java.util.Map<String, String> into a String array of * (key,value,key,value,key,value...etc)<br> * If you supply values for exclusionPatterns, they will be treated as regular expressions and * each one will be matched against every key.<br> * If any of the patterns match, the key (and its value) will be left out of the resulting array. * <br> * Supplying an empty string ("") will cause everything to be excluded.<br> * If you want to include everything in the map, do not supply any exclusion patterns.<br> * * @param properties The map of strings to strings to convert * @param exclusionPatterns A list of patterns or keys to ignore * @return The resulting string array. It will have an even length. */ public static String[] getStringArray( Map<String, String> properties, String... exclusionPatterns) { Vector<String> items = new Vector<String>(); if (properties == null) { return new String[0]; } for (String key : properties.keySet()) { boolean notIncluded = false; for (String p : exclusionPatterns) { if (Regex.stringToMatcher(p, key).find()) { notIncluded = true; } } if (notIncluded == false) { items.add(key); items.add(StringFunctions.ifNull(properties.get(key))); } } return items.toArray(new String[0]); }