001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.propertyhelper;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import java.util.Map;
020import java.util.Optional;
021import java.util.Properties;
022import java.util.StringJoiner;
023
024public interface ValueProvider {
025
026    ValueProvider NULL_PROVIDER = Optional::empty;
027
028    Optional<String> getValue();
029
030    default void setValue(String value) {
031    }
032
033    class SingleValueProvider implements ValueProvider {
034
035        private String value;
036
037        @Override
038        public Optional<String> getValue() {
039            return Optional.ofNullable(value);
040        }
041
042        @Override
043        public void setValue(final String value) {
044            this.value = value;
045        }
046
047        @Override
048        public String toString() {
049            return new StringJoiner(", ", SingleValueProvider.class.getSimpleName() + "[", "]")
050                .add("value='" + value + "'")
051                .toString();
052        }
053    }
054
055    class MapBackedValueAdapter implements ValueProvider {
056
057        private final Map<String, String> values;
058        private final String valueName;
059
060        MapBackedValueAdapter(final Map<String, String> values, final String valueName) {
061            this.valueName = checkNotNull(valueName, "valueName is null");
062            this.values = values;
063        }
064
065        @Override
066        public Optional<String> getValue() {
067            return Optional.ofNullable(values.get(valueName));
068        }
069
070        @Override
071        public void setValue(final String value) {
072            checkNotNull(value, "value is null");
073            values.put(valueName, value);
074        }
075
076        @Override
077        public String toString() {
078            return new StringJoiner(", ", MapBackedValueAdapter.class.getSimpleName() + "[", "]")
079                .add("values=" + values)
080                .add("valueName='" + valueName + "'")
081                .toString();
082        }
083    }
084
085    class PropertyBackedValueAdapter implements ValueProvider {
086
087        private final Properties props;
088        private final String propertyName;
089
090        public PropertyBackedValueAdapter(final Properties props, final String propertyName) {
091            this.props = props;
092            this.propertyName = checkNotNull(propertyName, "propertyName is null");
093        }
094
095        @Override
096        public Optional<String> getValue() {
097            return Optional.ofNullable(props.getProperty(propertyName));
098        }
099
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