Skip to content

Commit 2818ce8

Browse files
author
Devota Aabel
authored
Implemented Comparable in BannerComponents (#768)
1 parent 3847436 commit 2818ce8

2 files changed

Lines changed: 126 additions & 2 deletions

File tree

services-directions/src/main/java/com/mapbox/api/directions/v5/models/BannerComponents.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @since 3.0.0
2020
*/
2121
@AutoValue
22-
public abstract class BannerComponents implements Serializable {
22+
public abstract class BannerComponents implements Serializable, Comparable<BannerComponents> {
2323

2424
/**
2525
* Create a new instance of this class by using the {@link Builder} class.
@@ -94,7 +94,7 @@ public static Builder builder() {
9494
* icon that's included to better identify to your user to roadway. Note that this doesn't
9595
* return the image itself but rather the url which can be used to download the file.
9696
*
97-
* @return the url which can be used to download the shield icon if one is avaliable
97+
* @return the url which can be used to download the shield icon if one is available
9898
* @since 3.0.0
9999
*/
100100
@Nullable
@@ -112,6 +112,32 @@ public static TypeAdapter<BannerComponents> typeAdapter(Gson gson) {
112112
return new AutoValue_BannerComponents.GsonTypeAdapter(gson);
113113
}
114114

115+
/**
116+
* Allows ability to sort/compare by abbreviation priority. This is null-safe for values of
117+
* abbreviationPriority, and treats BannerComponents with a null abreviationPriority as having an
118+
* abbreviationPriority of infinity. This method returns a negative integer, zero, or a positive
119+
* integer as this object is less than, equal to, or greater than the specified object.
120+
*
121+
* @param bannerComponents to compare to
122+
* @return the compareTo int value
123+
* @since 3.0.0
124+
*/
125+
@Override
126+
public int compareTo(BannerComponents bannerComponents) {
127+
Integer ab1 = this.abbreviationPriority();
128+
Integer ab2 = bannerComponents.abbreviationPriority();
129+
130+
if (ab1 == null && ab2 == null) {
131+
return 0;
132+
} else if (ab1 == null) {
133+
return 1;
134+
} else if (ab2 == null) {
135+
return -1;
136+
} else {
137+
return ab1.compareTo(ab2);
138+
}
139+
}
140+
115141
/**
116142
* This builder can be used to set the values describing the {@link BannerComponents}.
117143
*

services-directions/src/test/java/com/mapbox/api/directions/v5/models/BannerComponentTest.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,102 @@ public void testSerializable() throws Exception {
2828
byte[] serialized = TestUtils.serialize(bannerComponents);
2929
assertEquals(bannerComponents, deserialize(serialized, BannerComponents.class));
3030
}
31+
32+
@Test
33+
public void compareTo_lessThan() {
34+
BannerComponents bannerComponents1 = BannerComponents.builder()
35+
.text("test")
36+
.type("text")
37+
.abbreviationPriority(2)
38+
.build();
39+
40+
BannerComponents bannerComponents2 = BannerComponents.builder()
41+
.text("test")
42+
.type("text")
43+
.abbreviationPriority(3)
44+
.build();
45+
46+
assertEquals(-1, bannerComponents1.compareTo(bannerComponents2));
47+
}
48+
49+
@Test
50+
public void compareTo_greaterThan() {
51+
BannerComponents bannerComponents1 = BannerComponents.builder()
52+
.text("test")
53+
.type("text")
54+
.abbreviationPriority(2)
55+
.build();
56+
57+
BannerComponents bannerComponents2 = BannerComponents.builder()
58+
.text("test")
59+
.type("text")
60+
.abbreviationPriority(3)
61+
.build();
62+
63+
assertEquals(1, bannerComponents2.compareTo(bannerComponents1));
64+
}
65+
66+
@Test
67+
public void compareTo_equals() {
68+
BannerComponents bannerComponents1 = BannerComponents.builder()
69+
.text("test")
70+
.type("text")
71+
.abbreviationPriority(3)
72+
.build();
73+
74+
BannerComponents bannerComponents2 = BannerComponents.builder()
75+
.text("test")
76+
.type("text")
77+
.abbreviationPriority(3)
78+
.build();
79+
80+
assertEquals(0, bannerComponents2.compareTo(bannerComponents1));
81+
}
82+
83+
@Test
84+
public void compareTo_firstIsNull() {
85+
BannerComponents bannerComponents1 = BannerComponents.builder()
86+
.text("test")
87+
.type("text")
88+
.build();
89+
90+
BannerComponents bannerComponents2 = BannerComponents.builder()
91+
.text("test")
92+
.type("text")
93+
.abbreviationPriority(3)
94+
.build();
95+
96+
assertEquals(1, bannerComponents1.compareTo(bannerComponents2));
97+
}
98+
99+
@Test
100+
public void compareTo_secondIsNull() {
101+
BannerComponents bannerComponents1 = BannerComponents.builder()
102+
.text("test")
103+
.type("text")
104+
.abbreviationPriority(3)
105+
.build();
106+
107+
BannerComponents bannerComponents2 = BannerComponents.builder()
108+
.text("test")
109+
.type("text")
110+
.build();
111+
112+
assertEquals(-1, bannerComponents1.compareTo(bannerComponents2));
113+
}
114+
115+
@Test
116+
public void compareTo_bothAreNull() {
117+
BannerComponents bannerComponents1 = BannerComponents.builder()
118+
.text("test")
119+
.type("text")
120+
.build();
121+
122+
BannerComponents bannerComponents2 = BannerComponents.builder()
123+
.text("test")
124+
.type("text")
125+
.build();
126+
127+
assertEquals(0, bannerComponents1.compareTo(bannerComponents2));
128+
}
31129
}

0 commit comments

Comments
 (0)