Skip to content

Commit 1863d67

Browse files
author
Chase Coalwell
authored
Implement comparable, add static compare method (#1192)
* Implement comparable, add static compare method
1 parent ce3aa97 commit 1863d67

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

smithy-model/src/main/java/software/amazon/smithy/model/FromSourceLocation.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ public interface FromSourceLocation {
2828
default SourceLocation getSourceLocation() {
2929
return SourceLocation.none();
3030
}
31+
32+
/**
33+
* Compares two FromSourceLocations.
34+
*
35+
* @param s1 the first FromSourceLocation to compare.
36+
* @param s2 the second FromSourceLocation to compare.
37+
* @return the value 0 if s1 == s2; a value less than 0 if s1 < s2; and a value greater than 0 if s1 > s2.
38+
*/
39+
static int compare(FromSourceLocation s1, FromSourceLocation s2) {
40+
return s1.getSourceLocation().compareTo(s2.getSourceLocation());
41+
}
3142
}

smithy-model/src/main/java/software/amazon/smithy/model/SourceLocation.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Represents the source location of a model component.
2222
*/
23-
public final class SourceLocation implements FromSourceLocation {
23+
public final class SourceLocation implements FromSourceLocation, Comparable<SourceLocation> {
2424

2525
public static final SourceLocation NONE = new SourceLocation("N/A");
2626

@@ -97,4 +97,18 @@ public int hashCode() {
9797

9898
return h;
9999
}
100+
101+
@Override
102+
public int compareTo(SourceLocation o) {
103+
if (!this.getFilename().equals(o.getFilename())) {
104+
return this.getFilename().compareTo(o.getFilename());
105+
}
106+
107+
int lineComparison = Integer.compare(this.getLine(), o.getLine());
108+
if (lineComparison != 0) {
109+
return lineComparison;
110+
}
111+
112+
return Integer.compare(this.getColumn(), o.getColumn());
113+
}
100114
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.smithy.model;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
import java.util.Comparator;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
import org.junit.jupiter.api.Test;
24+
import software.amazon.smithy.model.shapes.Shape;
25+
import software.amazon.smithy.model.shapes.StringShape;
26+
27+
public class SourceLocationTest {
28+
@Test
29+
public void sortsSourceLocations() {
30+
Model model = Model.builder()
31+
.addShape(StringShape.builder()
32+
.id("smithy.example#First")
33+
.source(new SourceLocation("a.smithy", 1, 1))
34+
.build())
35+
.addShape(StringShape.builder()
36+
.id("smithy.example#Second")
37+
.source(new SourceLocation("a.smithy", 1, 2))
38+
.build())
39+
.addShape(StringShape.builder()
40+
.id("smithy.example#Third")
41+
.source(new SourceLocation("a.smithy", 2, 1))
42+
.build())
43+
.addShape(StringShape.builder()
44+
.id("smithy.example#Fourth")
45+
.source(new SourceLocation("b.smithy", 1, 1))
46+
.build())
47+
.build();
48+
49+
List<Shape> shapes = model.shapes()
50+
.sorted(Comparator.comparing(Shape::getSourceLocation))
51+
.collect(Collectors.toList());
52+
assertEquals("First", shapes.get(0).getId().getName());
53+
assertEquals("Second", shapes.get(1).getId().getName());
54+
assertEquals("Third", shapes.get(2).getId().getName());
55+
assertEquals("Fourth", shapes.get(3).getId().getName());
56+
}
57+
}

0 commit comments

Comments
 (0)