001 /*
002 * Created on Sep 17, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with 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
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2007-2009 the original author or authors.
015 */
016 package org.fest.assertions;
017
018 import static org.fest.util.Strings.concat;
019 import static org.fest.util.Strings.isEmpty;
020
021 /**
022 * Understands a condition to be met by a given <code>{@link Object}</code>.
023 * @param <T> the type of <code>Object</code> this condition accepts.
024 *
025 * @author Yvonne Wang
026 * @author Alex Ruiz
027 */
028 public abstract class Condition<T> {
029
030 private String description;
031
032 /**
033 * Creates a new <code>{@link Condition}</code>.
034 */
035 public Condition() {}
036
037 /**
038 * Creates a new <code>{@link Condition}</code>.
039 * @param description the description of this condition.
040 */
041 public Condition(String description) {
042 as(description);
043 }
044
045 /**
046 * Sets the description of this condition.
047 * @param newDescription the description to set.
048 * @return this condition.
049 */
050 public final Condition<T> as(String newDescription) {
051 description = newDescription;
052 return this;
053 }
054
055 final String addDescriptionTo(String s) {
056 String descriptionToAdd = description();
057 if (isEmpty(descriptionToAdd)) descriptionToAdd = getClass().getSimpleName();
058 return concat(s, ":<", descriptionToAdd, ">");
059 }
060
061 /**
062 * Returns the description of this condition, if any.
063 * @return the description of this condition.
064 */
065 public final String description() { return description; }
066
067 /**
068 * Verifies that the given value satisfies this condition.
069 * @param value the value to verify.
070 * @return <code>true</code> if the given value satisfies this condition, <code>false</code> otherwise.
071 */
072 public abstract boolean matches(T value);
073 }