blob: 71e3a3af1142f27a6d3ed4bc12f4a9f4046409d6 [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
Jordan Halterman0d89ea32017-06-13 10:42:36 -070028 private static final int NAME_MAX_LENGTH = 1024;
alshabib92c65ad2014-10-08 21:56:05 -070029 private final short id;
30 private final String name;
31
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070032 /**
33 * Creates a new application ID.
34 *
35 * @param id application identifier
36 * @param name application name
37 */
Thomas Vachuska02aeb032015-01-06 22:36:30 -080038 public DefaultApplicationId(int id, String name) {
39 checkArgument(0 <= id && id <= Short.MAX_VALUE, "id is outside range");
Jordan Halterman0d89ea32017-06-13 10:42:36 -070040 if (name != null) {
41 checkArgument(name.length() <= NAME_MAX_LENGTH, "name exceeds maximum length " + NAME_MAX_LENGTH);
42 }
Thomas Vachuska02aeb032015-01-06 22:36:30 -080043 this.id = (short) id;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070044 this.name = name;
45 }
46
47 // Constructor for serializers.
48 private DefaultApplicationId() {
49 this.id = 0;
50 this.name = null;
alshabib92c65ad2014-10-08 21:56:05 -070051 }
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 Li3756cc32016-01-12 21:18:20 -080065 return Objects.hash(name);
alshabib92c65ad2014-10-08 21:56:05 -070066 }
67
68 @Override
69 public boolean equals(Object obj) {
70 if (this == obj) {
71 return true;
72 }
toma6897792014-10-08 22:21:05 -070073 if (obj instanceof DefaultApplicationId) {
74 DefaultApplicationId other = (DefaultApplicationId) obj;
Jian Li3756cc32016-01-12 21:18:20 -080075 return Objects.equals(this.name, other.name);
alshabib92c65ad2014-10-08 21:56:05 -070076 }
toma6897792014-10-08 22:21:05 -070077 return false;
alshabib92c65ad2014-10-08 21:56:05 -070078 }
toma6897792014-10-08 22:21:05 -070079
80 @Override
81 public String toString() {
82 return toStringHelper(this).add("id", id).add("name", name).toString();
83 }
84
alshabib92c65ad2014-10-08 21:56:05 -070085}