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-2011 the original author or authors.
015 */
016 package org.fest.assertions;
017
018 import static org.fest.util.Strings.isEmpty;
019
020 /**
021 * Condition to be met by an <code>{@link Object}</code>.
022 * @param <T> the type of {@code Object} this condition accepts.
023 *
024 * @author Yvonne Wang
025 * @author Alex Ruiz
026 */
027 public abstract class Condition<T> {
028
029 private String description;
030
031 /**
032 * Creates a new <code>{@link Condition}</code>.
033 */
034 public Condition() {}
035
036 /**
037 * Creates a new <code>{@link Condition}</code>.
038 * @param description the description of this condition.
039 */
040 public Condition(String description) {
041 as(description);
042 }
043
044 /**
045 * Sets the description of this condition.
046 * @param newDescription the description to set.
047 * @return this condition.
048 */
049 public final Condition<T> as(String newDescription) {
050 description = newDescription;
051 return this;
052 }
053
054 final String addDescriptionTo(String s) {
055 String descriptionToAdd = description();
056 if (isEmpty(descriptionToAdd)) descriptionToAdd = getClass().getSimpleName();
057 return String.format("%s:<%s>", s, descriptionToAdd);
058 }
059
060 /**
061 * Returns the description of this condition, if any.
062 * @return the description of this condition.
063 */
064 public final String description() { return description; }
065
066 /**
067 * Verifies that the given value satisfies this condition.
068 * @param value the value to verify.
069 * @return {@code true} if the given value satisfies this condition, {@code false} otherwise.
070 */
071 public abstract boolean matches(T value);
072 }