1 package io.github.robertograham.rleparser.domain;
 2 
 3 import java.util.Objects;
 4 import java.util.Set;
 5 import java.util.function.Function;
 6 import java.util.stream.Collectors;
 7 
 8 public class LiveCells {
 9 
10     private final Set<Coordinate> coordinates;
11 
12     public LiveCells(Set<Coordinate> coordinates) {
13         this.coordinates = coordinates;
14     }
15 
16     public Set<Coordinate> getCoordinates() {
17         return coordinates;
18     }
19 
20     public LiveCells filteredByX(int x) {
21         return new LiveCells(getCoordinatesWithPropertyEqualToValue(x, Coordinate::getX));
22     }
23 
24     public LiveCells filteredByY(int y) {
25         return new LiveCells(getCoordinatesWithPropertyEqualToValue(y, Coordinate::getY));
26     }
27 
28     @Override
29     public String toString() {
30         return "LiveCells{" +
31                 "coordinates=" + coordinates +
32                 '}';
33     }
34 
35     @Override
36     public boolean equals(Object object) {
37         if (this == object)
38             return true;
39 
40         if (!(object instanceof LiveCells))
41             return false;
42 
43         LiveCells liveCells = (LiveCells) object;
44 
45         return Objects.equals(coordinates, liveCells.coordinates);
46     }
47 
48     @Override
49     public int hashCode() {
50         return Objects.hash(coordinates);
51     }
52 
53     private Set<Coordinate> getCoordinatesWithPropertyEqualToValue(int value, Function<Coordinate, Integer> coordinatePropertyAccessor) {
54         return coordinates.stream()
55                 .filter(coordinate -> value == coordinatePropertyAccessor.apply(coordinate))
56                 .collect(Collectors.toSet());
57     }
58 }