Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.core; |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 17 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 18 | import java.util.Objects; |
| 19 | |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 21 | import static com.google.common.base.Preconditions.checkArgument; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 24 | * Application identifier. |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 25 | */ |
| 26 | public class DefaultApplicationId implements ApplicationId { |
| 27 | |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 28 | private static final int NAME_MAX_LENGTH = 1024; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 29 | private final short id; |
| 30 | private final String name; |
| 31 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Creates a new application ID. |
| 34 | * |
| 35 | * @param id application identifier |
| 36 | * @param name application name |
| 37 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 38 | public DefaultApplicationId(int id, String name) { |
| 39 | checkArgument(0 <= id && id <= Short.MAX_VALUE, "id is outside range"); |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 40 | if (name != null) { |
| 41 | checkArgument(name.length() <= NAME_MAX_LENGTH, "name exceeds maximum length " + NAME_MAX_LENGTH); |
| 42 | } |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 43 | this.id = (short) id; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 44 | this.name = name; |
| 45 | } |
| 46 | |
| 47 | // Constructor for serializers. |
| 48 | private DefaultApplicationId() { |
| 49 | this.id = 0; |
| 50 | this.name = null; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public short id() { |
| 55 | return id; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String name() { |
| 60 | return name; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int hashCode() { |
Jian Li | 3756cc3 | 2016-01-12 21:18:20 -0800 | [diff] [blame] | 65 | return Objects.hash(name); |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean equals(Object obj) { |
| 70 | if (this == obj) { |
| 71 | return true; |
| 72 | } |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 73 | if (obj instanceof DefaultApplicationId) { |
| 74 | DefaultApplicationId other = (DefaultApplicationId) obj; |
Jian Li | 3756cc3 | 2016-01-12 21:18:20 -0800 | [diff] [blame] | 75 | return Objects.equals(this.name, other.name); |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 76 | } |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 77 | return false; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 78 | } |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 79 | |
| 80 | @Override |
| 81 | public String toString() { |
| 82 | return toStringHelper(this).add("id", id).add("name", name).toString(); |
| 83 | } |
| 84 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 85 | } |