Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cluster; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
| 20 | import java.util.Objects; |
| 21 | |
| 22 | /** |
| 23 | * {@link Partition} identifier. |
| 24 | */ |
| 25 | public 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 |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 43 | * @return partition identification |
Madan Jampani | ab7e7cd | 2016-01-14 14:02:32 -0800 | [diff] [blame] | 44 | */ |
| 45 | public static PartitionId from(int id) { |
| 46 | return new PartitionId(id); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the partition identifier as an integer. |
| 51 | * @return number |
| 52 | */ |
| 53 | public int asInt() { |
| 54 | return id; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public int hashCode() { |
| 59 | return id; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public boolean equals(Object obj) { |
| 64 | if (this == obj) { |
| 65 | return true; |
| 66 | } |
| 67 | if (obj instanceof PartitionId) { |
| 68 | final PartitionId other = (PartitionId) obj; |
| 69 | return Objects.equals(this.id, other.id); |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public String toString() { |
| 76 | return String.valueOf(id); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int compareTo(PartitionId that) { |
| 81 | return Integer.compare(this.id, that.id); |
| 82 | } |
| 83 | } |