-
Notifications
You must be signed in to change notification settings - Fork 1
/
Adapter.java
178 lines (150 loc) · 4.28 KB
/
Adapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Vector / Raster Demo. Example: we are given an API, given a set of objects
that are incompatible, and we have to provide for some sort of compatibility.
We use caching and noncaching approaches
*/
package structural.adapter;
import java.util.*;
import java.util.function.Consumer;
class Point
{
public int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return x == point.x && y == point.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
@Override
public String toString() {
return "Point{" +
"x=" + x +
", y=" + y +
'}';
}
}
class Line
{
public Point start, end;
public Line(Point start, Point end) {
this.start = start;
this.end = end;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Line line = (Line) o;
if (!start.equals(line.start)) return false;
return end.equals(line.end);
}
@Override
public int hashCode()
{
int result = start.hashCode();
result = 31 * result + end.hashCode();
return result;
}
}
class VectorObject extends ArrayList<Line>
{ }
class VectorRectangle extends VectorObject
{
public VectorRectangle(int x, int y, int width, int height) {
add(new Line(new Point(x,y), new Point(x+width, y) ));
add(new Line(new Point(x+width,y), new Point(x+width, y+height) ));
add(new Line(new Point(x,y), new Point(x, y+height) ));
add(new Line(new Point(x,y+height), new Point(x+width, y+height) ));
}
}
// Adapter here
class LineToPointAdapter implements Iterable<Point>
{
private static int count = 0;
private static Map<Integer, List<Point>> cache = new HashMap<>();
private int hash; // tells you which hash corresponds to a line
public LineToPointAdapter(Line line)
{
// Checking if line is in cache
hash = line.hashCode();
if (cache.get(hash) != null) return;
System.out.println(
String.format("%d: Generating points for line [%d,%d]-[%d,%d] (with caching)",
++count, line.start.x, line.start.y, line.end.x, line.end.y));
ArrayList<Point> points = new ArrayList<>();
int left = Math.min(line.start.x, line.end.x);
int right = Math.max(line.start.x, line.end.x);
int top = Math.min(line.start.y, line.end.y);
int bottom = Math.max(line.start.y, line.end.y);
int dx = right - left;
int dy = line.end.y - line.start.y;
if (dx == 0)
{
for (int y = top; y <= bottom; ++y)
{
points.add(new Point(left, y));
}
}
else if (dy == 0)
{
for (int x = left; x <= right; ++x)
{
points.add(new Point(x, top));
}
}
cache.put(hash, points);
}
// Caching implementation
@Override
public Iterator<Point> iterator() {
return cache.get(hash).iterator();
}
@Override
public void forEach(Consumer<? super Point> action) {
cache.get(hash).forEach(action);
}
@Override
public Spliterator<Point> spliterator() {
return cache.get(hash).spliterator();
}
}
class AdapterDemo
{
private final static List<VectorObject> vectorObjects
= new ArrayList<>(Arrays.asList(
new VectorRectangle(1,1,10,10),
new VectorRectangle(3,3,6,6)
));
public static void drawPoint (Point p)
{
System.out.println(".");
}
private static void draw()
{
for(VectorObject vo : vectorObjects)
{
for(Line line : vo)
{
final LineToPointAdapter adapter = new LineToPointAdapter(line);
adapter.forEach(AdapterDemo::drawPoint);
}
}
}
public static void main(String[] args)
{
draw();
draw();
}
}