001    /*
002     * Created on Jul 15, 2010
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 @2010-2011 the original author or authors.
014     */
015    package org.fest.assertions.core;
016    
017    import static org.fest.assertions.core.DescriptionValidations.checkIsNotNull;
018    
019    import org.fest.assertions.description.Description;
020    import org.fest.util.VisibleForTesting;
021    
022    /**
023     * A condition to be met by an object.
024     * @param <T> the type of object this condition accepts.
025     *
026     * @author Yvonne Wang
027     * @author Alex Ruiz
028     */
029    public abstract class Condition<T> implements Descriptable<Condition<T>> {
030    
031      @VisibleForTesting Description description;
032    
033      /**
034       * Creates a new </code>{@link Condition}</code>. The default description of this condition will the simple name of
035       * the condition's class.
036       */
037      public Condition() {
038        as(getClass().getSimpleName());
039      }
040    
041      /**
042       * Creates a new </code>{@link Condition}</code>.
043       * @param description the description of this condition.
044       * @throws NullPointerException if the given description is {@code null}.
045       */
046      public Condition(String description) {
047        as(description);
048      }
049    
050      /**
051       * Creates a new </code>{@link Condition}</code>.
052       * @param description the description of this condition.
053       * @throws NullPointerException if the given description is {@code null}.
054       */
055      public Condition(Description description) {
056        as(description);
057      }
058    
059      /** {@inheritDoc} */
060      public final Condition<T> describedAs(String newDescription) {
061        return as(newDescription);
062      }
063    
064      /** {@inheritDoc} */
065      public final Condition<T> as(String newDescription) {
066        description = checkIsNotNull(newDescription);
067        return this;
068      }
069    
070      /** {@inheritDoc} */
071      public final Condition<T> describedAs(Description newDescription) {
072        return as(newDescription);
073      }
074    
075      /** {@inheritDoc} */
076      public final Condition<T> as(Description newDescription) {
077        description = checkIsNotNull(newDescription);
078        return this;
079      }
080    
081      /**
082       * Returns the description of this condition.
083       * @return the description of this condition.
084       */
085      public final Description description() {
086        return description;
087      }
088    
089      /**
090       * Verifies that the given value satisfies this condition.
091       * @param value the value to verify.
092       * @return {@code true} if the given value satisfies this condition; {@code false} otherwise.
093       */
094      public abstract boolean matches(T value);
095    
096      @Override public String toString() {
097        return description.value();
098      }
099    }