1818
1919package org .killbill .billing .plugin .api ;
2020
21+ import java .util .Collections ;
2122import java .util .HashMap ;
2223import java .util .List ;
2324import java .util .Map ;
25+ import java .util .Map .Entry ;
2426import java .util .regex .Pattern ;
27+ import java .util .stream .Collectors ;
2528
2629import javax .annotation .Nullable ;
2730
2831import org .killbill .billing .payment .api .PluginProperty ;
2932import org .killbill .billing .plugin .util .http .UTF8UrlDecoder ;
33+ import org .killbill .commons .utils .Strings ;
34+ import org .killbill .commons .utils .collect .Iterables ;
3035
31- import com .google .common .base .Function ;
32- import com .google .common .base .Predicate ;
33- import com .google .common .base .Strings ;
34- import com .google .common .collect .ImmutableList ;
35- import com .google .common .collect .Iterables ;
36- import com .google .common .collect .Maps ;
3736import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
3837
3938public abstract class PluginProperties {
4039
4140 // Last one has precedence
41+ @ SafeVarargs
4242 public static Iterable <PluginProperty > merge (final Iterable <PluginProperty >... propertiesLists ) {
4343 return buildPluginProperties (toMap (propertiesLists ));
4444 }
4545
4646 // Last one has precedence
47- public static Iterable <PluginProperty > merge (@ Nullable final Map data , final Iterable <PluginProperty >... propertiesLists ) {
47+ @ SafeVarargs
48+ public static <K , V > Iterable <PluginProperty > merge (@ Nullable final Map <K , V > data , final Iterable <PluginProperty >... propertiesLists ) {
4849 return merge (buildPluginProperties (data ), merge (propertiesLists ));
4950 }
5051
5152 // Last one has precedence
53+ @ SafeVarargs
5254 public static Map <String , Object > toMap (final Iterable <PluginProperty >... propertiesLists ) {
53- final Map <String , Object > mergedProperties = new HashMap <String , Object >();
55+ final Map <String , Object > mergedProperties = new HashMap <>();
5456 for (final Iterable <PluginProperty > propertiesList : propertiesLists ) {
5557 if (propertiesList == null ) {
5658 continue ;
@@ -65,14 +67,12 @@ public static Map<String, Object> toMap(final Iterable<PluginProperty>... proper
6567 }
6668
6769 // Last one has precedence
70+ @ SafeVarargs
6871 public static Map <String , String > toStringMap (final Iterable <PluginProperty >... propertiesLists ) {
69- return Maps .transformValues (toMap (propertiesLists ),
70- new Function <Object , String >() {
71- @ Override
72- public String apply (final Object input ) {
73- return input == null ? null : input .toString ();
74- }
75- });
72+ return toMap (propertiesLists )
73+ .entrySet ()
74+ .stream ()
75+ .collect (Collectors .toMap (Entry ::getKey , entry -> entry .getValue () == null ? null : entry .getValue ().toString ()));
7676 }
7777
7878 // Return the value from the plugin properties if it exists, or the fallback otherwise
@@ -86,13 +86,9 @@ public static String findPluginPropertyValue(final String pluginPropertyName, @N
8686 return null ;
8787 }
8888
89- final PluginProperty pluginProperty = Iterables .tryFind (properties ,
90- new Predicate <PluginProperty >() {
91- @ Override
92- public boolean apply (final PluginProperty input ) {
93- return input != null && pluginPropertyName .equals (input .getKey ());
94- }
95- }).orNull ();
89+ final PluginProperty pluginProperty = Iterables .toStream (properties )
90+ .filter (input -> input != null && pluginPropertyName .equals (input .getKey ()))
91+ .findFirst ().orElse (null );
9692
9793 if (pluginProperty == null || pluginProperty .getValue () == null ) {
9894 return null ;
@@ -111,40 +107,28 @@ public static Iterable<PluginProperty> findPluginProperties(final String key, @N
111107 return null ;
112108 }
113109
114- return Iterables .filter (properties ,
115- new Predicate <PluginProperty >() {
116- @ Override
117- public boolean apply (final PluginProperty input ) {
118- return key != null && key .equals (input .getKey ());
119- }
120- });
110+ return Iterables .toStream (properties )
111+ .filter (input -> key != null && input != null && key .equals (input .getKey ()))
112+ .collect (Collectors .toUnmodifiableSet ());
121113 }
122114
123115 public static Iterable <PluginProperty > findPluginProperties (final Pattern keyPattern , @ Nullable final Iterable <PluginProperty > properties ) {
124116 if (properties == null ) {
125117 return null ;
126118 }
127119
128- return Iterables .filter (properties ,
129- new Predicate <PluginProperty >() {
130- @ Override
131- public boolean apply (final PluginProperty input ) {
132- return input != null && keyPattern .matcher (input .getKey ()).matches ();
133- }
134- });
120+ return Iterables .toStream (properties )
121+ .filter (input -> keyPattern != null && input != null && keyPattern .matcher (input .getKey ()).matches ())
122+ .collect (Collectors .toUnmodifiableSet ());
135123 }
136124
137125 @ SuppressFBWarnings ("WMI_WRONG_MAP_ITERATOR" )
138- public static List <PluginProperty > buildPluginProperties (@ Nullable final Map data ) {
139- final ImmutableList .Builder <PluginProperty > propertiesBuilder = ImmutableList .builder ();
140- if (data != null ) {
141- for (final Object key : data .keySet ()) {
142- if (key != null ) {
143- final PluginProperty property = new PluginProperty (key .toString (), data .get (key ), false );
144- propertiesBuilder .add (property );
145- }
146- }
126+ public static <K , V > List <PluginProperty > buildPluginProperties (@ Nullable final Map <K , V > data ) {
127+ if (data == null || data .isEmpty ()) {
128+ return Collections .emptyList ();
147129 }
148- return propertiesBuilder .build ();
130+ return data .entrySet ().stream ()
131+ .map (entry -> new PluginProperty (entry .getKey ().toString (), entry .getValue (), false ))
132+ .collect (Collectors .toUnmodifiableList ());
149133 }
150134}
0 commit comments