Committed by
Gerrit Code Review
Implement methods to compare objects in classes representing unit
Methods are added to the following class. - Bandwidth - Frequency To reduce redundant code, RichComparable interface is defined. Change-Id: Iec96bee9754c6dfca62255b8b4068925d3be13a1
Showing
5 changed files
with
107 additions
and
2 deletions
... | @@ -15,13 +15,15 @@ | ... | @@ -15,13 +15,15 @@ |
15 | */ | 15 | */ |
16 | package org.onlab.util; | 16 | package org.onlab.util; |
17 | 17 | ||
18 | +import com.google.common.collect.ComparisonChain; | ||
19 | + | ||
18 | import java.util.Objects; | 20 | import java.util.Objects; |
19 | 21 | ||
20 | /** | 22 | /** |
21 | * Representation of bandwidth. | 23 | * Representation of bandwidth. |
22 | * Use the static factory method corresponding to the unit (like Kbps) you desire on instantiation. | 24 | * Use the static factory method corresponding to the unit (like Kbps) you desire on instantiation. |
23 | */ | 25 | */ |
24 | -public final class Bandwidth { | 26 | +public final class Bandwidth implements RichComparable<Bandwidth> { |
25 | 27 | ||
26 | private final double bps; | 28 | private final double bps; |
27 | 29 | ||
... | @@ -89,6 +91,13 @@ public final class Bandwidth { | ... | @@ -89,6 +91,13 @@ public final class Bandwidth { |
89 | } | 91 | } |
90 | 92 | ||
91 | @Override | 93 | @Override |
94 | + public int compareTo(Bandwidth other) { | ||
95 | + return ComparisonChain.start() | ||
96 | + .compare(this.bps, other.bps) | ||
97 | + .result(); | ||
98 | + } | ||
99 | + | ||
100 | + @Override | ||
92 | public boolean equals(Object obj) { | 101 | public boolean equals(Object obj) { |
93 | if (obj instanceof Bandwidth) { | 102 | if (obj instanceof Bandwidth) { |
94 | Bandwidth that = (Bandwidth) obj; | 103 | Bandwidth that = (Bandwidth) obj; | ... | ... |
... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
16 | package org.onlab.util; | 16 | package org.onlab.util; |
17 | 17 | ||
18 | import com.google.common.base.MoreObjects; | 18 | import com.google.common.base.MoreObjects; |
19 | +import com.google.common.collect.ComparisonChain; | ||
19 | 20 | ||
20 | import java.util.Objects; | 21 | import java.util.Objects; |
21 | 22 | ||
... | @@ -30,7 +31,7 @@ import java.util.Objects; | ... | @@ -30,7 +31,7 @@ import java.util.Objects; |
30 | * sub-Hz accuracy. | 31 | * sub-Hz accuracy. |
31 | * </p> | 32 | * </p> |
32 | */ | 33 | */ |
33 | -public final class Frequency { | 34 | +public final class Frequency implements RichComparable<Frequency> { |
34 | 35 | ||
35 | private static final long KHZ = 1_000L; | 36 | private static final long KHZ = 1_000L; |
36 | private static final long MHZ = 1_000_000L; | 37 | private static final long MHZ = 1_000_000L; |
... | @@ -138,6 +139,13 @@ public final class Frequency { | ... | @@ -138,6 +139,13 @@ public final class Frequency { |
138 | } | 139 | } |
139 | 140 | ||
140 | @Override | 141 | @Override |
142 | + public int compareTo(Frequency other) { | ||
143 | + return ComparisonChain.start() | ||
144 | + .compare(this.frequency, other.frequency) | ||
145 | + .result(); | ||
146 | + } | ||
147 | + | ||
148 | + @Override | ||
141 | public int hashCode() { | 149 | public int hashCode() { |
142 | return Objects.hash(frequency); | 150 | return Objects.hash(frequency); |
143 | } | 151 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 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.onlab.util; | ||
17 | + | ||
18 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
19 | + | ||
20 | +/** | ||
21 | + * Extends useful methods for comparison to {@link Comparable} interface. | ||
22 | + * | ||
23 | + * @param <T> type of instance to be compared | ||
24 | + */ | ||
25 | +public interface RichComparable<T> extends Comparable<T> { | ||
26 | + /** | ||
27 | + * Compares if this object is less than the specified object. | ||
28 | + * | ||
29 | + * @param other the object to be compared | ||
30 | + * @return true if this object is less than the specified object | ||
31 | + */ | ||
32 | + default boolean isLessThan(T other) { | ||
33 | + return compareTo(checkNotNull(other)) < 0; | ||
34 | + } | ||
35 | + | ||
36 | + /** | ||
37 | + * Compares if this object is greater than the specified object. | ||
38 | + * | ||
39 | + * @param other the object to be compared | ||
40 | + * @return true if this object is less thant the specified object | ||
41 | + */ | ||
42 | + default boolean isGreaterThan(T other) { | ||
43 | + return compareTo(checkNotNull(other)) > 0; | ||
44 | + } | ||
45 | +} |
... | @@ -18,10 +18,19 @@ package org.onlab.util; | ... | @@ -18,10 +18,19 @@ package org.onlab.util; |
18 | import com.google.common.testing.EqualsTester; | 18 | import com.google.common.testing.EqualsTester; |
19 | import org.junit.Test; | 19 | import org.junit.Test; |
20 | 20 | ||
21 | +import static org.hamcrest.Matchers.greaterThan; | ||
22 | +import static org.hamcrest.Matchers.is; | ||
23 | +import static org.hamcrest.Matchers.lessThan; | ||
24 | +import static org.junit.Assert.assertThat; | ||
25 | + | ||
21 | /** | 26 | /** |
22 | * Unit tests for Bandwidth. | 27 | * Unit tests for Bandwidth. |
23 | */ | 28 | */ |
24 | public class BandwidthTest { | 29 | public class BandwidthTest { |
30 | + | ||
31 | + private final Bandwidth small = Bandwidth.kbps(100.0); | ||
32 | + private final Bandwidth large = Bandwidth.mbps(1.0); | ||
33 | + | ||
25 | /** | 34 | /** |
26 | * Tests equality of Bandwidth instances. | 35 | * Tests equality of Bandwidth instances. |
27 | */ | 36 | */ |
... | @@ -33,4 +42,21 @@ public class BandwidthTest { | ... | @@ -33,4 +42,21 @@ public class BandwidthTest { |
33 | .testEquals(); | 42 | .testEquals(); |
34 | } | 43 | } |
35 | 44 | ||
45 | + /** | ||
46 | + * Tests if the first object is less than the second object. | ||
47 | + */ | ||
48 | + @Test | ||
49 | + public void testLessThan() { | ||
50 | + assertThat(small, is(lessThan(large))); | ||
51 | + assertThat(small.isLessThan(large), is(true)); | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * Tests if the first object is greater than the second object. | ||
56 | + */ | ||
57 | + @Test | ||
58 | + public void testGreaterThan() { | ||
59 | + assertThat(large, is(greaterThan(small))); | ||
60 | + assertThat(large.isGreaterThan(small), is(true)); | ||
61 | + } | ||
36 | } | 62 | } | ... | ... |
... | @@ -20,7 +20,9 @@ import org.junit.Test; | ... | @@ -20,7 +20,9 @@ import org.junit.Test; |
20 | import org.onlab.junit.ImmutableClassChecker; | 20 | import org.onlab.junit.ImmutableClassChecker; |
21 | 21 | ||
22 | import static org.hamcrest.MatcherAssert.assertThat; | 22 | import static org.hamcrest.MatcherAssert.assertThat; |
23 | +import static org.hamcrest.Matchers.greaterThan; | ||
23 | import static org.hamcrest.Matchers.is; | 24 | import static org.hamcrest.Matchers.is; |
25 | +import static org.hamcrest.Matchers.lessThan; | ||
24 | 26 | ||
25 | public class FrequencyTest { | 27 | public class FrequencyTest { |
26 | 28 | ||
... | @@ -53,6 +55,21 @@ public class FrequencyTest { | ... | @@ -53,6 +55,21 @@ public class FrequencyTest { |
53 | } | 55 | } |
54 | 56 | ||
55 | /** | 57 | /** |
58 | + * Tests the first object is less than the second object. | ||
59 | + */ | ||
60 | + @Test | ||
61 | + public void testLessThan() { | ||
62 | + assertThat(frequency1, is(lessThan(frequency2))); | ||
63 | + assertThat(frequency1.isLessThan(frequency2), is(true)); | ||
64 | + } | ||
65 | + | ||
66 | + @Test | ||
67 | + public void testGreaterThan() { | ||
68 | + assertThat(frequency2, is(greaterThan(frequency1))); | ||
69 | + assertThat(frequency2.isGreaterThan(frequency1), is(true)); | ||
70 | + } | ||
71 | + | ||
72 | + /** | ||
56 | * Tests add operation of two Frequencies. | 73 | * Tests add operation of two Frequencies. |
57 | */ | 74 | */ |
58 | @Test | 75 | @Test | ... | ... |
-
Please register or login to post a comment