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

Add abstraction over Symbols #202

Merged
merged 1 commit into from
Nov 11, 2019
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 @@ -16,6 +16,7 @@
package software.amazon.smithy.codegen.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import software.amazon.smithy.utils.ListUtils;
Expand Down Expand Up @@ -58,7 +59,8 @@
* }
* </pre>
*/
public final class Symbol extends TypedPropertiesBag implements ToSmithyBuilder<Symbol> {
public final class Symbol extends TypedPropertiesBag
implements SymbolContainer, SymbolDependencyContainer, ToSmithyBuilder<Symbol> {
private final String namespace;
private final String namespaceDelimiter;
private final String name;
Expand Down Expand Up @@ -187,20 +189,16 @@ public List<SymbolReference> getReferences() {
return references;
}

/**
* Gets the list of dependencies that this symbol introduces.
*
* <p>A dependency is a dependency on another package that the symbol
* requires. It is quite different from a reference since a reference
* only refers to a symbol; a reference provides no context as to whether
* or not a dependency is required or the dependency's coordinates.
*
* @return Returns the Symbol's dependencies.
*/
@Override
public List<SymbolDependency> getDependencies() {
return dependencies;
}

@Override
public List<Symbol> getSymbols() {
return Collections.singletonList(this);
}

@Override
public Builder toBuilder() {
Builder builder = new Builder();
Expand Down Expand Up @@ -363,14 +361,25 @@ public Builder dependencies(List<SymbolDependency> dependencies) {
return this;
}

/**
* Replaces the symbol dependencies of the symbol.
*
* @param container Dependencies to set.
* @return Returns the builder.
*/
public Builder dependencies(SymbolDependencyContainer container) {
this.dependencies.clear();
return addDependency(container);
}

/**
* Add a symbol dependency.
*
* @param dependency Symbol dependency to add.
* @return Returns the builder.
*/
public Builder addDependency(SymbolDependency dependency) {
dependencies.add(dependency);
public Builder addDependency(SymbolDependencyContainer dependency) {
dependencies.addAll(dependency.getDependencies());
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 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.codegen.core;

import java.util.List;

/**
* A holder for {@link Symbol} objects.
*/
public interface SymbolContainer {
/**
* Returns any {@link Symbol} objects contained in the object.
*
* @return Returns a collection of {@code Symbol}s.
*/
List<Symbol> getSymbols();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package software.amazon.smithy.codegen.core;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
Expand Down Expand Up @@ -66,7 +68,7 @@
* suggestion.
*/
public final class SymbolDependency extends TypedPropertiesBag
implements ToSmithyBuilder<SymbolDependency>, Comparable<SymbolDependency> {
implements SymbolDependencyContainer, ToSmithyBuilder<SymbolDependency>, Comparable<SymbolDependency> {

private final String dependencyType;
private final String packageName;
Expand Down Expand Up @@ -187,6 +189,11 @@ public String getVersion() {
return version;
}

@Override
public List<SymbolDependency> getDependencies() {
return Collections.singletonList(this);
}

@Override
public Builder toBuilder() {
return builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019 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.codegen.core;

import java.util.List;

/**
* A container for {@link SymbolDependency} objects.
*/
public interface SymbolDependencyContainer {
/**
* Gets the list of dependencies that this object introduces.
*
* <p>A dependency is a dependency on another package that a Symbol
* or type requires. It is quite different from a reference since a
* reference only refers to a symbol; a reference provides no context
* as to whether or not a dependency is required or the dependency's
* coordinates.
*
* @return Returns the dependencies.
*/
List<SymbolDependency> getDependencies();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand All @@ -42,7 +43,9 @@
* {@link ContextOption#USE} options, meaning that the reference is
* necessary both when defining and when using a symbol.
*/
public final class SymbolReference extends TypedPropertiesBag implements ToSmithyBuilder<SymbolReference> {
public final class SymbolReference
extends TypedPropertiesBag
implements SymbolContainer, SymbolDependencyContainer, ToSmithyBuilder<SymbolReference> {

/**
* Top-level interface for all {@code SymbolReference} options.
Expand Down Expand Up @@ -153,6 +156,16 @@ public boolean hasOption(Option option) {
return options.contains(option);
}

@Override
public List<Symbol> getSymbols() {
return Collections.singletonList(getSymbol());
}

@Override
public List<SymbolDependency> getDependencies() {
return symbol.getDependencies();
}

@Override
public Builder toBuilder() {
return new Builder()
Expand Down