From f0373d9bef4f308d92f449ee70637a71c009d79b Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 20 Oct 2014 22:28:33 +0200 Subject: [PATCH] remove methods of KeyValueFormat that are deprecated since 2.7 --- .../measures/RangeDistributionBuilder.java | 6 +- .../java/org/sonar/api/utils/KeyValue.java | 2 + .../org/sonar/api/utils/KeyValueFormat.java | 122 +----------------- .../utils/DeprecatedKeyValueFormatTest.java | 40 +----- 4 files changed, 11 insertions(+), 159 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/RangeDistributionBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/RangeDistributionBuilder.java index 7a8e905f8ed..eae383f6f81 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/RangeDistributionBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/RangeDistributionBuilder.java @@ -27,6 +27,8 @@ import org.apache.commons.lang.NumberUtils; import org.sonar.api.utils.KeyValueFormat; import org.sonar.api.utils.SonarException; +import javax.annotation.Nullable; + import java.util.Arrays; import java.util.Collections; import java.util.Map; @@ -144,9 +146,9 @@ public class RangeDistributionBuilder implements MeasureBuilder { * @param measure the measure to add to the current one * @return the current object */ - public RangeDistributionBuilder add(Measure measure) { + public RangeDistributionBuilder add(@Nullable Measure measure) { if (measure != null && measure.getData() != null) { - Map map = KeyValueFormat.parse(measure.getData(), new KeyValueFormat.DoubleNumbersPairTransformer()); + Map map = KeyValueFormat.parse(measure.getData(), KeyValueFormat.newDoubleConverter(), KeyValueFormat.newDoubleConverter()); Number[] limits = map.keySet().toArray(new Number[map.size()]); if (bottomLimits == null) { init(limits); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValue.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValue.java index 9198f40694d..9a8045d1bb4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/KeyValue.java @@ -23,7 +23,9 @@ package org.sonar.api.utils; * A utility class to store a key / value couple of generic types * * @since 1.10 + * @deprecated in 2.7. Used only in deprecated methods of {@link org.sonar.api.utils.KeyValueFormat} */ +@Deprecated public class KeyValue { private K key; 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 f4e5cb7a889..6319effa84d 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 @@ -26,12 +26,14 @@ import com.google.common.collect.Multiset; import org.apache.commons.collections.Bag; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; -import org.slf4j.LoggerFactory; import org.sonar.api.rules.RulePriority; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.Collection; +import java.util.Date; +import java.util.Map; +import java.util.Set; /** *

Formats and parses key/value pairs with the string representation : "key1=value1;key2=value2". Conversion @@ -203,16 +205,6 @@ public final class KeyValueFormat { return new DateConverter(format); } - /** - * @deprecated in version 2.13. Replaced by {@link org.sonar.api.utils.KeyValueFormat#newDateTimeConverter()} - */ - @Deprecated - public static class DateTimeConverter extends DateConverter { - public DateTimeConverter() { - super(DateUtils.DATETIME_FORMAT); - } - } - public static Map parse(String data, Converter keyConverter, Converter valueConverter) { Map map = Maps.newLinkedHashMap(); if (data != null) { @@ -317,28 +309,6 @@ public final class KeyValueFormat { return parseMultiset(data, newStringConverter()); } - /** - * Transforms a string with the following format: "key1=value1;key2=value2..." - * into a Map. Requires to implement the transform(key,value) method - * - * @param data the input string - * @param transformer the interface to implement - * @return a Map of - * @deprecated since 2.7 - */ - @Deprecated - public static Map parse(String data, Transformer transformer) { - Map rawData = parse(data); - Map map = new HashMap(); - for (Map.Entry entry : rawData.entrySet()) { - KeyValue keyVal = transformer.transform(entry.getKey(), entry.getValue()); - if (keyVal != null) { - map.put(keyVal.getKey(), keyVal.getValue()); - } - } - return map; - } - private static String formatEntries(Collection> entries, Converter keyConverter, Converter valueConverter) { StringBuilder sb = new StringBuilder(); boolean first = true; @@ -442,15 +412,6 @@ public final class KeyValueFormat { } - /** - * @since 1.11 - * @deprecated use Multiset from google collections instead of commons-collections bags - */ - @Deprecated - public static String format(Bag bag) { - return format(bag, 0); - } - /** * @since 1.11 * @deprecated use Multiset from google collections instead of commons-collections bags @@ -472,79 +433,4 @@ public final class KeyValueFormat { } return sb.toString(); } - - - /** - * @deprecated since 2.7. Replaced by Converter - */ - @Deprecated - public interface Transformer { - KeyValue transform(String key, String value); - } - - /** - * Implementation of Transformer - * - * @deprecated since 2.7 replaced by Converter - */ - @Deprecated - public static class StringNumberPairTransformer implements Transformer { - public KeyValue transform(String key, String value) { - return new KeyValue(key, toDouble(value)); - } - } - - /** - * Implementation of Transformer - * - * @deprecated since 2.7. Replaced by Converter - */ - @Deprecated - public static class DoubleNumbersPairTransformer implements Transformer { - public KeyValue transform(String key, String value) { - return new KeyValue(toDouble(key), toDouble(value)); - } - } - - /** - * Implementation of Transformer - * - * @deprecated since 2.7. Replaced by Converter - */ - @Deprecated - public static class IntegerNumbersPairTransformer implements Transformer { - public KeyValue transform(String key, String value) { - return new KeyValue(toInteger(key), toInteger(value)); - } - } - - - /** - * Implementation of Transformer - * - * @deprecated since 2.7. Replaced by Converter - */ - @Deprecated - public static class RulePriorityNumbersPairTransformer implements Transformer { - - public KeyValue transform(String key, String value) { - try { - if (StringUtils.isBlank(value)) { - value = "0"; - } - return new KeyValue(RulePriority.valueOf(key.toUpperCase()), Integer.parseInt(value)); - } catch (Exception e) { - LoggerFactory.getLogger(RulePriorityNumbersPairTransformer.class).warn("Property " + key + " has invalid value: " + value, e); - return null; - } - } - } - - private static Double toDouble(String value) { - return StringUtils.isBlank(value) ? null : NumberUtils.toDouble(value); - } - - private static Integer toInteger(String value) { - return StringUtils.isBlank(value) ? null : NumberUtils.toInt(value); - } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DeprecatedKeyValueFormatTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DeprecatedKeyValueFormatTest.java index de49842346a..6d644688e47 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DeprecatedKeyValueFormatTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DeprecatedKeyValueFormatTest.java @@ -23,7 +23,6 @@ import com.google.common.collect.Multiset; import com.google.common.collect.TreeMultiset; import org.apache.commons.collections.bag.TreeBag; import org.junit.Test; -import org.sonar.api.rules.RulePriority; import java.util.Map; import java.util.TreeMap; @@ -49,7 +48,7 @@ public class DeprecatedKeyValueFormatTest { TreeBag bag = new TreeBag(); bag.add("hello", 1); bag.add("key", 3); - assertThat(KeyValueFormat.format(bag), is("hello=1;key=3")); + assertThat(KeyValueFormat.format(bag, 0), is("hello=1;key=3")); } @Test @@ -77,41 +76,4 @@ public class DeprecatedKeyValueFormatTest { assertEquals("", map.get("key2")); assertEquals("val3", map.get("key3")); } - - @Test - public void parseWithStringNumberTransformation() { - Map map = KeyValueFormat.parse("hello=1;key1=;key2=3;key3=5.1", new KeyValueFormat.StringNumberPairTransformer()); - assertThat(map.size(), is(4)); - assertEquals(new Double(1), map.get("hello")); - assertEquals(null, map.get("key1")); - assertEquals(new Double(3), map.get("key2")); - assertEquals(new Double(5.1), map.get("key3")); - } - - @Test - public void parseWithDoubleNumbersTransformation() { - Map map = KeyValueFormat.parse("0=1;10=;60=5.1", new KeyValueFormat.DoubleNumbersPairTransformer()); - assertThat(map.size(), is(3)); - assertEquals(new Double(1), map.get(new Double(0))); - assertEquals(null, map.get(10)); - assertEquals(new Double(5.1), map.get(new Double(60))); - } - - @Test - public void parseWithIntegerNumbersTransformation() { - Map map = KeyValueFormat.parse("0=1;10=;60=5", new KeyValueFormat.IntegerNumbersPairTransformer()); - assertThat(map.size(), is(3)); - assertEquals(new Integer(1), map.get(new Integer(0))); - assertEquals(null, map.get(10)); - assertEquals(new Integer(5), map.get(new Integer(60))); - } - - @Test - public void parseWithRulePriorityNumbersTransformation() { - Map map = KeyValueFormat.parse("BLOCKER=1;MAJOR=;INFO=5", new KeyValueFormat.RulePriorityNumbersPairTransformer()); - assertThat(map.size(), is(3)); - assertEquals(new Integer(1), map.get(RulePriority.BLOCKER)); - assertEquals(new Integer(0), map.get(RulePriority.MAJOR)); - assertEquals(new Integer(5), map.get(RulePriority.INFO)); - } } -- 2.39.5