View Javadoc
1   /*
2    * $Header: /cvsroot/sinon/sinon/src/java/eteg/sinon/executor/PageState.java,v 1.2 2005/06/21 14:25:08 thiagohp Exp $
3    * $Revision: 1.2 $
4    * $Date: 2005/06/21 14:25:08 $
5    * $Author: thiagohp $
6    *
7    * =============================================================================
8    *
9    * Copyright 2004-2005 Eteg Internet Ltda. (http://www.eteg.com.br)
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License");
12   * you may not use this file except in compliance with the License.
13   * You may obtain a copy of the License at
14   *
15   *     http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   */
23  package eteg.sinon.executor;
24  
25  /***
26   * Class that represents the state of processing of a page.
27   *
28   * @author <a href="mailto:thiagohp at users.sourceforge.net">Thiago H. de Paula Figueiredo</a>
29   * @author Last modified by $Author: thiagohp $
30   * @version $Revision: 1.2 $
31   * @since 1.0
32   */
33  public class PageState {
34  
35      /***
36       * Constant that defines the state of a page before its processing
37       * begins.
38       */
39      final private static int BEFORE_DOWNLOAD_ID = 0;
40  
41      /***
42       * Constant that defines the state of a page before its download.
43       * begins.
44       */
45      final private static int BEFORE_PROCESSING_ID = 1;
46  
47      /***
48       * Constant that defines the state of a page before after download and
49       * processing.
50       */
51      final private static int AFTER_PROCESSING_ID = 2;
52  
53      /***
54       * Constant that defines the state of a page before after all its actions
55       * were executed.
56       */
57      final private static int FINISHED_ID = 3;
58  
59      /***
60       * Constant that defines the state of a page before its processing
61       * begins.
62       */
63      final public static PageState BEFORE_DOWNLOAD;
64  
65      /***
66       * Constant that defines the state of a page before its download.
67       * begins.
68       */
69      final public static PageState BEFORE_PROCESSING;
70  
71      /***
72       * Constant that defines the state of a page before after download and
73       * processing.
74       */
75      final public static PageState AFTER_PROCESSING;
76  
77      /***
78       * Constant that defines the state of a page before after all its actions
79       * were executed.
80       */
81      final public static PageState FINISHED;
82  
83      /***
84       * Page state identifier.
85       */
86      private int stateId;
87  
88      static {
89  
90          BEFORE_DOWNLOAD = new PageState(BEFORE_DOWNLOAD_ID);
91          BEFORE_PROCESSING = new PageState(BEFORE_PROCESSING_ID);
92          AFTER_PROCESSING = new PageState(AFTER_PROCESSING_ID);
93          FINISHED = new PageState(FINISHED_ID);
94  
95      }
96  
97      /***
98       * Single constructor of this class. The valid values for
99       * <code>stateId</code> are
100      * {@link #BEFORE_DOWNLOAD_ID}, {@link #BEFORE_PROCESSING_ID},
101      * {@link #AFTER_PROCESSING_ID}, and {@link #FINISHED_ID}.
102      * @param stateId an <code>int</code>.
103      * @throws java.lang.IllegalArgumentException if <code>stateId</code> is
104      * not a valid value.
105      */
106     private PageState(int stateId) {
107 
108         if (stateId < BEFORE_DOWNLOAD_ID || stateId > FINISHED_ID) {
109 
110             throw new IllegalArgumentException(
111                     "Parameter stateId does not have a valid value : " +
112                     stateId);
113 
114         }
115 
116         this.stateId = stateId;
117 
118     }
119 
120     /***
121      * Returns <code>true</code> if this state is
122      * {@link #BEFORE_DOWNLOAD}.
123      *
124      * @return <code>true</code> if this state is
125      * {@link #BEFORE_DOWNLOAD}, <code>false</code> otherwise.
126      */
127     public boolean isBeforeDownload() {
128         return (stateId == BEFORE_DOWNLOAD_ID);
129     }
130 
131     /***
132      * Returns <code>true</code> if this state is
133      * {@link #BEFORE_PROCESSING}.
134      *
135      * @return <code>true</code> if this state is
136      * {@link #BEFORE_PROCESSING}, <code>false</code> otherwise.
137      */
138     public boolean isBeforeProcessing() {
139         return (stateId == BEFORE_PROCESSING_ID);
140     }
141 
142     /***
143      * Returns <code>true</code> if this state is
144      * {@link #AFTER_PROCESSING}.
145      *
146      * @return <code>true</code> if this state is
147      * {@link #AFTER_PROCESSING}, <code>false</code> otherwise.
148      */
149     public boolean isAfterProcessing() {
150         return (stateId == AFTER_PROCESSING_ID);
151     }
152 
153     /***
154      * Returns <code>true</code> if this state is
155      * {@link #FINISHED}.
156      *
157      * @return <code>true</code> if this state is
158      * {@link #BEFORE_DOWNLOAD}, <code>false</code> otherwise.
159      */
160     public boolean isFinished() {
161         return (stateId == FINISHED_ID);
162     }
163 
164     /***
165      * Returns a <code>String</code> containing the name of this state.
166      *
167      * @return a <code>String</code>.
168      */
169     public String getStateName() {
170 
171         if (isBeforeDownload()) {
172             return "before download";
173         }
174         else if (isBeforeProcessing()) {
175             return "before processing";
176         }
177         else if (isAfterProcessing()) {
178             return "after processing";
179         }
180         else if (isFinished()) {
181             return "finished";
182         }
183         else {
184             return "invalid page state";
185         }
186 
187     }
188 
189     public boolean equals(final Object other) {
190 
191         if (other instanceof PageState == false) {
192             return false;
193         }
194 
195         PageState castOther = (PageState) other;
196         return this.stateId == castOther.stateId;
197 
198     }
199 
200 }