| /* |
| * Copyright 2016-present Open Networking Foundation |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.onosproject.nemo.model.common; |
| |
| import java.beans.ConstructorProperties; |
| import java.io.Serializable; |
| import java.util.Objects; |
| import java.util.regex.Pattern; |
| |
| import static com.google.common.base.Preconditions.checkArgument; |
| import static com.google.common.base.Preconditions.checkNotNull; |
| |
| /** |
| * A generic string name type. Names must start with a letter. |
| */ |
| public class Name implements Serializable { |
| private static final long serialVersionUID = -1664832583906649664L; |
| |
| // package-private for unit test access |
| static final int MIN_LENGTH = 1; |
| static final int MAX_LENGTH = 256; |
| |
| static final String E_NULL_VALUE = "Supplied value may not be null"; |
| static final String E_BAD_FORMAT = |
| "Supplied value \"%s\" does not match required pattern \"%s\""; |
| static String E_BAD_LENGTH = "Invalid length: %d, expected: [[" + |
| MIN_LENGTH + "‥" + MAX_LENGTH + "]]."; |
| |
| static final String NAME_PATTERN = "^[a-zA-Z]([a-zA-Z0-9\\-_.])*$"; |
| |
| private static final Pattern RE_NAME = Pattern.compile(NAME_PATTERN); |
| |
| private final String value; |
| |
| /** |
| * Creates a new instance from a given name. |
| * |
| * @param value name |
| */ |
| @ConstructorProperties("value") |
| public Name(String value) { |
| checkNotNull(value, E_NULL_VALUE); |
| checkValueLength(value); |
| checkArgument(RE_NAME.matcher(value).matches(), |
| E_BAD_FORMAT, value, NAME_PATTERN); |
| this.value = value; |
| } |
| |
| /** |
| * Creates a copy from the given name object. |
| * |
| * @param source name object |
| */ |
| public Name(Name source) { |
| this.value = source.value; |
| } |
| |
| /** |
| * Returns the value of this instance. |
| * |
| * @return the value |
| */ |
| public String getValue() { |
| return value; |
| } |
| |
| @Override |
| public int hashCode() { |
| return value.hashCode(); |
| } |
| |
| @Override |
| public boolean equals(Object obj) { |
| if (this == obj) { |
| return true; |
| } |
| if (obj == null) { |
| return false; |
| } |
| if (getClass() != obj.getClass()) { |
| return false; |
| } |
| Name other = (Name) obj; |
| return Objects.equals(value, other.value); |
| } |
| |
| private static void checkValueLength(String value) { |
| int length = value.length(); |
| if (length < MIN_LENGTH || length > MAX_LENGTH) { |
| throw new IllegalArgumentException(String.format(E_BAD_LENGTH, length)); |
| } |
| } |
| } |
| |