blob: 867c8fc3bd0ab96615d91e8ed5feab7ddee0048e [file] [log] [blame]
anjiaqi12345677cc5e712016-10-18 16:07:06 +08001/*
Brian O'Connor23c7e322017-08-03 18:48:27 -07002 * Copyright 2016-present Open Networking Foundation
anjiaqi12345677cc5e712016-10-18 16:07:06 +08003 *
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 */
16package org.onosproject.nemo.model.common;
17
18import java.beans.ConstructorProperties;
19import java.io.Serializable;
20import java.util.Objects;
21import java.util.regex.Pattern;
22
23import static com.google.common.base.Preconditions.checkArgument;
24import static com.google.common.base.Preconditions.checkNotNull;
25
26/**
27 * A generic string name type. Names must start with a letter.
28 */
29public class Name implements Serializable {
30 private static final long serialVersionUID = -1664832583906649664L;
31
32 // package-private for unit test access
33 static final int MIN_LENGTH = 1;
34 static final int MAX_LENGTH = 256;
35
36 static final String E_NULL_VALUE = "Supplied value may not be null";
37 static final String E_BAD_FORMAT =
38 "Supplied value \"%s\" does not match required pattern \"%s\"";
39 static String E_BAD_LENGTH = "Invalid length: %d, expected: [[" +
40 MIN_LENGTH + "‥" + MAX_LENGTH + "]].";
41
42 static final String NAME_PATTERN = "^[a-zA-Z]([a-zA-Z0-9\\-_.])*$";
43
44 private static final Pattern RE_NAME = Pattern.compile(NAME_PATTERN);
45
46 private final String value;
47
48 /**
49 * Creates a new instance from a given name.
50 *
51 * @param value name
52 */
53 @ConstructorProperties("value")
54 public Name(String value) {
55 checkNotNull(value, E_NULL_VALUE);
56 checkValueLength(value);
57 checkArgument(RE_NAME.matcher(value).matches(),
58 E_BAD_FORMAT, value, NAME_PATTERN);
59 this.value = value;
60 }
61
62 /**
63 * Creates a copy from the given name object.
64 *
65 * @param source name object
66 */
67 public Name(Name source) {
68 this.value = source.value;
69 }
70
71 /**
72 * Returns the value of this instance.
73 *
74 * @return the value
75 */
76 public String getValue() {
77 return value;
78 }
79
80 @Override
81 public int hashCode() {
82 return value.hashCode();
83 }
84
85 @Override
86 public boolean equals(Object obj) {
87 if (this == obj) {
88 return true;
89 }
90 if (obj == null) {
91 return false;
92 }
93 if (getClass() != obj.getClass()) {
94 return false;
95 }
96 Name other = (Name) obj;
97 return Objects.equals(value, other.value);
98 }
99
100 private static void checkValueLength(String value) {
101 int length = value.length();
102 if (length < MIN_LENGTH || length > MAX_LENGTH) {
103 throw new IllegalArgumentException(String.format(E_BAD_LENGTH, length));
104 }
105 }
106}
107