001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.context.properties.source;
018
019import org.springframework.boot.origin.Origin;
020import org.springframework.boot.origin.OriginProvider;
021import org.springframework.boot.origin.OriginTrackedValue;
022import org.springframework.core.style.ToStringCreator;
023import org.springframework.util.Assert;
024import org.springframework.util.ObjectUtils;
025
026/**
027 * A single configuration property obtained from a {@link ConfigurationPropertySource}
028 * consisting of a {@link #getName() name}, {@link #getValue() value} and optional
029 * {@link #getOrigin() origin}.
030 *
031 * @author Phillip Webb
032 * @author Madhura Bhave
033 * @since 2.0.0
034 */
035public final class ConfigurationProperty
036                implements OriginProvider, Comparable<ConfigurationProperty> {
037
038        private final ConfigurationPropertyName name;
039
040        private final Object value;
041
042        private final Origin origin;
043
044        public ConfigurationProperty(ConfigurationPropertyName name, Object value,
045                        Origin origin) {
046                Assert.notNull(name, "Name must not be null");
047                Assert.notNull(value, "Value must not be null");
048                this.name = name;
049                this.value = value;
050                this.origin = origin;
051        }
052
053        public ConfigurationPropertyName getName() {
054                return this.name;
055        }
056
057        public Object getValue() {
058                return this.value;
059        }
060
061        @Override
062        public Origin getOrigin() {
063                return this.origin;
064        }
065
066        @Override
067        public boolean equals(Object obj) {
068                if (this == obj) {
069                        return true;
070                }
071                if (obj == null || getClass() != obj.getClass()) {
072                        return false;
073                }
074                ConfigurationProperty other = (ConfigurationProperty) obj;
075                boolean result = true;
076                result = result && ObjectUtils.nullSafeEquals(this.name, other.name);
077                result = result && ObjectUtils.nullSafeEquals(this.value, other.value);
078                return result;
079        }
080
081        @Override
082        public int hashCode() {
083                int result = ObjectUtils.nullSafeHashCode(this.name);
084                result = 31 * result + ObjectUtils.nullSafeHashCode(this.value);
085                return result;
086        }
087
088        @Override
089        public String toString() {
090                return new ToStringCreator(this).append("name", this.name)
091                                .append("value", this.value).append("origin", this.origin).toString();
092        }
093
094        @Override
095        public int compareTo(ConfigurationProperty other) {
096                return this.name.compareTo(other.name);
097        }
098
099        static ConfigurationProperty of(ConfigurationPropertyName name,
100                        OriginTrackedValue value) {
101                if (value == null) {
102                        return null;
103                }
104                return new ConfigurationProperty(name, value.getValue(), value.getOrigin());
105        }
106
107        static ConfigurationProperty of(ConfigurationPropertyName name, Object value,
108                        Origin origin) {
109                if (value == null) {
110                        return null;
111                }
112                return new ConfigurationProperty(name, value, origin);
113        }
114
115}