View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  
19  import java.util.Map;
20  import java.util.Optional;
21  import java.util.Properties;
22  import java.util.StringJoiner;
23  
24  public interface ValueProvider {
25  
26      ValueProvider NULL_PROVIDER = Optional::empty;
27  
28      Optional<String> getValue();
29  
30      default void setValue(String value) {
31      }
32  
33      class SingleValueProvider implements ValueProvider {
34  
35          private String value;
36  
37          @Override
38          public Optional<String> getValue() {
39              return Optional.ofNullable(value);
40          }
41  
42          @Override
43          public void setValue(final String value) {
44              this.value = value;
45          }
46  
47          @Override
48          public String toString() {
49              return new StringJoiner(", ", SingleValueProvider.class.getSimpleName() + "[", "]")
50                  .add("value='" + value + "'")
51                  .toString();
52          }
53      }
54  
55      class MapBackedValueAdapter implements ValueProvider {
56  
57          private final Map<String, String> values;
58          private final String valueName;
59  
60          MapBackedValueAdapter(final Map<String, String> values, final String valueName) {
61              this.valueName = checkNotNull(valueName, "valueName is null");
62              this.values = values;
63          }
64  
65          @Override
66          public Optional<String> getValue() {
67              return Optional.ofNullable(values.get(valueName));
68          }
69  
70          @Override
71          public void setValue(final String value) {
72              checkNotNull(value, "value is null");
73              values.put(valueName, value);
74          }
75  
76          @Override
77          public String toString() {
78              return new StringJoiner(", ", MapBackedValueAdapter.class.getSimpleName() + "[", "]")
79                  .add("values=" + values)
80                  .add("valueName='" + valueName + "'")
81                  .toString();
82          }
83      }
84  
85      class PropertyBackedValueAdapter implements ValueProvider {
86  
87          private final Properties props;
88          private final String propertyName;
89  
90          public PropertyBackedValueAdapter(final Properties props, final String propertyName) {
91              this.props = props;
92              this.propertyName = checkNotNull(propertyName, "propertyName is null");
93          }
94  
95          @Override
96          public Optional<String> getValue() {
97              return Optional.ofNullable(props.getProperty(propertyName));
98          }
99  
100         @Override
101         public void setValue(final String value) {
102             checkNotNull(value, "value is null");
103             props.setProperty(propertyName, value);
104         }
105 
106         @Override
107         public String toString() {
108             return new StringJoiner(", ", PropertyBackedValueAdapter.class.getSimpleName() + "[", "]")
109                 .add("props=" + props)
110                 .add("propertyName='" + propertyName + "'")
111                 .toString();
112         }
113     }
114 }
115