01: package com.coldcore.coloradoftp.connection.impl;
02:
03: import com.coldcore.coloradoftp.connection.DataPortListener;
04: import com.coldcore.coloradoftp.factory.ObjectFactory;
05: import com.coldcore.coloradoftp.factory.ObjectName;
06:
07: import java.util.HashSet;
08: import java.util.Iterator;
09:
10: /**
11: * Factory-like class to create a sequence of data port listeners.
12: * This class can be used as an imput parameter into data port listener set.
13: *
14: * Class contructor takes port numbers and creates a sequence of data port listeners with assigned
15: * port from port 1 to port 2 (inclusive). A very easy way rather than to create every data port
16: * listener individualy.
17: *
18: * This class relies on the ObjectFactory to get instances of DataPortListeners, but the ObjectFactory
19: * cannot be used in a constructor. This class overwrites important methods of the HashSet initializing
20: * itself when those will be called (e.g. by DataPortListenerSet).
21: */
22: public class DataPortListenerSequence extends HashSet<DataPortListener> {
23:
24: protected int port1;
25: protected int port2;
26: protected boolean initialized;
27:
28: public DataPortListenerSequence(int port1, int port2) {
29: this .port1 = port1;
30: this .port2 = port2;
31: }
32:
33: protected void initialize() {
34: if (initialized)
35: return;
36: initialized = true;
37: for (int port = port1; port <= port2; port++) {
38: DataPortListener listener = (DataPortListener) ObjectFactory
39: .getObject(ObjectName.DATA_PORT_LISTENER);
40: listener.setPort(port);
41: add(listener);
42: }
43: }
44:
45: public Iterator<DataPortListener> iterator() {
46: initialize();
47: return super .iterator();
48: }
49:
50: public int size() {
51: initialize();
52: return super .size();
53: }
54:
55: public boolean isEmpty() {
56: initialize();
57: return super.isEmpty();
58: }
59: }
|