-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #5 - Implemented Caching Adapter
- Loading branch information
Showing
6 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/design/patterns/adapter/caching/AdapterCachingDemo.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,48 @@ | ||
package design.patterns.adapter.caching; | ||
|
||
import design.patterns.adapter.caching.adapters.LineToPointAdapter; | ||
import design.patterns.adapter.caching.model.Line; | ||
import design.patterns.adapter.caching.model.Point; | ||
import design.patterns.adapter.caching.model.VectorObject; | ||
import design.patterns.adapter.caching.model.VectorRectangle; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
// used when the interface you have is not the interface you need | ||
|
||
// In this example, caching is used to prevent generating the same | ||
// lines multiple times with successive calls to the draw method. | ||
|
||
public class AdapterCachingDemo { | ||
|
||
public AdapterCachingDemo() { | ||
} | ||
|
||
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 point) { | ||
System.out.println("."); | ||
} | ||
|
||
private static void draw() { | ||
for(VectorObject object : vectorObjects) { | ||
for(Line line : object) { | ||
LineToPointAdapter adapter = new LineToPointAdapter(line); | ||
adapter.forEach(AdapterCachingDemo::drawPoint); | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
draw(); | ||
draw(); | ||
|
||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/design/patterns/adapter/caching/adapters/LineToPointAdapter.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,69 @@ | ||
package design.patterns.adapter.caching.adapters; | ||
|
||
import design.patterns.adapter.caching.model.Line; | ||
import design.patterns.adapter.caching.model.Point; | ||
|
||
import java.util.*; | ||
import java.util.function.Consumer; | ||
|
||
public class LineToPointAdapter implements Iterable<Point> { | ||
|
||
private static int count = 0; | ||
private static Map<Integer, List<Point>> cache = new HashMap<>(); | ||
private int hash; | ||
|
||
|
||
public LineToPointAdapter(Line line) { | ||
|
||
hash = line.hashCode(); | ||
|
||
if(cache.containsKey(hash)) { | ||
return; | ||
} | ||
|
||
int x1 = line.getStart().getX(); | ||
int y1 = line.getStart().getY(); | ||
int x2 = line.getEnd().getX(); | ||
int y2 = line.getEnd().getY(); | ||
|
||
System.out.println( | ||
String.format("%d: Generating points for line [%d,%d]-[%d,%d] (caching)", | ||
++count, x1, y1, x2, y2)); | ||
|
||
ArrayList<Point> points = new ArrayList<>(); | ||
|
||
int left = Math.min(x1, x2); | ||
int right = Math.max(x1, x2); | ||
int top = Math.min(y1, y2); | ||
int bottom = Math.max(y1, y2); | ||
int dx = right - left; | ||
int dy = y2 - y1; | ||
|
||
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); | ||
} | ||
|
||
@Override | ||
public void forEach(Consumer<? super Point> action) { | ||
cache.get(hash).forEach(action); | ||
} | ||
|
||
@Override | ||
public Spliterator<Point> spliterator() { | ||
return cache.get(hash).spliterator(); | ||
} | ||
|
||
@Override | ||
public Iterator<Point> iterator() { | ||
return cache.get(hash).iterator(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/design/patterns/adapter/caching/model/Line.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,51 @@ | ||
package design.patterns.adapter.caching.model; | ||
|
||
import java.util.Objects; | ||
|
||
public class Line { | ||
|
||
Point start, end; | ||
|
||
public Line(Point start, Point end) { | ||
this.start = start; | ||
this.end = end; | ||
} | ||
|
||
public Point getStart() { | ||
return start; | ||
} | ||
|
||
public void setStart(Point start) { | ||
this.start = start; | ||
} | ||
|
||
public Point getEnd() { | ||
return end; | ||
} | ||
|
||
public void setEnd(Point end) { | ||
this.end = end; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Line{" + | ||
"start=" + start + | ||
", 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; | ||
return Objects.equals(start, line.start) && | ||
Objects.equals(end, line.end); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(start, end); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/design/patterns/adapter/caching/model/Point.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,51 @@ | ||
package design.patterns.adapter.caching.model; | ||
|
||
import java.util.Objects; | ||
|
||
public class Point { | ||
|
||
private int x, y; | ||
|
||
public Point(int x, int y) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
public int getY() { | ||
return y; | ||
} | ||
|
||
public void setY(int y) { | ||
this.y = y; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Point{" + | ||
"x=" + x + | ||
", 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() { | ||
return Objects.hash(x, y); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/design/patterns/adapter/caching/model/VectorObject.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,6 @@ | ||
package design.patterns.adapter.caching.model; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class VectorObject extends ArrayList<Line> { | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/design/patterns/adapter/caching/model/VectorRectangle.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,12 @@ | ||
package design.patterns.adapter.caching.model; | ||
|
||
public 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))); | ||
} | ||
} |