Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 28 | private final short id; |
| 29 | private final String name; |
| 30 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Creates a new application ID. |
| 33 | * |
| 34 | * @param id application identifier |
| 35 | * @param name application name |
| 36 | */ |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 37 | public DefaultApplicationId(int id, String name) { |
| 38 | checkArgument(0 <= id && id <= Short.MAX_VALUE, "id is outside range"); |
| 39 | this.id = (short) id; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 40 | this.name = name; |
| 41 | } |
| 42 | |
| 43 | // Constructor for serializers. |
| 44 | private DefaultApplicationId() { |
| 45 | this.id = 0; |
| 46 | this.name = null; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public short id() { |
| 51 | return id; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public String name() { |
| 56 | return name; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public int hashCode() { |
Jian Li | 3756cc3 | 2016-01-12 21:18:20 -0800 | [diff] [blame] | 61 | return Objects.hash(name); |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public boolean equals(Object obj) { |
| 66 | if (this == obj) { |
| 67 | return true; |
| 68 | } |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 69 | if (obj instanceof DefaultApplicationId) { |
| 70 | DefaultApplicationId other = (DefaultApplicationId) obj; |
Jian Li | 3756cc3 | 2016-01-12 21:18:20 -0800 | [diff] [blame] | 71 | return Objects.equals(this.name, other.name); |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 72 | } |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 73 | return false; |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 74 | } |
tom | a689779 | 2014-10-08 22:21:05 -0700 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | public String toString() { |
| 78 | return toStringHelper(this).add("id", id).add("name", name).toString(); |
| 79 | } |
| 80 | |
alshabib | 92c65ad | 2014-10-08 21:56:05 -0700 | [diff] [blame] | 81 | } |