org.sonar.api.utils
Class KeyValueFormat

java.lang.Object
  extended by org.sonar.api.utils.KeyValueFormat

public final class KeyValueFormat
extends Object

Formats and parses key/value pairs with the text representation : "key1=value1;key2=value2". Field typing is supported, to make conversion from/to primitive types easier for example.

Since version 4.5.1, text keys and values are escaped if they contain the separator characters '=' or ';'.

Parsing examples

   Map<String,String> mapOfStrings = KeyValueFormat.parse("hello=world;foo=bar");
   Map<String,Integer> mapOfStringInts = KeyValueFormat.parseStringInt("one=1;two=2");
   Map<Integer,String> mapOfIntStrings = KeyValueFormat.parseIntString("1=one;2=two");
   Map<String,Date> mapOfStringDates = KeyValueFormat.parseStringDate("d1=2014-01-14;d2=2015-07-28");

   // custom conversion
   Map<String,MyClass> mapOfStringMyClass = KeyValueFormat.parse("foo=xxx;bar=yyy",
     KeyValueFormat.newStringConverter(), new MyClassConverter());
 

Formatting examples

   String output = KeyValueFormat.format(map);

   Map<Integer,String> mapIntString;
   KeyValueFormat.formatIntString(mapIntString);
 

Since:
1.10

Nested Class Summary
static class KeyValueFormat.Converter<T>
           
static class KeyValueFormat.DateConverter
           
static class KeyValueFormat.DoubleConverter
           
static class KeyValueFormat.IntegerConverter
           
static class KeyValueFormat.PriorityConverter
           
static class KeyValueFormat.StringConverter
           
static class KeyValueFormat.ToStringConverter
           
 
Field Summary
static String FIELD_SEPARATOR
           
static String PAIR_SEPARATOR
           
 
Method Summary
static String format(org.apache.commons.collections.Bag bag, int var)
          Deprecated. use Multiset from google collections instead of commons-collections bags
static String format(Map map)
           
static
<K,V> String
format(Map<K,V> map, KeyValueFormat.Converter<K> keyConverter, KeyValueFormat.Converter<V> valueConverter)
           
static String format(com.google.common.collect.Multiset multiset)
           
static
<K> String
format(com.google.common.collect.Multiset<K> multiset, KeyValueFormat.Converter<K> keyConverter)
           
static String formatIntDate(Map<Integer,Date> map)
           
static String formatIntDateTime(Map<Integer,Date> map)
           
static String formatIntDouble(Map<Integer,Double> map)
           
static String formatIntString(Map<Integer,String> map)
           
static String formatStringInt(Map<String,Integer> map)
           
static KeyValueFormat.DateConverter newDateConverter()
           
static KeyValueFormat.DateConverter newDateConverter(String format)
           
static KeyValueFormat.DateConverter newDateTimeConverter()
           
static KeyValueFormat.DoubleConverter newDoubleConverter()
           
static KeyValueFormat.IntegerConverter newIntegerConverter()
           
static KeyValueFormat.PriorityConverter newPriorityConverter()
           
static KeyValueFormat.StringConverter newStringConverter()
           
static KeyValueFormat.ToStringConverter newToStringConverter()
           
static Map<String,String> parse(String data)
           
static
<K,V> Map<K,V>
parse(String input, KeyValueFormat.Converter<K> keyConverter, KeyValueFormat.Converter<V> valueConverter)
          If input is null, then an empty map is returned.
static Map<Integer,Date> parseIntDate(String data)
           
static Map<Integer,Date> parseIntDateTime(String data)
           
static Map<Integer,Double> parseIntDouble(String data)
           
static Map<Integer,Integer> parseIntInt(String data)
           
static Map<Integer,String> parseIntString(String data)
           
static Map<String,Double> parseStringDouble(String data)
           
static Map<String,Integer> parseStringInt(String data)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PAIR_SEPARATOR

public static final String PAIR_SEPARATOR
See Also:
Constant Field Values

FIELD_SEPARATOR

public static final String FIELD_SEPARATOR
See Also:
Constant Field Values
Method Detail

newStringConverter

public static KeyValueFormat.StringConverter newStringConverter()

newToStringConverter

public static KeyValueFormat.ToStringConverter newToStringConverter()

newIntegerConverter

public static KeyValueFormat.IntegerConverter newIntegerConverter()

newPriorityConverter

public static KeyValueFormat.PriorityConverter newPriorityConverter()

newDoubleConverter

public static KeyValueFormat.DoubleConverter newDoubleConverter()

newDateConverter

public static KeyValueFormat.DateConverter newDateConverter()

newDateTimeConverter

public static KeyValueFormat.DateConverter newDateTimeConverter()

newDateConverter

public static KeyValueFormat.DateConverter newDateConverter(String format)

parse

public static <K,V> Map<K,V> parse(@Nullable
                                   String input,
                                   KeyValueFormat.Converter<K> keyConverter,
                                   KeyValueFormat.Converter<V> valueConverter)
If input is null, then an empty map is returned.


parse

public static Map<String,String> parse(@Nullable
                                       String data)

parseStringInt

public static Map<String,Integer> parseStringInt(@Nullable
                                                 String data)
Since:
2.7

parseStringDouble

public static Map<String,Double> parseStringDouble(@Nullable
                                                   String data)
Since:
2.7

parseIntString

public static Map<Integer,String> parseIntString(@Nullable
                                                 String data)
Since:
2.7

parseIntDouble

public static Map<Integer,Double> parseIntDouble(@Nullable
                                                 String data)
Since:
2.7

parseIntDate

public static Map<Integer,Date> parseIntDate(@Nullable
                                             String data)
Since:
2.7

parseIntInt

public static Map<Integer,Integer> parseIntInt(@Nullable
                                               String data)
Since:
2.7

parseIntDateTime

public static Map<Integer,Date> parseIntDateTime(@Nullable
                                                 String data)
Since:
2.7

format

public static <K,V> String format(Map<K,V> map,
                                  KeyValueFormat.Converter<K> keyConverter,
                                  KeyValueFormat.Converter<V> valueConverter)
Since:
2.7

format

public static String format(Map map)
Since:
2.7

formatIntString

public static String formatIntString(Map<Integer,String> map)
Since:
2.7

formatIntDouble

public static String formatIntDouble(Map<Integer,Double> map)
Since:
2.7

formatIntDate

public static String formatIntDate(Map<Integer,Date> map)
Since:
2.7

formatIntDateTime

public static String formatIntDateTime(Map<Integer,Date> map)
Since:
2.7

formatStringInt

public static String formatStringInt(Map<String,Integer> map)
Since:
2.7

format

public static <K> String format(com.google.common.collect.Multiset<K> multiset,
                                KeyValueFormat.Converter<K> keyConverter)
Since:
2.7

format

public static String format(com.google.common.collect.Multiset multiset)

format

@Deprecated
public static String format(org.apache.commons.collections.Bag bag,
                                       int var)
Deprecated. use Multiset from google collections instead of commons-collections bags

Since:
1.11


Copyright © 2009–2015 SonarSource. All rights reserved.