带颜色的形状接口:
public interface ColoredShape {
String getColor();
double getArea();
}
基础彩色形状类:
public abstract class BaseColoredShape implements ColoredShape {
protected final String color;
public BaseColoredShape(String color) {
this.color = color;
}
@Override
public String getColor() {
return this.color;
}
@Override
public String toString() {
return this.color;
}
}
矩形实现:
public class Rectangle extends BaseColoredShape {
private final double width;
private final double height;
public Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public String toString() {
return String.format("rectangle %s %s %s", this.width, this.height, this.color);
}
}
Cercle 实现:
public class Circle extends BaseColoredShape {
private final double radius;
public Circle(String colour, double radius) {
super(colour);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public String toString() {
return String.format("circle %s %s", this.radius, this.color);
}
}
输入数据解析器(txt文件)实现:
public class ShapeDataParser {
private final String filepath;
private final ColoredShapeFactory factory;
public ShapeDataParser(String filepath) {
this.filepath = filepath;
this.factory = new ColoredShapeFactory();
}
public Stream<ColoredShape> parse() throws IOException, URISyntaxException {
Path path = Paths.get(getClass().getClassLoader().getResource(filepath).toURI());
return Files.lines(path).map(this::stringToColoredShape);
}
private ColoredShape stringToColoredShape(String shapeStr) {
String[] shapeParts = shapeStr.split(" ");
String shapeType = shapeParts[0];
if("circle".equals(shapeType)) {
return factory.createCircle(shapeParts);
}
return factory.createRectangle(shapeParts);
}
}
形状工厂:
public class ColoredShapeFactory {
public ColoredShape createCircle(String[] shapeParts) {
//TODO implement validation of shapeParts array
double radius = Double.valueOf(shapeParts[1]);
String color = shapeParts[2];
return new Circle(color, radius);
}
public ColoredShape createRectangle(String[] shapeParts) {
//TODO implement validation of shapeParts array
double width = Double.valueOf(shapeParts[1]);
double height = Double.valueOf(shapeParts[2]);
String color = shapeParts[3];
return new Rectangle(color, width, height);
}
}
最后入口点类:
import java.io.IOException;
import java.net.URISyntaxException;
public class ShapeFilter {
public static void main(String[] args) throws IOException, URISyntaxException {
ShapeDataParser parser = new ShapeDataParser("shape_data.txt");
parser.parse()
.filter(ShapeFilter::isValid)
.forEach(System.out::println);
}
private static boolean isValid(ColoredShape shape) {
return shape.getArea() > 1000 && shape.getColor().equals("green");
}
}
输入数据shape_data.txt:
rectangle 68.01 77.63 orange
circle 88.06 green
circle 18.29 green
circle 71.71 red
rectangle 17.91 8.75 orange
circle 2.16 white
rectangle 83.12 98.71 green
rectangle 37.27 35.93 green
rectangle 45.13 74.55 green
circle 36.62 white
circle 72.59 yellow
输出:
circle 88.06 green
circle 18.29 green
rectangle 83.12 98.71 green
rectangle 37.27 35.93 green
rectangle 45.13 74.55 green