001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.util;
027:
028: import java.io.Serializable;
029:
030: /**
031: *
032: *
033: * @author $author$
034: * @version $Revision: 1.18 $
035: */
036: public abstract class State implements Serializable {
037: /** */
038: protected int state;
039:
040: /**
041: * Creates a new State object.
042: *
043: * @param initialState
044: */
045: public State(int initialState) {
046: this .state = initialState;
047: }
048:
049: /**
050: *
051: *
052: * @param state
053: *
054: * @return
055: */
056: public abstract boolean isValidState(int state);
057:
058: /**
059: *
060: *
061: * @param state
062: *
063: * @throws InvalidStateException
064: */
065: public synchronized void setValue(int state)
066: throws InvalidStateException {
067: if (!isValidState(state)) {
068: throw new InvalidStateException("The state is invalid");
069: }
070:
071: this .state = state;
072: notifyAll();
073: }
074:
075: /**
076: *
077: *
078: * @return
079: */
080: public synchronized int getValue() {
081: return state;
082: }
083:
084: /**
085: *
086: */
087: public synchronized void breakWaiting() {
088: notifyAll();
089: }
090:
091: /**
092: *
093: *
094: * @param state
095: *
096: * @return
097: *
098: * @throws InvalidStateException
099: * @throws InterruptedException
100: */
101: public synchronized boolean waitForState(int state)
102: throws InvalidStateException, InterruptedException {
103: return waitForState(state, 0);
104: }
105:
106: /**
107: *
108: *
109: * @param state
110: * @param timeout
111: *
112: * @return
113: *
114: * @throws InvalidStateException
115: * @throws InterruptedException
116: */
117: public synchronized boolean waitForState(int state, long timeout)
118: throws InvalidStateException, InterruptedException {
119: if (!isValidState(state)) {
120: throw new InvalidStateException("The state is invalid");
121: }
122:
123: if (timeout < 0) {
124: timeout = 0;
125: }
126:
127: while (this .state != state) {
128: // try {
129: wait(timeout);
130:
131: if (timeout != 0) {
132: break;
133: }
134:
135: // } catch (InterruptedException e) {
136: // break;
137: // }
138: }
139:
140: return this .state == state;
141: }
142:
143: /**
144: *
145: *
146: * @return
147: *
148: * @throws InterruptedException
149: */
150: public synchronized int waitForStateUpdate()
151: throws InterruptedException {
152: // try {
153: wait();
154:
155: // } catch (InterruptedException ie) {
156: // }
157: return state;
158: }
159:
160: /*public synchronized boolean waitForStateUpdate(long timeout) {
161: int oldstate = state;
162: if(timeout<0)
163: timeout=0;
164: try {
165: wait(timeout);
166: } catch(InterruptedException ie) {
167: }
168: return !(oldstate==state);
169: }*/
170: }
|