public class Door
1: // Door.java
2: // Sends DoorEvents to DoorListeners when opened or closed
3: package com.deitel.jhtp5.elevator.model;
4:
5: // Java core packages
6: import java.util.*;
7:
8: // Deitel packages
9: import com.deitel.jhtp5.elevator.event.*;
10:
11: public class Door {
12:
13: // represent whether Door is open or closed
14: private boolean open = false;
15:
16: // time before Door closes automatically
17: public static final int AUTOMATIC_CLOSE_DELAY = 3000;
18:
19: // Set of DoorListeners
20: private Set doorListeners;
21:
22: // location where Door opened or closed
23: private Location doorLocation;
24:
25: // Door constructor instantiates Set for DoorListeners
26: public Door()
27: {
28: doorListeners = new HashSet( 1 );
29: }
30:
31: // add Door listener
32: public void addDoorListener( DoorListener listener )
33: {
34: // prevent other objects from modifying doorListeners
35: synchronized( doorListeners )
36: {
37: doorListeners.add( listener );
38: }
39: }
40:
41: // remove Door listener
42: public void removeDoorListener( DoorListener listener )
43: {
44: // prevent other objects from modifying doorListeners
45: synchronized( doorListeners )
46: {
47: doorListeners.remove( listener );
48: }
49: }
50:
51: // open Door and send all listeners DoorEvent objects
52: public synchronized void openDoor( Location location )
53: {
54: if ( !open ) {
55:
56: open = true;
57:
58: // obtain iterator from Set
59: Iterator iterator;
60: synchronized( doorListeners )
61: {
62: iterator = new HashSet( doorListeners ).iterator();
63: }
64:
65: // get next DoorListener
66: while ( iterator.hasNext() ) {
67: DoorListener doorListener =
68: ( DoorListener ) iterator.next();
69:
70: // send doorOpened event to this DoorListener
71: doorListener.doorOpened(
72: new DoorEvent( this, location ) );
73: }
74:
75: doorLocation = location;
76:
77: // declare Thread that ensures automatic Door closing
78: Thread closeThread = new Thread(
79: new Runnable() {
80:
81: public void run()
82: {
83: // close Door if open for more than 3 seconds
84: try {
85: Thread.sleep( AUTOMATIC_CLOSE_DELAY );
86: closeDoor( doorLocation );
87: }
88:
89: // handle exception if interrupted
90: catch ( InterruptedException exception ) {
91: exception.printStackTrace();
92: }
93: }
94: } // end anonymous inner class
95: );
96:
97: closeThread.start();
98: }
99:
100: // notify all waiting threads that the door has opened
101: notifyAll();
102:
103: } // end method openDoor
104:
105: // close Door and send all listeners DoorEvent objects
106: public synchronized void closeDoor( Location location )
107: {
108: if ( open ) {
109:
110: open = false;
111:
112: // obtain iterator from Set
113: Iterator iterator;
114: synchronized( doorListeners )
115: {
116: iterator = new HashSet( doorListeners ).iterator();
117: }
118:
119: // get next DoorListener
120: while ( iterator.hasNext() ) {
121: DoorListener doorListener =
122: ( DoorListener ) iterator.next();
123:
124: // send doorClosed event to this DoorListener
125: doorListener.doorClosed(
126: new DoorEvent( this, location ) );
127: }
128: }
129:
130: } // end method closeDoor
131:
132: // return whether Door is open or closed
133: public synchronized boolean isDoorOpen()
134: {
135: return open;
136: }
137: }
138:
139:
140: /**************************************************************************
141: * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and *
142: * Prentice Hall. All Rights Reserved. *
143: * *
144: * DISCLAIMER: The authors and publisher of this book have used their *
145: * best efforts in preparing the book. These efforts include the *
146: * development, research, and testing of the theories and programs *
147: * to determine their effectiveness. The authors and publisher make *
148: * no warranty of any kind, expressed or implied, with regard to these *
149: * programs or to the documentation contained in these books. The authors *
150: * and publisher shall not be liable in any event for incidental or *
151: * consequential damages in connection with, or arising out of, the *
152: * furnishing, performance, or use of these programs. *
153: *************************************************************************/