Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @since 3.0.0
*/
@AutoValue
public abstract class BannerComponents implements Serializable {
public abstract class BannerComponents implements Serializable, Comparable<BannerComponents> {

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

/**
* Allows ability to sort/compare by abbreviation priority. This is null-safe for values of
* abbreviationPriority, and treats BannerComponents with a null abreviationPriority as having an
* abbreviationPriority of infinity. This method returns a negative integer, zero, or a positive
* integer as this object is less than, equal to, or greater than the specified object.
*
* @param bannerComponents to compare to
* @return the compareTo int value
* @since 3.0.0
*/
@Override
public int compareTo(BannerComponents bannerComponents) {
Integer ab1 = this.abbreviationPriority();
Integer ab2 = bannerComponents.abbreviationPriority();

if (ab1 == null && ab2 == null) {
return 0;
} else if (ab1 == null) {
return 1;
} else if (ab2 == null) {
return -1;
} else {
return ab1.compareTo(ab2);
}
}

/**
* This builder can be used to set the values describing the {@link BannerComponents}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,102 @@ public void testSerializable() throws Exception {
byte[] serialized = TestUtils.serialize(bannerComponents);
assertEquals(bannerComponents, deserialize(serialized, BannerComponents.class));
}

@Test
public void compareTo_lessThan() {
BannerComponents bannerComponents1 = BannerComponents.builder()
.text("test")
.type("text")
.abbreviationPriority(2)
.build();

BannerComponents bannerComponents2 = BannerComponents.builder()
.text("test")
.type("text")
.abbreviationPriority(3)
.build();

assertEquals(-1, bannerComponents1.compareTo(bannerComponents2));
}

@Test
public void compareTo_greaterThan() {
BannerComponents bannerComponents1 = BannerComponents.builder()
.text("test")
.type("text")
.abbreviationPriority(2)
.build();

BannerComponents bannerComponents2 = BannerComponents.builder()
.text("test")
.type("text")
.abbreviationPriority(3)
.build();

assertEquals(1, bannerComponents2.compareTo(bannerComponents1));
}

@Test
public void compareTo_equals() {
BannerComponents bannerComponents1 = BannerComponents.builder()
.text("test")
.type("text")
.abbreviationPriority(3)
.build();

BannerComponents bannerComponents2 = BannerComponents.builder()
.text("test")
.type("text")
.abbreviationPriority(3)
.build();

assertEquals(0, bannerComponents2.compareTo(bannerComponents1));
}

@Test
public void compareTo_firstIsNull() {
BannerComponents bannerComponents1 = BannerComponents.builder()
.text("test")
.type("text")
.build();

BannerComponents bannerComponents2 = BannerComponents.builder()
.text("test")
.type("text")
.abbreviationPriority(3)
.build();

assertEquals(1, bannerComponents1.compareTo(bannerComponents2));
}

@Test
public void compareTo_secondIsNull() {
BannerComponents bannerComponents1 = BannerComponents.builder()
.text("test")
.type("text")
.abbreviationPriority(3)
.build();

BannerComponents bannerComponents2 = BannerComponents.builder()
.text("test")
.type("text")
.build();

assertEquals(-1, bannerComponents1.compareTo(bannerComponents2));
}

@Test
public void compareTo_bothAreNull() {
BannerComponents bannerComponents1 = BannerComponents.builder()
.text("test")
.type("text")
.build();

BannerComponents bannerComponents2 = BannerComponents.builder()
.text("test")
.type("text")
.build();

assertEquals(0, bannerComponents1.compareTo(bannerComponents2));
}
}