Explorar o código

refactor: refactor Functions class with lambda (#4419)

* refactor Functions class with lambda

* update CHANGES.md
Anthony-Lu %!s(int64=2) %!d(string=hai) anos
pai
achega
b73204ac64

+ 1 - 0
CHANGES.md

@@ -8,5 +8,6 @@ Apollo 2.1.0
 * [Add a config adjust the property source overriden behavior](https://github.com/apolloconfig/apollo/pull/4409)
 * [feat(apollo-client): the spi of config service load balancer client](https://github.com/apolloconfig/apollo/pull/4394)
 * [add cat-client as optional dependency](https://github.com/apolloconfig/apollo/pull/4414)
+* [refactor Functions class with lambda](https://github.com/apolloconfig/apollo/pull/4419)
 ------------------
 All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/11?closed=1)

+ 19 - 60
apollo-client/src/main/java/com/ctrip/framework/apollo/util/function/Functions.java

@@ -27,66 +27,25 @@ import com.google.common.base.Function;
  * @author Jason Song(song_s@ctrip.com)
  */
 public interface Functions {
-  Function<String, Integer> TO_INT_FUNCTION = new Function<String, Integer>() {
-    @Override
-    public Integer apply(String input) {
-      return Integer.parseInt(input);
-    }
-  };
-  Function<String, Long> TO_LONG_FUNCTION = new Function<String, Long>() {
-    @Override
-    public Long apply(String input) {
-      return Long.parseLong(input);
-    }
-  };
-  Function<String, Short> TO_SHORT_FUNCTION = new Function<String, Short>() {
-    @Override
-    public Short apply(String input) {
-      return Short.parseShort(input);
-    }
-  };
-  Function<String, Float> TO_FLOAT_FUNCTION = new Function<String, Float>() {
-    @Override
-    public Float apply(String input) {
-      return Float.parseFloat(input);
-    }
-  };
-  Function<String, Double> TO_DOUBLE_FUNCTION = new Function<String, Double>() {
-    @Override
-    public Double apply(String input) {
-      return Double.parseDouble(input);
-    }
-  };
-  Function<String, Byte> TO_BYTE_FUNCTION = new Function<String, Byte>() {
-    @Override
-    public Byte apply(String input) {
-      return Byte.parseByte(input);
-    }
-  };
-  Function<String, Boolean> TO_BOOLEAN_FUNCTION = new Function<String, Boolean>() {
-    @Override
-    public Boolean apply(String input) {
-      return Boolean.parseBoolean(input);
-    }
-  };
-  Function<String, Date> TO_DATE_FUNCTION = new Function<String, Date>() {
-    @Override
-    public Date apply(String input) {
-      try {
-        return Parsers.forDate().parse(input);
-      } catch (ParserException ex) {
-        throw new ApolloConfigException("Parse date failed", ex);
-      }
-    }
-  };
-  Function<String, Long> TO_DURATION_FUNCTION = new Function<String, Long>() {
-    @Override
-    public Long apply(String input) {
-      try {
-        return Parsers.forDuration().parseToMillis(input);
-      } catch (ParserException ex) {
-        throw new ApolloConfigException("Parse duration failed", ex);
-      }
+  Function<String, Integer> TO_INT_FUNCTION = Integer::parseInt;
+  Function<String, Long> TO_LONG_FUNCTION = Long::parseLong;
+  Function<String, Short> TO_SHORT_FUNCTION = Short::parseShort;
+  Function<String, Float> TO_FLOAT_FUNCTION = Float::parseFloat;
+  Function<String, Double> TO_DOUBLE_FUNCTION = Double::parseDouble;
+  Function<String, Byte> TO_BYTE_FUNCTION = Byte::parseByte;
+  Function<String, Boolean> TO_BOOLEAN_FUNCTION = Boolean::parseBoolean;
+  Function<String, Date> TO_DATE_FUNCTION = input -> {
+    try {
+      return Parsers.forDate().parse(input);
+    } catch (ParserException ex) {
+      throw new ApolloConfigException("Parse date failed", ex);
+    }
+  };
+  Function<String, Long> TO_DURATION_FUNCTION = input -> {
+    try {
+      return Parsers.forDuration().parseToMillis(input);
+    } catch (ParserException ex) {
+      throw new ApolloConfigException("Parse duration failed", ex);
     }
   };
 }