Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement comparable, add static compare method #1192

Merged
merged 2 commits into from
Apr 19, 2022
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 @@ -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());
}
}