From e768a3bcd80cd35f06fca377321a6c5897252f8a Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 25 Feb 2011 07:57:59 +0100 Subject: [PATCH] Add support of Multisets in KeyValueFormat --- .../org/sonar/api/utils/KeyValueFormat.java | 328 ++++++++++-------- .../sonar/api/utils/KeyValueFormatTest.java | 59 +++- 2 files changed, 224 insertions(+), 163 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java index cd44e7a9ba7..e2d198fc387 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValueFormat.java @@ -19,10 +19,10 @@ */ package org.sonar.api.utils; +import com.google.common.collect.LinkedHashMultiset; +import com.google.common.collect.Maps; import com.google.common.collect.Multimap; import com.google.common.collect.Multiset; -import com.google.common.collect.SortedSetMultimap; -import com.google.common.collect.TreeMultimap; import org.apache.commons.collections.Bag; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; @@ -35,28 +35,24 @@ import java.text.SimpleDateFormat; import java.util.*; /** - * Util class to format key/value data. Output is a string representation ready to be - * injected into the database + * Formats and parses key/value pairs with the string representation : "key1=value1;key2=value2". Conversion + * of fields is supported and can be extended. * * @since 1.10 */ -public final class KeyValueFormat { +public final class KeyValueFormat { public static final String PAIR_SEPARATOR = ";"; public static final String FIELD_SEPARATOR = "="; - private Converter keyConverter; - private Converter valueConverter; - - private KeyValueFormat(Converter keyConverter, Converter valueConverter) { - this.keyConverter = keyConverter; - this.valueConverter = valueConverter; + private KeyValueFormat() { + // only static methods } public static abstract class Converter { - abstract String toString(TYPE type); + abstract String format(TYPE type); - abstract TYPE fromString(String s); + abstract TYPE parse(String s); } public static final class StringConverter extends Converter { @@ -66,12 +62,12 @@ public final class KeyValueFormat { } @Override - String toString(String s) { + String format(String s) { return s; } @Override - String fromString(String s) { + String parse(String s) { return s; } } @@ -83,29 +79,29 @@ public final class KeyValueFormat { } @Override - String toString(Integer s) { + String format(Integer s) { return (s == null ? "" : String.valueOf(s)); } @Override - Integer fromString(String s) { + Integer parse(String s) { return StringUtils.isBlank(s) ? null : NumberUtils.toInt(s); } } - public static final class SeverityConverter extends Converter { - static final SeverityConverter INSTANCE = new SeverityConverter(); + public static final class PriorityConverter extends Converter { + static final PriorityConverter INSTANCE = new PriorityConverter(); - private SeverityConverter() { + private PriorityConverter() { } @Override - String toString(RulePriority s) { + String format(RulePriority s) { return (s == null ? "" : s.toString()); } @Override - RulePriority fromString(String s) { + RulePriority parse(String s) { return StringUtils.isBlank(s) ? null : RulePriority.valueOf(s); } } @@ -117,12 +113,12 @@ public final class KeyValueFormat { } @Override - String toString(Double d) { + String format(Double d) { return (d == null ? "" : String.valueOf(d)); } @Override - Double fromString(String s) { + Double parse(String s) { return StringUtils.isBlank(s) ? null : NumberUtils.toDouble(s); } } @@ -139,12 +135,12 @@ public final class KeyValueFormat { } @Override - String toString(Date d) { + String format(Date d) { return (d == null ? "" : dateFormat.format(d)); } @Override - Date fromString(String s) { + Date parse(String s) { try { return StringUtils.isBlank(s) ? null : dateFormat.parse(s); } catch (ParseException e) { @@ -159,95 +155,101 @@ public final class KeyValueFormat { } } - public static KeyValueFormat create(Converter keyConverter, Converter valueConverter) { - return new KeyValueFormat(keyConverter, valueConverter); - } - - public static KeyValueFormat createStringString() { - return new KeyValueFormat(StringConverter.INSTANCE, StringConverter.INSTANCE); - } - - public static KeyValueFormat createStringDate() { - return new KeyValueFormat(StringConverter.INSTANCE, new DateConverter()); - } - - public static KeyValueFormat createStringDateTime() { - return new KeyValueFormat(StringConverter.INSTANCE, new DateTimeConverter()); - } - - public static KeyValueFormat createIntString() { - return new KeyValueFormat(IntegerConverter.INSTANCE, StringConverter.INSTANCE); + public static Map parse(String data, Converter keyConverter, Converter valueConverter) { + Map map = Maps.newLinkedHashMap(); + if (data != null) { + String[] pairs = StringUtils.split(data, PAIR_SEPARATOR); + for (String pair : pairs) { + String[] keyValue = StringUtils.split(pair, FIELD_SEPARATOR); + String key = keyValue[0]; + String value = (keyValue.length == 2 ? keyValue[1] : ""); + map.put(keyConverter.parse(key), valueConverter.parse(value)); + } + } + return map; } - public static KeyValueFormat createIntInt() { - return new KeyValueFormat(IntegerConverter.INSTANCE, IntegerConverter.INSTANCE); + public static Map parse(String data) { + return parse(data, StringConverter.INSTANCE, StringConverter.INSTANCE); } - public static KeyValueFormat createIntDouble() { - return new KeyValueFormat(IntegerConverter.INSTANCE, DoubleConverter.INSTANCE); + /** + * @since 2.7 + */ + public static Map parseStringInt(String data) { + return parse(data, StringConverter.INSTANCE, IntegerConverter.INSTANCE); } - public static KeyValueFormat createIntDate() { - return new KeyValueFormat(IntegerConverter.INSTANCE, new DateConverter()); + /** + * @since 2.7 + */ + public static Map parseStringDouble(String data) { + return parse(data, StringConverter.INSTANCE, DoubleConverter.INSTANCE); } - public static KeyValueFormat createIntDateTime() { - return new KeyValueFormat(IntegerConverter.INSTANCE, new DateTimeConverter()); + /** + * @since 2.7 + */ + public static Map parseIntString(String data) { + return parse(data, IntegerConverter.INSTANCE, StringConverter.INSTANCE); } - public String toString(Map map) { - return toString(map.entrySet()); + /** + * @since 2.7 + */ + public static Map parseIntDouble(String data) { + return parse(data, IntegerConverter.INSTANCE, DoubleConverter.INSTANCE); } - public String toString(Multimap multimap) { - return toString(multimap.entries()); + /** + * @since 2.7 + */ + public static Map parseIntDate(String data) { + return parse(data, IntegerConverter.INSTANCE, new DateConverter()); } - private String toString(Collection> entries) { - StringBuilder sb = new StringBuilder(); - boolean first = true; - for (Map.Entry entry : entries) { - if (!first) { - sb.append(PAIR_SEPARATOR); - } - sb.append(keyConverter.toString(entry.getKey())); - sb.append(FIELD_SEPARATOR); - if (entry.getValue() != null) { - sb.append(valueConverter.toString(entry.getValue())); - } - first = false; - } - return sb.toString(); + /** + * @since 2.7 + */ + public static Map parseIntDateTime(String data) { + return parse(data, IntegerConverter.INSTANCE, new DateTimeConverter()); } - public SortedMap toSortedMap(String data) { - SortedMap map = new TreeMap(); + /** + * Value of pairs is the occurrences of the same single key. A multiset is sometimes called a bag. + * For example parsing "foo=2;bar=1" creates a multiset with 3 elements : foo, foo and bar. + */ + /** + * @since 2.7 + */ + public static Multiset parseMultiset(String data, Converter keyConverter) { + Multiset multiset = LinkedHashMultiset.create();// to keep the same order if (data != null) { String[] pairs = StringUtils.split(data, PAIR_SEPARATOR); for (String pair : pairs) { String[] keyValue = StringUtils.split(pair, FIELD_SEPARATOR); String key = keyValue[0]; - String value = (keyValue.length == 2 ? keyValue[1] : ""); - map.put(keyConverter.fromString(key), valueConverter.fromString(value)); + String value = (keyValue.length == 2 ? keyValue[1] : "0"); + multiset.add(keyConverter.parse(key), IntegerConverter.INSTANCE.parse(value)); } } - return map; + return multiset; } - public SortedSetMultimap toSortedMultimap(String data) { - SortedSetMultimap map = TreeMultimap.create(); - if (data != null) { - String[] pairs = StringUtils.split(data, PAIR_SEPARATOR); - for (String pair : pairs) { - String[] keyValue = StringUtils.split(pair, FIELD_SEPARATOR); - String key = keyValue[0]; - String value = (keyValue.length == 2 ? keyValue[1] : ""); - map.put(keyConverter.fromString(key), valueConverter.fromString(value)); - } - } - return map; + + /** + * @since 2.7 + */ + public static Multiset parseIntegerMultiset(String data) { + return parseMultiset(data, IntegerConverter.INSTANCE); } + /** + * @since 2.7 + */ + public static Multiset parseMultiset(String data) { + return parseMultiset(data, StringConverter.INSTANCE); + } /** * Transforms a string with the following format : "key1=value1;key2=value2..." @@ -256,7 +258,7 @@ public final class KeyValueFormat { * @param data the input string * @param transformer the interface to implement * @return a Map of - * @deprecated since 2.7. Use instance methods instead of static methods, for example KeyValueFormat.createIntString().parse(String) + * @deprecated since 2.7 */ @Deprecated public static Map parse(String data, Transformer transformer) { @@ -271,53 +273,109 @@ public final class KeyValueFormat { return map; } - /** - * Transforms a string with the following format : "key1=value1;key2=value2..." - * into a Map - * - * @param data the string to parse - * @return a map - * @deprecated since 2.7. Use instance methods instead of static methods, for example KeyValueFormat.createIntString().parse(String) - */ - @Deprecated - public static Map parse(String data) { - Map map = new HashMap(); - String[] pairs = StringUtils.split(data, PAIR_SEPARATOR); - for (String pair : pairs) { - String[] keyValue = StringUtils.split(pair, FIELD_SEPARATOR); - String key = keyValue[0]; - String value = (keyValue.length == 2 ? keyValue[1] : ""); - map.put(key, value); - } - return map; - } - - /** - * Transforms a map into a string with the format : "key1=value1;key2=value2..." - * - * @param map the map to transform - * @return the formatted string - * @deprecated since 2.7. Use instance methods instead of static methods, for example KeyValueFormat.createIntString().parse(String) - */ - @Deprecated - public static String format(Map map) { + private static String formatEntries(Collection> entries, Converter keyConverter, Converter valueConverter) { StringBuilder sb = new StringBuilder(); boolean first = true; - for (Map.Entry entry : map.entrySet()) { + for (Map.Entry entry : entries) { if (!first) { sb.append(PAIR_SEPARATOR); } - sb.append(entry.getKey().toString()); + sb.append(keyConverter.format(entry.getKey())); sb.append(FIELD_SEPARATOR); if (entry.getValue() != null) { - sb.append(entry.getValue()); + sb.append(valueConverter.format(entry.getValue())); } first = false; } + return sb.toString(); + } + private static String formatEntries(Set> entries, Converter keyConverter) { + StringBuilder sb = new StringBuilder(); + boolean first = true; + for (Multiset.Entry entry : entries) { + if (!first) { + sb.append(PAIR_SEPARATOR); + } + sb.append(keyConverter.format(entry.getElement())); + sb.append(FIELD_SEPARATOR); + sb.append(IntegerConverter.INSTANCE.format(entry.getCount())); + first = false; + } return sb.toString(); } + + /** + * @since 2.7 + */ + public static String format(Map map, Converter keyConverter, Converter valueConverter) { + return formatEntries(map.entrySet(), keyConverter, valueConverter); + } + + /** + * @since 2.7 + */ + public static String format(Map map) { + return format(map, StringConverter.INSTANCE, StringConverter.INSTANCE); + } + + /** + * @since 2.7 + */ + public static String formatIntString(Map map) { + return format(map, IntegerConverter.INSTANCE, StringConverter.INSTANCE); + } + + /** + * @since 2.7 + */ + public static String formatIntDouble(Map map) { + return format(map, IntegerConverter.INSTANCE, DoubleConverter.INSTANCE); + } + + /** + * @since 2.7 + */ + public static String formatIntDate(Map map) { + return format(map, IntegerConverter.INSTANCE, new DateConverter()); + } + + /** + * @since 2.7 + */ + public static String formatIntDateTime(Map map) { + return format(map, IntegerConverter.INSTANCE, new DateTimeConverter()); + } + + /** + * @since 2.7 + */ + public static String formatStringInt(Map map) { + return format(map, StringConverter.INSTANCE, IntegerConverter.INSTANCE); + } + + /** + * Limitation: there's currently no methods to parse into Multimap. + * @since 2.7 + */ + public static String format(Multimap map, Converter keyConverter, Converter valueConverter) { + return formatEntries(map.entries(), keyConverter, valueConverter); + } + + /** + * @since 2.7 + */ + public static String format(Multiset multiset, Converter keyConverter) { + return formatEntries(multiset.entrySet(), keyConverter); + } + + public static String format(Multiset multiset) { + return formatEntries(multiset.entrySet(), StringConverter.INSTANCE); + } + + + /** * @since 1.11 * @deprecated use Multiset from google collections instead of commons-collections bags @@ -349,37 +407,13 @@ public final class KeyValueFormat { return sb.toString(); } - /** - * Transforms a Multiset into a string with the format : "key1=count1;key2=count2..." - * - * @param set the set to transform - * @return the formatted string - * @deprecated since 2.7. Use instance methods instead of static methods, for example KeyValueFormat.createIntString().parse(String) - */ - @Deprecated - public static String format(Multiset set) { - StringBuilder sb = new StringBuilder(); - if (set != null) { - boolean first = true; - for (Multiset.Entry entry : set.entrySet()) { - if (!first) { - sb.append(PAIR_SEPARATOR); - } - sb.append(entry.getElement().toString()); - sb.append(FIELD_SEPARATOR); - sb.append(entry.getCount()); - first = false; - } - } - return sb.toString(); - } /** * Transforms a Object... into a string with the format : "object1=object2;object3=object4..." * * @param objects the object list to transform * @return the formatted string - * @deprecated since 2.7. Use instance methods instead of static methods, for example KeyValueFormat.createIntString().parse(String) + * @deprecated since 2.7 because there's not the inverse method to parse. */ @Deprecated public static String format(Object... objects) { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java index 58b2fa9bf53..74d7cf28b0e 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/KeyValueFormatTest.java @@ -19,7 +19,9 @@ */ package org.sonar.api.utils; +import com.google.common.collect.LinkedHashMultiset; import com.google.common.collect.Maps; +import com.google.common.collect.Multiset; import org.junit.Test; import org.sonar.api.rules.RulePriority; @@ -27,7 +29,6 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; -import java.util.SortedMap; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; @@ -40,7 +41,7 @@ public class KeyValueFormatTest { Map map = Maps.newLinkedHashMap(); map.put("lucky", "luke"); map.put("aste", "rix"); - String s = KeyValueFormat.createStringString().toString(map); + String s = KeyValueFormat.format(map); assertThat(s, is("lucky=luke;aste=rix"));// same order } @@ -49,7 +50,7 @@ public class KeyValueFormatTest { Map map = Maps.newLinkedHashMap(); map.put(3, "three"); map.put(5, "five"); - String s = KeyValueFormat.createIntString().toString(map); + String s = KeyValueFormat.formatIntString(map); assertThat(s, is("3=three;5=five"));// same order } @@ -58,7 +59,7 @@ public class KeyValueFormatTest { Map map = Maps.newLinkedHashMap(); map.put(13, 2.0); map.put(5, 5.75); - String s = KeyValueFormat.createIntDouble().toString(map); + String s = KeyValueFormat.formatIntDouble(map); assertThat(s, is("13=2.0;5=5.75"));// same order } @@ -67,14 +68,14 @@ public class KeyValueFormatTest { Map map = Maps.newLinkedHashMap(); map.put(13, null); map.put(5, 5.75); - String s = KeyValueFormat.createIntDouble().toString(map); + String s = KeyValueFormat.formatIntDouble(map); assertThat(s, is("13=;5=5.75"));// same order } @Test public void shouldFormatBlank() { Map map = Maps.newTreeMap(); - String s = KeyValueFormat.createIntString().toString(map); + String s = KeyValueFormat.formatIntString(map); assertThat(s, is("")); } @@ -84,13 +85,13 @@ public class KeyValueFormatTest { map.put(4, new SimpleDateFormat("yyyy-MM-dd").parse("2010-12-25")); map.put(20, new SimpleDateFormat("yyyy-MM-dd").parse("2009-05-28")); map.put(12, null); - String s = KeyValueFormat.createIntDate().toString(map); + String s = KeyValueFormat.formatIntDate(map); assertThat(s, is("4=2010-12-25;20=2009-05-28;12=")); } @Test public void shouldParseStrings() throws ParseException { - SortedMap map = KeyValueFormat.createStringString().toSortedMap("one=un;two=deux"); + Map map = KeyValueFormat.parse("one=un;two=deux"); assertThat(map.size(), is(2)); assertThat(map.get("one"), is("un")); assertThat(map.get("two"), is("deux")); @@ -99,19 +100,19 @@ public class KeyValueFormatTest { @Test public void shouldParseBlank() throws ParseException { - SortedMap map = KeyValueFormat.createStringString().toSortedMap(""); + Map map = KeyValueFormat.parse(""); assertThat(map.size(), is(0)); } @Test public void shouldParseNull() throws ParseException { - SortedMap map = KeyValueFormat.createStringString().toSortedMap(null); + Map map = KeyValueFormat.parse(null); assertThat(map.size(), is(0)); } @Test public void shouldParseEmptyFields() throws ParseException { - SortedMap map = KeyValueFormat.createIntDouble().toSortedMap("4=4.2;2=;6=6.68"); + Map map = KeyValueFormat.parseIntDouble("4=4.2;2=;6=6.68"); assertThat(map.size(), is(3)); assertThat(map.get(4), is(4.2)); assertThat(map.get(2), nullValue()); @@ -119,11 +120,37 @@ public class KeyValueFormatTest { } @Test - public void shouldConvertSeverity() { - assertThat(KeyValueFormat.SeverityConverter.INSTANCE.toString(RulePriority.BLOCKER), is("BLOCKER")); - assertThat(KeyValueFormat.SeverityConverter.INSTANCE.toString(null), is("")); + public void shouldConvertPriority() { + assertThat(KeyValueFormat.PriorityConverter.INSTANCE.format(RulePriority.BLOCKER), is("BLOCKER")); + assertThat(KeyValueFormat.PriorityConverter.INSTANCE.format(null), is("")); - assertThat(KeyValueFormat.SeverityConverter.INSTANCE.fromString("MAJOR"), is(RulePriority.MAJOR)); - assertThat(KeyValueFormat.SeverityConverter.INSTANCE.fromString(""), nullValue()); + assertThat(KeyValueFormat.PriorityConverter.INSTANCE.parse("MAJOR"), is(RulePriority.MAJOR)); + assertThat(KeyValueFormat.PriorityConverter.INSTANCE.parse(""), nullValue()); + } + + @Test + public void shouldFormatMultiset() { + Multiset set = LinkedHashMultiset.create(); + set.add("foo"); + set.add("foo"); + set.add("bar"); + assertThat(KeyValueFormat.format(set), is("foo=2;bar=1")); + } + + @Test + public void shouldParseMultiset() { + Multiset multiset = KeyValueFormat.parseMultiset("foo=2;bar=1;none="); + assertThat(multiset.count("foo"), is(2)); + assertThat(multiset.count("bar"), is(1)); + assertThat(multiset.count("none"), is(0)); + assertThat(multiset.contains("none"), is(false)); + } + + @Test + public void shouldKeepOrderWhenParsingMultiset() { + Multiset multiset = KeyValueFormat.parseMultiset("foo=2;bar=1"); + + // first one is foo + assertThat(multiset.iterator().next(), is("foo")); } } -- 2.39.5