blob: 39852de5e2a76a104cc3301e8209f6e5108bee4e [file] [log] [blame]
Madan Jampaniab7e7cd2016-01-14 14:02:32 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampaniab7e7cd2016-01-14 14:02:32 -08003 *
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
Jian Lib6d998e2016-02-29 11:41:18 -080018import org.onlab.util.Identifier;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080019
Jian Lib6d998e2016-02-29 11:41:18 -080020import static com.google.common.base.Preconditions.checkArgument;
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080021
22/**
23 * {@link Partition} identifier.
24 */
Jian Lib6d998e2016-02-29 11:41:18 -080025public class PartitionId extends Identifier<Integer> implements Comparable<PartitionId> {
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080026
27 /**
Jordan Halterman980a8c12017-09-22 18:01:19 -070028 * The {@code PartitionId} for the shared coordination partition.
29 */
30 public static final PartitionId SHARED = PartitionId.from(0);
31
32 /**
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080033 * Creates a partition identifier from an integer.
34 *
35 * @param id input integer
36 */
37 public PartitionId(int id) {
Jian Lib6d998e2016-02-29 11:41:18 -080038 super(id);
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080039 checkArgument(id >= 0, "partition id must be non-negative");
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080040 }
41
42 /**
43 * Creates a partition identifier from an integer.
44 *
45 * @param id input integer
Jian Lidfba7392016-01-22 16:46:58 -080046 * @return partition identification
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080047 */
48 public static PartitionId from(int id) {
49 return new PartitionId(id);
50 }
51
52 /**
53 * Returns the partition identifier as an integer.
54 * @return number
55 */
56 public int asInt() {
Jian Lib6d998e2016-02-29 11:41:18 -080057 return id();
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080058 }
59
60 @Override
61 public int compareTo(PartitionId that) {
Jian Lib6d998e2016-02-29 11:41:18 -080062 return Integer.compare(this.identifier, that.identifier);
Madan Jampaniab7e7cd2016-01-14 14:02:32 -080063 }
64}