Skip to content

Commit 0f4a1a1

Browse files
committed
Change to Data Provider Model
1 parent b114884 commit 0f4a1a1

File tree

6 files changed

+64
-18
lines changed

6 files changed

+64
-18
lines changed

src/paintcomponents/DataFromPoint.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
*
1212
*/
1313
public class DataFromPoint<T> extends SimplePoint {
14-
15-
16-
DataLineSegment<T> lineSegment;
17-
Queue<T> datas;
14+
15+
private DataLineSegment<T> lineSegment;
16+
private DataFromPointDataProvider<T> provider;
1817

1918
/**
2019
* @return the lineSegment
@@ -33,20 +32,32 @@ public void setLineSegment(DataLineSegment<T> lineSegment) {
3332

3433
public DataFromPoint(int x, int y) {
3534
super(x, y);
36-
datas = new LinkedBlockingQueue<>();
37-
}
38-
39-
public void offer(T data){
40-
this.datas.offer(data);
4135
}
42-
36+
4337
/**
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.
38+
* Fetches the data, users should not try to call this method, except from
39+
* DataToPoint class
40+
*
4641
* @param data
42+
* @throws DataFromPointNoDataProviderException if provider for this method is not set
43+
* @throws DataFromPointProviderCannotProvideDataException if the provider cannot provide such information
4744
*/
48-
protected T poll(){
49-
return this.datas.poll();
45+
protected T getData() throws DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException {
46+
if (this.provider == null){
47+
throw new DataFromPointNoDataProviderException();
48+
}
49+
if (!this.provider.canProvideInformationToDataFromPoint(this)){
50+
throw new DataFromPointProviderCannotProvideDataException();
51+
}
52+
return this.provider.provideInformationToDataFromPoint(this);
53+
}
54+
55+
public DataFromPointDataProvider<T> getProvider() {
56+
return provider;
57+
}
58+
59+
public void setProvider(DataFromPointDataProvider<T> provider) {
60+
this.provider = provider;
5061
}
5162

5263
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package paintcomponents;
2+
3+
4+
public interface DataFromPointDataProvider<T> {
5+
6+
public T provideInformationToDataFromPoint(DataFromPoint<T> dataFromPoint);
7+
public boolean canProvideInformationToDataFromPoint(DataFromPoint<T> dataFromPoint);
8+
9+
10+
11+
}
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 DataFromPointNoDataProviderException extends Exception {
4+
5+
}
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 DataFromPointProviderCannotProvideDataException extends Exception {
4+
5+
}

src/paintcomponents/DataInputTextfieldPaintComponent.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @author chenzb
1313
*
1414
*/
15-
public class DataInputTextfieldPaintComponent extends DataTextPaintComponent {
15+
public class DataInputTextfieldPaintComponent extends DataTextPaintComponent implements DataFromPointDataProvider<String> {
1616

1717

1818
private static final int HORIZONTAL_OFFSET = 10;
@@ -22,6 +22,7 @@ public DataInputTextfieldPaintComponent(String displayingText, int x,
2222
int y) {
2323
super(displayingText, x, y);
2424
this.fromPoint = new DataFromPoint<>(x, y);
25+
fromPoint.setProvider(this);
2526
}
2627

2728
@Override
@@ -60,8 +61,19 @@ public boolean contains(int x, int y) {
6061
}
6162

6263
public void inputData(String s) {
63-
fromPoint.offer(s);
6464
this.setDisplayingText(s);
6565
}
66+
67+
@Override
68+
public String provideInformationToDataFromPoint(
69+
DataFromPoint<String> dataFromPoint) {
70+
return displayingText;
71+
}
72+
73+
@Override
74+
public boolean canProvideInformationToDataFromPoint(
75+
DataFromPoint<String> dataFromPoint) {
76+
return displayingText == null;
77+
}
6678

6779
}

src/paintcomponents/DataToPoint.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ public DataToPoint(int x, int y) {
3737
*
3838
* @throws NoConnectingLineSegmentException when this point is not connected to a valid line segment
3939
* @throws NoSuchElementException when the connecting in point does not have a data to offer
40+
* @throws DataFromPointProviderCannotProvideDataException when the connecting dataInputPoint's data provider cannot provide a information
41+
* @throws DataFromPointNoDataProviderException when the connecting DataInputPoint does not have a data provider associated
4042
*/
41-
public T fetchData() throws NoConnectingLineSegmentException, NoSuchElementException{
43+
public T fetchData() throws NoConnectingLineSegmentException, NoSuchElementException, DataFromPointNoDataProviderException, DataFromPointProviderCannotProvideDataException{
4244
if(this.lineSegment == null) throw new NoConnectingLineSegmentException();
4345

44-
T returnVal = lineSegment.getFromPoint().poll();
46+
T returnVal = lineSegment.getFromPoint().getData();
4547
if(returnVal == null) throw new NoSuchElementException();
4648
return returnVal;
4749

0 commit comments

Comments
 (0)