Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -18,33 +18,41 @@ import com.iluwatar.specification.selector.MovementSelector;
|
||||
|
||||
/**
|
||||
*
|
||||
* The central idea of the Specification pattern is to separate the statement of how to match a candidate, from the
|
||||
* candidate object that it is matched against. As well as its usefulness in selection, it is also valuable for
|
||||
* validation and for building to order.
|
||||
* The central idea of the Specification pattern is to separate the statement of how to match a
|
||||
* candidate, from the candidate object that it is matched against. As well as its usefulness in
|
||||
* selection, it is also valuable for validation and for building to order.
|
||||
* <p>
|
||||
* In this example we have a pool of creatures with different properties. We then have defined separate selection
|
||||
* rules (Specifications) that we apply to the collection and as output receive only the creatures that match
|
||||
* the selection criteria.
|
||||
* In this example we have a pool of creatures with different properties. We then have defined
|
||||
* separate selection rules (Specifications) that we apply to the collection and as output receive
|
||||
* only the creatures that match the selection criteria.
|
||||
* <p>
|
||||
* http://martinfowler.com/apsupp/spec.pdf
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main( String[] args ) {
|
||||
// initialize creatures list
|
||||
List<Creature> creatures = Arrays.asList(new Goblin(), new Octopus(), new Dragon(), new Shark(), new Troll(), new KillerBee());
|
||||
// find all walking creatures
|
||||
System.out.println("Find all walking creatures");
|
||||
List<Creature> walkingCreatures = creatures.stream().filter(new MovementSelector(Movement.WALKING)).collect(Collectors.toList());
|
||||
walkingCreatures.stream().forEach(System.out::println);
|
||||
// find all dark creatures
|
||||
System.out.println("Find all dark creatures");
|
||||
List<Creature> darkCreatures = creatures.stream().filter(new ColorSelector(Color.DARK)).collect(Collectors.toList());
|
||||
darkCreatures.stream().forEach(System.out::println);
|
||||
// find all red and flying creatures
|
||||
System.out.println("Find all red and flying creatures");
|
||||
List<Creature> redAndFlyingCreatures = creatures.stream().filter(new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING))).collect(Collectors.toList());
|
||||
redAndFlyingCreatures.stream().forEach(System.out::println);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// initialize creatures list
|
||||
List<Creature> creatures =
|
||||
Arrays.asList(new Goblin(), new Octopus(), new Dragon(), new Shark(), new Troll(),
|
||||
new KillerBee());
|
||||
// find all walking creatures
|
||||
System.out.println("Find all walking creatures");
|
||||
List<Creature> walkingCreatures =
|
||||
creatures.stream().filter(new MovementSelector(Movement.WALKING))
|
||||
.collect(Collectors.toList());
|
||||
walkingCreatures.stream().forEach(System.out::println);
|
||||
// find all dark creatures
|
||||
System.out.println("Find all dark creatures");
|
||||
List<Creature> darkCreatures =
|
||||
creatures.stream().filter(new ColorSelector(Color.DARK)).collect(Collectors.toList());
|
||||
darkCreatures.stream().forEach(System.out::println);
|
||||
// find all red and flying creatures
|
||||
System.out.println("Find all red and flying creatures");
|
||||
List<Creature> redAndFlyingCreatures =
|
||||
creatures.stream()
|
||||
.filter(new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING)))
|
||||
.collect(Collectors.toList());
|
||||
redAndFlyingCreatures.stream().forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
@ -11,40 +11,40 @@ import com.iluwatar.specification.property.Size;
|
||||
*/
|
||||
public abstract class AbstractCreature implements Creature {
|
||||
|
||||
private String name;
|
||||
private Size size;
|
||||
private Movement movement;
|
||||
private Color color;
|
||||
private String name;
|
||||
private Size size;
|
||||
private Movement movement;
|
||||
private Color color;
|
||||
|
||||
public AbstractCreature(String name, Size size, Movement movement, Color color) {
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
this.movement = movement;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s [size=%s, movement=%s, color=%s]", name, size, movement, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Size getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement getMovement() {
|
||||
return movement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
public AbstractCreature(String name, Size size, Movement movement, Color color) {
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
this.movement = movement;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s [size=%s, movement=%s, color=%s]", name, size, movement, color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Size getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Movement getMovement() {
|
||||
return movement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
}
|
||||
|
@ -11,11 +11,11 @@ import com.iluwatar.specification.property.Size;
|
||||
*/
|
||||
public interface Creature {
|
||||
|
||||
String getName();
|
||||
|
||||
Size getSize();
|
||||
|
||||
Movement getMovement();
|
||||
|
||||
Color getColor();
|
||||
String getName();
|
||||
|
||||
Size getSize();
|
||||
|
||||
Movement getMovement();
|
||||
|
||||
Color getColor();
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import com.iluwatar.specification.property.Size;
|
||||
*/
|
||||
public class Dragon extends AbstractCreature {
|
||||
|
||||
public Dragon() {
|
||||
super("Dragon", Size.LARGE, Movement.FLYING, Color.RED);
|
||||
}
|
||||
public Dragon() {
|
||||
super("Dragon", Size.LARGE, Movement.FLYING, Color.RED);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import com.iluwatar.specification.property.Size;
|
||||
*/
|
||||
public class Goblin extends AbstractCreature {
|
||||
|
||||
public Goblin() {
|
||||
super("Goblin", Size.SMALL, Movement.WALKING, Color.GREEN);
|
||||
}
|
||||
public Goblin() {
|
||||
super("Goblin", Size.SMALL, Movement.WALKING, Color.GREEN);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import com.iluwatar.specification.property.Size;
|
||||
*/
|
||||
public class KillerBee extends AbstractCreature {
|
||||
|
||||
public KillerBee() {
|
||||
super("KillerBee", Size.SMALL, Movement.FLYING, Color.LIGHT);
|
||||
}
|
||||
public KillerBee() {
|
||||
super("KillerBee", Size.SMALL, Movement.FLYING, Color.LIGHT);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import com.iluwatar.specification.property.Size;
|
||||
*/
|
||||
public class Octopus extends AbstractCreature {
|
||||
|
||||
public Octopus() {
|
||||
super("Octopus", Size.NORMAL, Movement.SWIMMING, Color.DARK);
|
||||
}
|
||||
public Octopus() {
|
||||
super("Octopus", Size.NORMAL, Movement.SWIMMING, Color.DARK);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import com.iluwatar.specification.property.Size;
|
||||
*/
|
||||
public class Shark extends AbstractCreature {
|
||||
|
||||
public Shark() {
|
||||
super("Shark", Size.NORMAL, Movement.SWIMMING, Color.LIGHT);
|
||||
}
|
||||
public Shark() {
|
||||
super("Shark", Size.NORMAL, Movement.SWIMMING, Color.LIGHT);
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,8 @@ import com.iluwatar.specification.property.Size;
|
||||
*
|
||||
*/
|
||||
public class Troll extends AbstractCreature {
|
||||
|
||||
public Troll() {
|
||||
super("Troll", Size.LARGE, Movement.WALKING, Color.DARK);
|
||||
}
|
||||
|
||||
public Troll() {
|
||||
super("Troll", Size.LARGE, Movement.WALKING, Color.DARK);
|
||||
}
|
||||
}
|
||||
|
@ -7,16 +7,16 @@ package com.iluwatar.specification.property;
|
||||
*/
|
||||
public enum Color {
|
||||
|
||||
DARK("dark"), LIGHT("light"), GREEN("green"), RED("red");
|
||||
|
||||
private String title;
|
||||
DARK("dark"), LIGHT("light"), GREEN("green"), RED("red");
|
||||
|
||||
Color(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
private String title;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
Color(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -7,16 +7,16 @@ package com.iluwatar.specification.property;
|
||||
*/
|
||||
public enum Movement {
|
||||
|
||||
WALKING("walking"), SWIMMING("swimming"), FLYING("flying");
|
||||
|
||||
private String title;
|
||||
WALKING("walking"), SWIMMING("swimming"), FLYING("flying");
|
||||
|
||||
Movement(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
private String title;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
Movement(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -7,16 +7,16 @@ package com.iluwatar.specification.property;
|
||||
*/
|
||||
public enum Size {
|
||||
|
||||
SMALL("small"), NORMAL("normal"), LARGE("large");
|
||||
|
||||
private String title;
|
||||
SMALL("small"), NORMAL("normal"), LARGE("large");
|
||||
|
||||
Size(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
private String title;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
Size(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -12,14 +12,14 @@ import com.iluwatar.specification.property.Color;
|
||||
*/
|
||||
public class ColorSelector implements Predicate<Creature> {
|
||||
|
||||
private final Color c;
|
||||
private final Color c;
|
||||
|
||||
public ColorSelector(Color c) {
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Creature t) {
|
||||
return t.getColor().equals(c);
|
||||
}
|
||||
public ColorSelector(Color c) {
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Creature t) {
|
||||
return t.getColor().equals(c);
|
||||
}
|
||||
}
|
||||
|
@ -11,15 +11,15 @@ import com.iluwatar.specification.property.Movement;
|
||||
*
|
||||
*/
|
||||
public class MovementSelector implements Predicate<Creature> {
|
||||
|
||||
private final Movement m;
|
||||
|
||||
public MovementSelector(Movement m) {
|
||||
this.m = m;
|
||||
}
|
||||
private final Movement m;
|
||||
|
||||
@Override
|
||||
public boolean test(Creature t) {
|
||||
return t.getMovement().equals(m);
|
||||
}
|
||||
public MovementSelector(Movement m) {
|
||||
this.m = m;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Creature t) {
|
||||
return t.getMovement().equals(m);
|
||||
}
|
||||
}
|
||||
|
@ -12,14 +12,14 @@ import com.iluwatar.specification.property.Size;
|
||||
*/
|
||||
public class SizeSelector implements Predicate<Creature> {
|
||||
|
||||
private final Size s;
|
||||
private final Size s;
|
||||
|
||||
public SizeSelector(Size s) {
|
||||
this.s = s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Creature t) {
|
||||
return t.getSize().equals(s);
|
||||
}
|
||||
public SizeSelector(Size s) {
|
||||
this.s = s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Creature t) {
|
||||
return t.getSize().equals(s);
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.specification.app.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user