001 /*
002 * Created on Apr 14, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2007-2010 the original author or authors.
014 */
015 package org.fest.swing.timing;
016
017 import static org.fest.util.Strings.concat;
018
019 import org.fest.assertions.BasicDescription;
020 import org.fest.assertions.Description;
021
022 /**
023 * Understands a condition to verify, usually used in the method <code>{@link Pause#pause(Condition)}</code>.
024 *
025 * @author Yvonne Wang
026 * @author Alex Ruiz
027 */
028 public abstract class Condition {
029
030 protected static final String EMPTY_TEXT = "";
031
032 private final Description description;
033
034 /**
035 * Creates a new <code>{@link Condition}</code>.
036 * @param description describes this condition.
037 */
038 public Condition(String description) {
039 this(new BasicDescription(description));
040 }
041
042 /**
043 * Creates a new </code>{@link Condition}</code>.
044 * @param description describes this condition.
045 */
046 public Condition(Description description) {
047 this.description = description;
048 }
049
050 /**
051 * Checks if the condition has been satisfied.
052 * @return <code>true</code> if the condition has been satisfied, otherwise <code>false</code>.
053 */
054 public abstract boolean test();
055
056 /**
057 * Returns the <code>String</code> representation of this condition, which is its description.
058 * @return the description of this condition.
059 */
060 @Override public final String toString() {
061 String descriptionText = description != null ? description.value() : defaultDescription();
062 String addendum = descriptionAddendum();
063 return concat(descriptionText, addendum != null ? addendum : EMPTY_TEXT);
064 }
065
066 private String defaultDescription() {
067 return concat("condition of type [", getClass().getName(), "]");
068 }
069
070 /**
071 * Returns any text to be added to this condition's description. The default value is an empty <code>String</code>.
072 * @return by default, an empty <code>String</code>.
073 */
074 protected String descriptionAddendum() {
075 return EMPTY_TEXT;
076 }
077
078 /**
079 * Notification that this condition has been evaluated. This method is invoked by
080 * <code>{@link Pause#pause(Condition)}</code> (and all overloaded methods) when this condition is evaluated (either
081 * it was satisfied or it timed-out.) This is a good place to do any necessary resource cleanup.
082 */
083 protected void done() {}
084 }