blob: 81dd07c76da135a0749129de52a89dbd1d5cb8a2 [file] [log] [blame]
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -07001/*
2 * Copyright 2016-present 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.onlab.util;
17
18import com.google.common.annotations.Beta;
19import com.google.common.collect.DiscreteDomain;
20import com.google.common.collect.Range;
21
22import java.util.Objects;
23
24/**
25 * Represent a closed-open range.
26 * The primary user of this class is the ResourceService implementation.
27 */
28@Beta
29public final class ClosedOpenRange {
30 private final int lowerBound; // inclusive
31 private final int upperBound; // exclusive
32
33 /**
34 * Creates a range from a Guava's range.
35 *
36 * @param range Guava's range
37 * @return this range
38 */
39 public static ClosedOpenRange of(Range<Integer> range) {
40 return new ClosedOpenRange(
41 range.canonical(DiscreteDomain.integers()).lowerEndpoint(),
42 range.canonical(DiscreteDomain.integers()).upperEndpoint());
43 }
44
45 /**
46 * Create a range with a lower bound and an upper bound.
47 *
48 * @param lowerBound lower bound (inclusive)
49 * @param upperBound upper bound (exclusive)
50 * @return this range
51 */
52 public static ClosedOpenRange of(int lowerBound, int upperBound) {
53 return new ClosedOpenRange(lowerBound, upperBound);
54 }
55
56 private ClosedOpenRange(int lowerBound, int upperBound) {
57 this.lowerBound = lowerBound;
58 this.upperBound = upperBound;
59 }
60
61 /**
62 * Returns the lower bound.
63 *
64 * @return the lower bound
65 */
66 public int lowerBound() {
67 return lowerBound;
68 }
69
70 /**
71 * Returns the upper bound.
72 *
73 * @return the upper bound
74 */
75 public int upperBound() {
76 return upperBound;
77 }
78
79 @Override
80 public int hashCode() {
81 return Objects.hash(lowerBound, upperBound);
82 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (this == obj) {
87 return true;
88 }
89
90 if (!(obj instanceof ClosedOpenRange)) {
91 return false;
92 }
93
94 final ClosedOpenRange other = (ClosedOpenRange) obj;
95 return Objects.equals(this.lowerBound, other.lowerBound)
96 && Objects.equals(this.upperBound, other.upperBound);
97 }
98
99 @Override
100 public String toString() {
101 return "[" + lowerBound + ".." + upperBound + ")";
102 }
103}