Skip to content

Commit

Permalink
Allow dependency sorting and add helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Sep 19, 2019
1 parent 8d24733 commit fa7729f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,23 @@ public Builder addDependency(SymbolDependency dependency) {
* @return Returns the builder.
*/
public Builder addDependency(String packageName, String version) {
return addDependency(SymbolDependency.builder().packageName(packageName).version(version).build());
return addDependency(null, packageName, version);
}

/**
* Add a symbol dependency.
*
* @param dependencyType Type of dependency.
* @param packageName Name of the package to depend on.
* @param version Version to depend on.
* @return Returns the builder.
*/
public Builder addDependency(String dependencyType, String packageName, String version) {
return addDependency(SymbolDependency.builder()
.dependencyType(dependencyType)
.packageName(packageName)
.version(version)
.build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@
* of the suggested package, and {@code version} to the description of the
* suggestion.
*/
public final class SymbolDependency extends TypedPropertiesBag implements ToSmithyBuilder<SymbolDependency> {
public final class SymbolDependency extends TypedPropertiesBag
implements ToSmithyBuilder<SymbolDependency>, Comparable<SymbolDependency> {

private final String dependencyType;
private final String packageName;
private final String version;

private SymbolDependency(Builder builder) {
super(builder.properties);
this.dependencyType = SmithyBuilder.requiredState("dependencyType", builder.dependencyType);
this.dependencyType = builder.dependencyType == null ? "" : builder.dependencyType;
this.packageName = SmithyBuilder.requiredState("packageName", builder.packageName);
this.version = SmithyBuilder.requiredState("version", builder.version);
}
Expand Down Expand Up @@ -145,6 +146,27 @@ public int hashCode() {
return Objects.hash(dependencyType, packageName, version);
}

/**
* Dependencies can be sorted based on the natural sort order of
* the dependencyType, packageName, and finally the version.
*
* {@inheritDoc}
*/
@Override
public int compareTo(SymbolDependency other) {
int typeResult = dependencyType.compareTo(other.dependencyType);
if (typeResult != 0) {
return typeResult;
}

int packageResult = packageName.compareTo(other.packageName);
if (packageResult != 0) {
return packageResult;
}

return version.compareTo(other.version);
}

/**
* Builds a SymbolDependency.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package software.amazon.smithy.codegen.core;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;

public class SymbolDependencyTest {
Expand Down Expand Up @@ -40,4 +44,18 @@ public void hasProperties() {

assertThat(dependency.expectProperty("foo", String.class), equalTo("baz!"));
}

@Test
public void canBeSorted() {
SymbolDependency a = SymbolDependency.builder().dependencyType("a").packageName("a").version("1").build();
SymbolDependency a2 = SymbolDependency.builder().dependencyType("a").packageName("a2").version("1").build();
SymbolDependency a3 = SymbolDependency.builder().dependencyType("a2").packageName("a").version("1").build();
SymbolDependency b = SymbolDependency.builder().dependencyType("b").packageName("b").version("1").build();
SymbolDependency b2 = SymbolDependency.builder().dependencyType("b").packageName("b").version("2").build();
SymbolDependency c = SymbolDependency.builder().dependencyType("b").packageName("c").version("1").build();
List<SymbolDependency> dependencies = Arrays.asList(c, b2, b, a3, a2, a);
Collections.sort(dependencies);

assertThat(dependencies, contains(a, a2, a3, b, b2, c));
}
}

0 comments on commit fa7729f

Please sign in to comment.