Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 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 | /** |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 47 | * Indicates the value is a byte. |
| 48 | */ |
| 49 | BYTE, |
| 50 | |
| 51 | /** |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 52 | * Indicates the value is an integer. |
| 53 | */ |
| 54 | INTEGER, |
| 55 | |
| 56 | /** |
| 57 | * Indicates the value is a long. |
| 58 | */ |
| 59 | LONG, |
| 60 | |
| 61 | /** |
| 62 | * Indicates the value is a float. |
| 63 | */ |
| 64 | FLOAT, |
| 65 | |
| 66 | /** |
| 67 | * Indicates the value is a double. |
| 68 | */ |
| 69 | DOUBLE, |
| 70 | |
| 71 | /** |
| 72 | * Indicates the value is a boolean. |
| 73 | */ |
| 74 | BOOLEAN |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates a new configuration property with its default value. |
| 79 | * |
| 80 | * @param name property name |
| 81 | * @param type value type |
| 82 | * @param defaultValue default value as a string |
| 83 | * @param description property description |
| 84 | * @return newly defined property |
| 85 | */ |
| 86 | public static ConfigProperty defineProperty(String name, Type type, |
| 87 | String defaultValue, |
| 88 | String description) { |
| 89 | return new ConfigProperty(name, type, description, defaultValue, defaultValue, false); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates a new configuration property as a copy of an existing one, but |
| 94 | * with a new value. |
| 95 | * |
| 96 | * @param property property to be changed |
| 97 | * @param newValue new value as a string |
| 98 | * @return newly updated property |
| 99 | */ |
| 100 | public static ConfigProperty setProperty(ConfigProperty property, String newValue) { |
| 101 | return new ConfigProperty(property.name, property.type, property.description, |
| 102 | property.defaultValue, newValue, true); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Creates a new configuration property as a copy of an existing one, but |
| 107 | * without a specific value, thus making it take its default value. |
| 108 | * |
| 109 | * @param property property to be reset |
| 110 | * @return newly reset property |
| 111 | */ |
| 112 | public static ConfigProperty resetProperty(ConfigProperty property) { |
| 113 | return new ConfigProperty(property.name, property.type, property.description, |
| 114 | property.defaultValue, property.defaultValue, false); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Creates a new configuration property with its default value. |
| 119 | * |
| 120 | * @param name property name |
| 121 | * @param type value type |
| 122 | * @param defaultValue default value as a string |
| 123 | * @param description property description |
| 124 | * @param value property value |
| 125 | * @param isSet indicates whether the property is set or not |
| 126 | */ |
| 127 | private ConfigProperty(String name, Type type, String description, |
| 128 | String defaultValue, String value, boolean isSet) { |
| 129 | this.name = checkNotNull(name, "Property name cannot be null"); |
| 130 | this.type = checkNotNull(type, "Property type cannot be null"); |
| 131 | this.description = checkNotNull(description, "Property description cannot be null"); |
| 132 | this.defaultValue = defaultValue; |
| 133 | this.value = value; |
| 134 | this.isSet = isSet; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns the property name. |
| 139 | * |
| 140 | * @return property name |
| 141 | */ |
| 142 | public String name() { |
| 143 | return name; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns the property type. |
| 148 | * |
| 149 | * @return property type |
| 150 | */ |
| 151 | public Type type() { |
| 152 | return type; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns the property description. |
| 157 | * |
| 158 | * @return string value |
| 159 | */ |
| 160 | public String description() { |
| 161 | return description; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Returns the property default value as a string. |
| 166 | * |
| 167 | * @return string default value |
| 168 | */ |
| 169 | public String defaultValue() { |
| 170 | return defaultValue; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns the property value as a string. |
| 175 | * |
| 176 | * @return string value |
| 177 | */ |
| 178 | public String value() { |
| 179 | return value; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Indicates whether the property is set or whether it assumes its |
| 184 | * default value. |
| 185 | * |
| 186 | * @return true if the property is set |
| 187 | */ |
| 188 | public boolean isSet() { |
| 189 | return isSet; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns the property value as a string. |
| 194 | * |
| 195 | * @return string value |
| 196 | */ |
| 197 | public String asString() { |
| 198 | return value; |
| 199 | } |
| 200 | |
| 201 | /** |
Jonathan Hart | 81d7310 | 2016-02-19 10:32:05 -0800 | [diff] [blame] | 202 | * Returns the property value as a byte. |
| 203 | * |
| 204 | * @return byte value |
| 205 | */ |
| 206 | public byte asByte() { |
| 207 | checkState(type == Type.BYTE, "Value is not a byte"); |
| 208 | return Byte.parseByte(value); |
| 209 | } |
| 210 | |
| 211 | /** |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 212 | * Returns the property value as an integer. |
| 213 | * |
| 214 | * @return integer value |
| 215 | */ |
| 216 | public int asInteger() { |
| 217 | checkState(type == Type.INTEGER, "Value is not an integer"); |
| 218 | return Integer.parseInt(value); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Returns the property value as a long. |
| 223 | * |
| 224 | * @return long value |
| 225 | */ |
| 226 | public long asLong() { |
| 227 | checkState(type == Type.INTEGER || type == Type.LONG, "Value is not a long or integer"); |
| 228 | return Long.parseLong(value); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Returns the property value as a float. |
| 233 | * |
| 234 | * @return float value |
| 235 | */ |
| 236 | public float asFloat() { |
| 237 | checkState(type == Type.FLOAT, "Value is not a float"); |
| 238 | return Float.parseFloat(value); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Returns the property value as a double. |
| 243 | * |
| 244 | * @return double value |
| 245 | */ |
| 246 | public double asDouble() { |
| 247 | checkState(type == Type.FLOAT || type == Type.DOUBLE, "Value is not a float or double"); |
| 248 | return Double.parseDouble(value); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns the property value as a boolean. |
| 253 | * |
| 254 | * @return string value |
| 255 | */ |
| 256 | public boolean asBoolean() { |
| 257 | checkState(type == Type.BOOLEAN, "Value is not a boolean"); |
| 258 | return Boolean.parseBoolean(value); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public int hashCode() { |
HIGUCHI Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 263 | return name.hashCode(); |
Thomas Vachuska | 6d697f1 | 2015-03-08 20:59:50 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * {@inheritDoc} |
| 268 | * |
| 269 | * Equality is considered only on the basis of property name. |
| 270 | */ |
| 271 | @Override |
| 272 | public boolean equals(Object obj) { |
| 273 | if (this == obj) { |
| 274 | return true; |
| 275 | } |
| 276 | if (obj instanceof ConfigProperty) { |
| 277 | final ConfigProperty other = (ConfigProperty) obj; |
| 278 | return Objects.equals(this.name, other.name); |
| 279 | } |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public String toString() { |
| 285 | return MoreObjects.toStringHelper(this) |
| 286 | .add("name", name) |
| 287 | .add("type", type) |
| 288 | .add("value", value) |
| 289 | .add("defaultValue", defaultValue) |
| 290 | .add("description", description) |
| 291 | .add("isSet", isSet) |
| 292 | .toString(); |
| 293 | } |
| 294 | } |