Skip to content

Commit

Permalink
Implement comparable, add static compare method (#1192)
Browse files Browse the repository at this point in the history
* Implement comparable, add static compare method
  • Loading branch information
Chase Coalwell authored Apr 19, 2022
1 parent ce3aa97 commit 1863d67
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ public interface FromSourceLocation {
default SourceLocation getSourceLocation() {
return SourceLocation.none();
}

/**
* Compares two FromSourceLocations.
*
* @param s1 the first FromSourceLocation to compare.
* @param s2 the second FromSourceLocation to compare.
* @return the value 0 if s1 == s2; a value less than 0 if s1 < s2; and a value greater than 0 if s1 > s2.
*/
static int compare(FromSourceLocation s1, FromSourceLocation s2) {
return s1.getSourceLocation().compareTo(s2.getSourceLocation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Represents the source location of a model component.
*/
public final class SourceLocation implements FromSourceLocation {
public final class SourceLocation implements FromSourceLocation, Comparable<SourceLocation> {

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

Expand Down Expand Up @@ -97,4 +97,18 @@ public int hashCode() {

return h;
}

@Override
public int compareTo(SourceLocation o) {
if (!this.getFilename().equals(o.getFilename())) {
return this.getFilename().compareTo(o.getFilename());
}

int lineComparison = Integer.compare(this.getLine(), o.getLine());
if (lineComparison != 0) {
return lineComparison;
}

return Integer.compare(this.getColumn(), o.getColumn());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.smithy.model;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.StringShape;

public class SourceLocationTest {
@Test
public void sortsSourceLocations() {
Model model = Model.builder()
.addShape(StringShape.builder()
.id("smithy.example#First")
.source(new SourceLocation("a.smithy", 1, 1))
.build())
.addShape(StringShape.builder()
.id("smithy.example#Second")
.source(new SourceLocation("a.smithy", 1, 2))
.build())
.addShape(StringShape.builder()
.id("smithy.example#Third")
.source(new SourceLocation("a.smithy", 2, 1))
.build())
.addShape(StringShape.builder()
.id("smithy.example#Fourth")
.source(new SourceLocation("b.smithy", 1, 1))
.build())
.build();

List<Shape> shapes = model.shapes()
.sorted(Comparator.comparing(Shape::getSourceLocation))
.collect(Collectors.toList());
assertEquals("First", shapes.get(0).getId().getName());
assertEquals("Second", shapes.get(1).getId().getName());
assertEquals("Third", shapes.get(2).getId().getName());
assertEquals("Fourth", shapes.get(3).getId().getName());
}
}

0 comments on commit 1863d67

Please sign in to comment.