-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit improves the usability of Selectors by adding new methods to the Selector interface and by deprecating its runner functionality. The new methods include methods to stream matching shapes, stream matches, and consume matches. Streaming matches and shapes requires a minimal amount of buffering, and they provide the ability to more easily perform checks like determine if any shapes in a model match a selector. If all matching shapes are necessary, calling select is preferred. If consuming matches can be done in a way that is push-based (e.g., writing to CLI output), then using consumeMatches is preferred to matches. The previous runner() method of Selector is now deprecated and just delegates calls back to the Selector. runner() was initially intended to allow for the customization of the environment in which a selector is executed, but this design turned out to be unnecessary, and as such, the need to create a runner and use a kind of builder interface to use Selectors made them awkward to work with for no benefit. A final change is made to implement an IdentitySelector to optimize matching all shapes in a model.
- Loading branch information
Showing
10 changed files
with
358 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
smithy-model/src/main/java/software/amazon/smithy/model/selector/IdentitySelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2021 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.selector; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
|
||
/** | ||
* An optimized Selector implementation that uses the provided Model directly | ||
* rather than needing to send each shape through the Selector machinery. | ||
* | ||
* @see Selector#IDENTITY | ||
*/ | ||
final class IdentitySelector implements Selector { | ||
@Override | ||
public Set<Shape> select(Model model) { | ||
return model.toSet(); | ||
} | ||
|
||
@Override | ||
public Stream<Shape> shapes(Model model) { | ||
return model.shapes(); | ||
} | ||
|
||
@Override | ||
public Stream<ShapeMatch> matches(Model model) { | ||
return model.shapes().map(shape -> new ShapeMatch(shape, Collections.emptyMap())); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "*"; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other instanceof Selector && toString().equals(other.toString()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return toString().hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.