blob: ef46bf2fe2a86cd187f7220fc01462874598447c [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.core;
toma6897792014-10-08 22:21:05 -070017
alshabib92c65ad2014-10-08 21:56:05 -070018import java.util.Objects;
19
toma6897792014-10-08 22:21:05 -070020import static com.google.common.base.MoreObjects.toStringHelper;
Thomas Vachuska02aeb032015-01-06 22:36:30 -080021import static com.google.common.base.Preconditions.checkArgument;
alshabib92c65ad2014-10-08 21:56:05 -070022
23/**
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070024 * Application identifier.
alshabib92c65ad2014-10-08 21:56:05 -070025 */
26public class DefaultApplicationId implements ApplicationId {
27
alshabib92c65ad2014-10-08 21:56:05 -070028 private final short id;
29 private final String name;
30
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070031 /**
32 * Creates a new application ID.
33 *
34 * @param id application identifier
35 * @param name application name
36 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080037 public DefaultApplicationId(int id, String name) {
38 checkArgument(0 <= id && id <= Short.MAX_VALUE, "id is outside range");
39 this.id = (short) id;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070040 this.name = name;
41 }
42
43 // Constructor for serializers.
44 private DefaultApplicationId() {
45 this.id = 0;
46 this.name = null;
alshabib92c65ad2014-10-08 21:56:05 -070047 }
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 Li3756cc32016-01-12 21:18:20 -080061 return Objects.hash(name);
alshabib92c65ad2014-10-08 21:56:05 -070062 }
63
64 @Override
65 public boolean equals(Object obj) {
66 if (this == obj) {
67 return true;
68 }
toma6897792014-10-08 22:21:05 -070069 if (obj instanceof DefaultApplicationId) {
70 DefaultApplicationId other = (DefaultApplicationId) obj;
Jian Li3756cc32016-01-12 21:18:20 -080071 return Objects.equals(this.name, other.name);
alshabib92c65ad2014-10-08 21:56:05 -070072 }
toma6897792014-10-08 22:21:05 -070073 return false;
alshabib92c65ad2014-10-08 21:56:05 -070074 }
toma6897792014-10-08 22:21:05 -070075
76 @Override
77 public String toString() {
78 return toStringHelper(this).add("id", id).add("name", name).toString();
79 }
80
alshabib92c65ad2014-10-08 21:56:05 -070081}