/
MainPool.java
201 lines (172 loc) · 6.24 KB
/
MainPool.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* MainPool.java
*
* Created on 17. March 2006, 20:42
* This stands under Public domain
*/
package de.genvlin.core.data;
/** This is the factory of pool's, vector's and xyvector's. Or you
* specific IDData if you want implement. Get the singleton instance
* via getDefault().
*
* @author Peter Karich
*/
public class MainPool extends Pool<IDData> {
/** This is the singleton instance of this class!*/
static private MainPool single;
/** to create new id's we need a counter:*/
protected int counter = 1;
protected MainPool() {
super(new IntID(0));
}
/** This method returns the singleton instance of this factory class.*/
public static synchronized MainPool getDefault() {
if(single == null) {
single = new MainPool();
}
return single;
}
public static synchronized void setDefault(MainPool mainPool) {
if(mainPool == null) {
throw new NullPointerException();
}
single = mainPool;
}
/**
* This method adds the specified pool.
* @return true if successfully imported
*/
public boolean importPool(MainPool pool) {
return false;
}
/**
* This method returns a clone of specified object.
* All Object.clone() implementions should call this factory method!
*/
public Object clone(Object o) {
throw new UnsupportedOperationException("Not yet implemented!");
}
/**
* This method creates a new IDData entry of this.
*
* Known entries could be HistogrammInterface, VectorPool, PoolInterface,
* XYPoolInterface, DoubleVectorInterface and VectorInterface.
*
* @throws UnsupportedOperationException if class unsupported!
*/
@SuppressWarnings("unchecked")
public <T extends VectorInterface> T createVector(Class<T> clazz) {
IDData o;
if(clazz.equals(HistogrammInterface.class)) {
o = new HistogrammVector(new IntID(counter));
} else if(clazz.equals(VectorInterface.class)) {
o = new DataVector(new IntID(counter), null);
} else if(clazz.equals(DoubleVectorInterface.class)) {
o = new DoubleVector(new IntID(counter), null, 50, 100);
} else {
throw new UnsupportedOperationException("Couldn't create the vector!");
}
return (T)addToPool(o);
}
/**
* This method creates a new Pool.
*
* Known elements of the pool can be XYVectorInterface, VectorInterface and
* XYDataInterface.
*
* @throws UnsupportedOperationException if class unsupported!
*/
@SuppressWarnings("unchecked")
public <T extends CollectionInterface> Pool<T> createPool(Class<T> elementsClass) {
Pool pool;
if(elementsClass.equals(XYVectorInterface.class)) {
pool = new XYPool(new IntID(counter));
} else if(elementsClass.equals(VectorInterface.class)) {
pool = new VectorPool(new IntID(counter));
} else if(elementsClass.equals(XYDataInterface.class)){
pool = new Pool<XYDataInterface>(new IntID(counter));
} else {
throw new UnsupportedOperationException("Couldn't create the pool!");
}
return (Pool<T>)addToPool(pool);
}
/**
* This method creates an implementation of the XYDataInterface for the
* specified XYVectorInterface object.
*/
public XYDataInterface createXYData(XYVectorInterface xyVector) {
XYData d = new XYData(xyVector, new IntID(counter));
addToPool(d);
return d;
}
/**
* This method creates a specific implementation of the XYDataInterface.
* Known types are: "decorator"
*/
public XYDataInterface createXYData(String type) {
XYDataInterface d;
if("decorator".equalsIgnoreCase(type)) {
d = new XYDecorator(new IntID(counter));
} else {
throw new UnsupportedOperationException("Couldn't find an implementation of specified type:" + type);
}
addToPool(d);
return d;
}
/**
* This method creates an implementation of the XYDataInterface for the
* specified vectors.
*/
public XYDataInterface createXYData(VectorInterface xVector, VectorInterface yVector) {
XYData d = new XYData(xVector, yVector, new IntID(counter));
addToPool(d);
return d;
}
/**
* This method creates an implementation of the XYDataInterface for the
* specified vectors.
*/
public <S extends VectorInterface, T extends VectorInterface>
XYDataInterface createXYData(Class<S> x, Class<T> y) {
XYVectorInterface xyV = createXYVector(x, y);
return createXYData(xyV);
}
/**
* This method will create a new <tt>XYVectorInterface</tt>.
* Known entries could be DoubleVectorInterface and VectorInterface.
* The specified classes should implement IDData.
*
* @see #create(VectorInterface, VectorInterface)
* @throws UnsupportedOperationException if class not supported!
*/
public <S extends VectorInterface, T extends VectorInterface>
XYVectorInterface createXYVector(Class<S> x, Class<T> y) {
return createXYVector(createVector(x), createVector(y));
}
/**
* This method will create a new <tt>XYVectorInterface</tt>.
* Known entries could be DoubleVectorInterface and VectorInterface.
* The specified classes should implement IDData.
*
* @see #create(Class,Class)
* @throws UnsupportedOperationException if class not supported!
*/
public XYVectorInterface createXYVector(VectorInterface x, VectorInterface y){
return addToPool(new XYVector(x, y, new IntID(counter)));
}
/**
* This method should be called from all subclasses to let MainPool
* fullfill his "factory"-contract.
*/
final protected <T extends IDData> T addToPool(T data){
counter++;
super.add(data);
return data;
}
/**
* Please: only use this method, if you know what you do!
*/
public void clear() {
super.clear();
}
}