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  
23  public interface ValueProvider {
24  
25      ValueProvider NULL_PROVIDER = new NullProvider();
26  
27      Optional<String> getValue();
28  
29      void setValue(String value);
30  
31      class NullProvider
32              implements ValueProvider {
33  
34          private NullProvider() {
35          }
36  
37          @Override
38          public Optional<String> getValue() {
39              return Optional.empty();
40          }
41  
42          @Override
43          public void setValue(final String value) {
44          }
45      }
46  
47      class StaticValueProvider
48              implements ValueProvider {
49  
50          private String value;
51  
52          StaticValueProvider() {
53          }
54  
55          @Override
56          public Optional<String> getValue() {
57              return Optional.ofNullable(value);
58          }
59  
60          @Override
61          public void setValue(final String value) {
62              this.value = value;
63          }
64      }
65  
66      class MapValueProvider
67              implements ValueProvider {
68  
69          private final Map<String, String> values;
70          private final String valueName;
71  
72          MapValueProvider(final Map<String, String> values, final String valueName) {
73              this.valueName = checkNotNull(valueName, "valueName is null");
74              this.values = values;
75          }
76  
77          @Override
78          public Optional<String> getValue() {
79              return Optional.ofNullable(values.get(valueName));
80          }
81  
82          @Override
83          public void setValue(final String value) {
84              checkNotNull(value, "value is null");
85              values.put(valueName, value);
86          }
87      }
88  
89      class PropertyProvider
90              implements ValueProvider {
91  
92          private final Properties props;
93          private final String propertyName;
94  
95          PropertyProvider(final Properties props, final String propertyName) {
96              this.props = props;
97              this.propertyName = checkNotNull(propertyName, "propertyName is null");
98          }
99  
100         @Override
101         public Optional<String> getValue() {
102             return Optional.ofNullable(props.getProperty(propertyName));
103         }
104 
105         @Override
106         public void setValue(final String value) {
107             checkNotNull(value, "value is null");
108             props.setProperty(propertyName, value);
109         }
110     }
111 }
112