blob: 1e73d0e5aef051a4a43d8c373c567007f203da06 [file] [log] [blame]
Madan Jampaniab7e7cd2016-01-14 14:02:32 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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 */
16package org.onosproject.cluster;
17
18import static com.google.common.base.Preconditions.checkArgument;
19
20import java.util.Objects;
21
22/**
23 * {@link Partition} identifier.
24 */
25public class PartitionId implements Comparable<PartitionId> {
26
27 private final int id;
28
29 /**
30 * Creates a partition identifier from an integer.
31 *
32 * @param id input integer
33 */
34 public PartitionId(int id) {
35 checkArgument(id >= 0, "partition id must be non-negative");
36 this.id = id;
37 }
38
39 /**
40 * Creates a partition identifier from an integer.
41 *
42 * @param id input integer
43 */
44 public static PartitionId from(int id) {
45 return new PartitionId(id);
46 }
47
48 /**
49 * Returns the partition identifier as an integer.
50 * @return number
51 */
52 public int asInt() {
53 return id;
54 }
55
56 @Override
57 public int hashCode() {
58 return id;
59 }
60
61 @Override
62 public boolean equals(Object obj) {
63 if (this == obj) {
64 return true;
65 }
66 if (obj instanceof PartitionId) {
67 final PartitionId other = (PartitionId) obj;
68 return Objects.equals(this.id, other.id);
69 }
70 return false;
71 }
72
73 @Override
74 public String toString() {
75 return String.valueOf(id);
76 }
77
78 @Override
79 public int compareTo(PartitionId that) {
80 return Integer.compare(this.id, that.id);
81 }
82}