Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.cfg; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | import static com.google.common.base.Preconditions.checkState; |
| 24 | |
| 25 | /** |
| 26 | * Component configuration property. |
| 27 | */ |
| 28 | public final class ConfigProperty { |
| 29 | |
| 30 | private final String name; |
| 31 | private final Type type; |
| 32 | private final String value; |
| 33 | private final String defaultValue; |
| 34 | private final String description; |
| 35 | private final boolean isSet; |
| 36 | |
| 37 | /** |
| 38 | * Representation of the type of property value. |
| 39 | */ |
| 40 | public enum Type { |
| 41 | /** |
| 42 | * Indicates the value is a string. |
| 43 | */ |
| 44 | STRING, |
| 45 | |
| 46 | /** |
| 47 | * Indicates the value is an integer. |
| 48 | */ |
| 49 | INTEGER, |
| 50 | |
| 51 | /** |
| 52 | * Indicates the value is a long. |
| 53 | */ |
| 54 | LONG, |
| 55 | |
| 56 | /** |
| 57 | * Indicates the value is a float. |
| 58 | */ |
| 59 | FLOAT, |
| 60 | |
| 61 | /** |
| 62 | * Indicates the value is a double. |
| 63 | */ |
| 64 | DOUBLE, |
| 65 | |
| 66 | /** |
| 67 | * Indicates the value is a boolean. |
| 68 | */ |
| 69 | BOOLEAN |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Creates a new configuration property with its default value. |
| 74 | * |
| 75 | * @param name property name |
| 76 | * @param type value type |
| 77 | * @param defaultValue default value as a string |
| 78 | * @param description property description |
| 79 | * @return newly defined property |
| 80 | */ |
| 81 | public static ConfigProperty defineProperty(String name, Type type, |
| 82 | String defaultValue, |
| 83 | String description) { |
| 84 | return new ConfigProperty(name, type, description, defaultValue, defaultValue, false); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Creates a new configuration property as a copy of an existing one, but |
| 89 | * with a new value. |
| 90 | * |
| 91 | * @param property property to be changed |
| 92 | * @param newValue new value as a string |
| 93 | * @return newly updated property |
| 94 | */ |
| 95 | public static ConfigProperty setProperty(ConfigProperty property, String newValue) { |
| 96 | return new ConfigProperty(property.name, property.type, property.description, |
| 97 | property.defaultValue, newValue, true); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Creates a new configuration property as a copy of an existing one, but |
| 102 | * without a specific value, thus making it take its default value. |
| 103 | * |
| 104 | * @param property property to be reset |
| 105 | * @return newly reset property |
| 106 | */ |
| 107 | public static ConfigProperty resetProperty(ConfigProperty property) { |
| 108 | return new ConfigProperty(property.name, property.type, property.description, |
| 109 | property.defaultValue, property.defaultValue, false); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Creates a new configuration property with its default value. |
| 114 | * |
| 115 | * @param name property name |
| 116 | * @param type value type |
| 117 | * @param defaultValue default value as a string |
| 118 | * @param description property description |
| 119 | * @param value property value |
| 120 | * @param isSet indicates whether the property is set or not |
| 121 | */ |
| 122 | private ConfigProperty(String name, Type type, String description, |
| 123 | String defaultValue, String value, boolean isSet) { |
| 124 | this.name = checkNotNull(name, "Property name cannot be null"); |
| 125 | this.type = checkNotNull(type, "Property type cannot be null"); |
| 126 | this.description = checkNotNull(description, "Property description cannot be null"); |
| 127 | this.defaultValue = defaultValue; |
| 128 | this.value = value; |
| 129 | this.isSet = isSet; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns the property name. |
| 134 | * |
| 135 | * @return property name |
| 136 | */ |
| 137 | public String name() { |
| 138 | return name; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the property type. |
| 143 | * |
| 144 | * @return property type |
| 145 | */ |
| 146 | public Type type() { |
| 147 | return type; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns the property description. |
| 152 | * |
| 153 | * @return string value |
| 154 | */ |
| 155 | public String description() { |
| 156 | return description; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns the property default value as a string. |
| 161 | * |
| 162 | * @return string default value |
| 163 | */ |
| 164 | public String defaultValue() { |
| 165 | return defaultValue; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns the property value as a string. |
| 170 | * |
| 171 | * @return string value |
| 172 | */ |
| 173 | public String value() { |
| 174 | return value; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Indicates whether the property is set or whether it assumes its |
| 179 | * default value. |
| 180 | * |
| 181 | * @return true if the property is set |
| 182 | */ |
| 183 | public boolean isSet() { |
| 184 | return isSet; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Returns the property value as a string. |
| 189 | * |
| 190 | * @return string value |
| 191 | */ |
| 192 | public String asString() { |
| 193 | return value; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns the property value as an integer. |
| 198 | * |
| 199 | * @return integer value |
| 200 | */ |
| 201 | public int asInteger() { |
| 202 | checkState(type == Type.INTEGER, "Value is not an integer"); |
| 203 | return Integer.parseInt(value); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns the property value as a long. |
| 208 | * |
| 209 | * @return long value |
| 210 | */ |
| 211 | public long asLong() { |
| 212 | checkState(type == Type.INTEGER || type == Type.LONG, "Value is not a long or integer"); |
| 213 | return Long.parseLong(value); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Returns the property value as a float. |
| 218 | * |
| 219 | * @return float value |
| 220 | */ |
| 221 | public float asFloat() { |
| 222 | checkState(type == Type.FLOAT, "Value is not a float"); |
| 223 | return Float.parseFloat(value); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Returns the property value as a double. |
| 228 | * |
| 229 | * @return double value |
| 230 | */ |
| 231 | public double asDouble() { |
| 232 | checkState(type == Type.FLOAT || type == Type.DOUBLE, "Value is not a float or double"); |
| 233 | return Double.parseDouble(value); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Returns the property value as a boolean. |
| 238 | * |
| 239 | * @return string value |
| 240 | */ |
| 241 | public boolean asBoolean() { |
| 242 | checkState(type == Type.BOOLEAN, "Value is not a boolean"); |
| 243 | return Boolean.parseBoolean(value); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public int hashCode() { |
| 248 | return Objects.hash(name); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * {@inheritDoc} |
| 253 | * |
| 254 | * Equality is considered only on the basis of property name. |
| 255 | */ |
| 256 | @Override |
| 257 | public boolean equals(Object obj) { |
| 258 | if (this == obj) { |
| 259 | return true; |
| 260 | } |
| 261 | if (obj instanceof ConfigProperty) { |
| 262 | final ConfigProperty other = (ConfigProperty) obj; |
| 263 | return Objects.equals(this.name, other.name); |
| 264 | } |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public String toString() { |
| 270 | return MoreObjects.toStringHelper(this) |
| 271 | .add("name", name) |
| 272 | .add("type", type) |
| 273 | .add("value", value) |
| 274 | .add("defaultValue", defaultValue) |
| 275 | .add("description", description) |
| 276 | .add("isSet", isSet) |
| 277 | .toString(); |
| 278 | } |
| 279 | } |