Skip to content

Commit ea762e3

Browse files
committed
Add Data Paint Components
1 parent d8f165c commit ea762e3

5 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package paintcomponents;
2+
3+
import java.util.Queue;
4+
import java.util.concurrent.LinkedBlockingQueue;
5+
6+
/**
7+
* This point consumes data and tries to pass the data along a connecting line
8+
* segment
9+
*
10+
* @author chenzb
11+
*
12+
*/
13+
public class DataFromPoint<T> extends SimplePoint {
14+
15+
16+
DataLineSegment<T> lineSegment;
17+
Queue<T> datas;
18+
19+
/**
20+
* @return the lineSegment
21+
*/
22+
public DataLineSegment<T> getLineSegment() {
23+
return lineSegment;
24+
}
25+
26+
/**
27+
* @param lineSegment
28+
* the lineSegment to set
29+
*/
30+
public void setLineSegment(DataLineSegment<T> lineSegment) {
31+
this.lineSegment = lineSegment;
32+
}
33+
34+
public DataFromPoint(int x, int y) {
35+
super(x, y);
36+
datas = new LinkedBlockingQueue<>();
37+
}
38+
39+
public void offer(T data){
40+
this.datas.offer(data);
41+
}
42+
43+
/**
44+
* Fetches the data, users should not try to call this method, except from DataToPoint class
45+
* returns null if the underlying queue is empty.
46+
* @param data
47+
*/
48+
protected T poll(){
49+
return this.datas.poll();
50+
}
51+
52+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package paintcomponents;
2+
3+
public class DataLineSegment<T> extends LineSegment {
4+
5+
public DataLineSegment(DataFromPoint<T> fromPoint, DataToPoint<T> toPoint) {
6+
super(fromPoint, toPoint);
7+
8+
}
9+
10+
@SuppressWarnings("unchecked")
11+
@Override
12+
public DataFromPoint<T> getFromPoint() {
13+
return (DataFromPoint<T>) super.getFromPoint();
14+
}
15+
16+
17+
@SuppressWarnings("unchecked")
18+
@Override
19+
public DataToPoint<T> getToPoint() {
20+
return (DataToPoint<T>) super.getToPoint();
21+
}
22+
23+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package paintcomponents;
2+
3+
import java.util.NoSuchElementException;
4+
5+
/**
6+
* Data to point is the point where data is flowing to.
7+
* You can "get" data from this point
8+
* @author chenzb
9+
*
10+
*/
11+
public class DataToPoint<T> extends SimplePoint {
12+
13+
14+
DataLineSegment<T> lineSegment;
15+
16+
/**
17+
* @return the lineSegment
18+
*/
19+
public DataLineSegment<T> getLineSegment() {
20+
return lineSegment;
21+
}
22+
23+
/**
24+
* @param lineSegment the lineSegment to set
25+
*/
26+
public void setLineSegment(DataLineSegment<T> lineSegment) {
27+
this.lineSegment = lineSegment;
28+
}
29+
30+
public DataToPoint(int x, int y) {
31+
super(x, y);
32+
}
33+
34+
35+
/**
36+
* Try to fetch a data from a connecting point
37+
*
38+
* @throws NoConnectingLineSegmentException when this point is not connected to a valid line segment
39+
* @throws NoSuchElementException when the connecting in point does not have a data to offer
40+
*/
41+
public T fetchData() throws NoConnectingLineSegmentException, NoSuchElementException{
42+
if(this.lineSegment == null) throw new NoConnectingLineSegmentException();
43+
44+
T returnVal = lineSegment.getFromPoint().poll();
45+
if(returnVal == null) throw new NoSuchElementException();
46+
return returnVal;
47+
48+
}
49+
50+
51+
}

src/paintcomponents/LineSegment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public LineSegment(SimplePoint fromPoint, SimplePoint toPoint,
6161
this.strokeWidth = strokeWidth;
6262
}
6363

64+
/**
65+
* Constructs a line segment with default width and default height.
66+
* @param fromPoint
67+
* @param toPoint
68+
*/
6469
public LineSegment(SimplePoint fromPoint, SimplePoint toPoint) {
6570
this(fromPoint, toPoint,
6671
Defaults.sharedDefaults().defaultColorForLineSegment(),
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package paintcomponents;
2+
3+
public class NoConnectingLineSegmentException extends Exception {
4+
5+
}

0 commit comments

Comments
 (0)