All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.projectmonkey.Conditions Maven / Gradle / Ivy

/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.projectmonkey;

import java.io.Serializable;

import net.projectmonkey.internal.util.Assert;
import net.projectmonkey.spi.MappingContext;


/**
 * {@link Condition} utilities and implementations. This class can be extended by a PropertyMap to
 * provide convenient access to methods.
 * 
 * @author Jonathan Halterman
 */
public class Conditions {
  private static final Condition IS_NULL = new AbstractCondition() {
    @SuppressWarnings("unused")
    private static final long serialVersionUID = 0;

    public boolean applies(MappingContext context) {
      return context.getSource() == null;
    }

    @Override
    public String toString() {
      return "isNull()";
    }
  };

  private static final Condition IS_NOT_NULL = new AbstractCondition() {
    @SuppressWarnings("unused")
    private static final long serialVersionUID = 0;

    public boolean applies(MappingContext context) {
      return context.getSource() != null;
    }

    @Override
    public String toString() {
      return "isNotNull()";
    }
  };

  private static class AndCondition extends AbstractCondition implements Serializable {
    private static final long serialVersionUID = 0;
    private final Condition a;
    private final Condition b;

    AndCondition(Condition a, Condition b) {
      this.a = a;
      this.b = b;
    }

    public boolean applies(MappingContext context) {
      return a.applies(context) && b.applies(context);
    }

    @Override
    public boolean equals(Object other) {
      return other instanceof AndCondition && ((AndCondition) other).a.equals(a)
          && ((AndCondition) other).b.equals(b);
    }

    @Override
    public int hashCode() {
      return 41 * (a.hashCode() ^ b.hashCode());
    }

    @Override
    public String toString() {
      return String.format("and(%s, %s)", a, b);
    }
  }

  private static class Not extends AbstractCondition implements Serializable {
    private static final long serialVersionUID = 0;
    private final Condition delegate;

    private Not(Condition delegate) {
      this.delegate = Assert.notNull(delegate, "delegate");
    }

    public boolean applies(MappingContext context) {
      return !delegate.applies(context);
    }

    @Override
    public boolean equals(Object other) {
      return other instanceof Not && ((Not) other).delegate.equals(delegate);
    }

    @Override
    public int hashCode() {
      return -delegate.hashCode();
    }

    @Override
    public String toString() {
      return "not(" + delegate + ")";
    }
  }

  private static class OrCondition extends AbstractCondition implements Serializable {
    private static final long serialVersionUID = 0;
    private final Condition a;
    private final Condition b;

    OrCondition(Condition a, Condition b) {
      this.a = a;
      this.b = b;
    }

    public boolean applies(MappingContext context) {
      return a.applies(context) || b.applies(context);
    }

    @Override
    public boolean equals(Object other) {
      return other instanceof OrCondition && ((OrCondition) other).a.equals(a)
          && ((OrCondition) other).b.equals(b);
    }

    @Override
    public int hashCode() {
      return 37 * (a.hashCode() ^ b.hashCode());
    }

    @Override
    public String toString() {
      return String.format("or(%s, %s)", a, b);
    }
  }

  /**
   * Returns a new condition that applies if {@code condition1} AND {@code condition2} apply.
   * 
   * @return new condition
   * @throws IllegalArgumentException if {@code condition1} or {@code condition2} is null
   */
  public static  Condition and(Condition condition1, Condition condition2) {
    Assert.notNull(condition1, "condition1");
    Assert.notNull(condition2, "condition2");
    return new AndCondition(condition1, condition2);
  }

  /**
   * Returns a condition that applies when the mapping source is not {@code null}.
   */

  public static Condition isNotNull() {
    return IS_NOT_NULL;
  }

  /**
   * Returns a condition that applies when the mapping source is {@code null}.
   */

  public static Condition isNull() {
    return IS_NULL;
  }

  /**
   * Returns a condition that does NOT apply when the given {@code condition} applies.
   * 
   * @throws IllegalArgumentException if {@code condition} is null
   */
  public static  Condition not(Condition condition) {
    Assert.notNull(condition, "condition");
    return new Not(condition);
  }

  /**
   * Returns a new condition that applies if {@code condition1} OR {@code condition2} apply.
   * 
   * @return new condition
   * @throws IllegalArgumentException if {@code condition1} or {@code condition2} is null
   */
  public static  Condition or(Condition condition1, Condition condition2) {
    Assert.notNull(condition1, "condition1");
    Assert.notNull(condition2, "condition2");
    return new OrCondition(condition1, condition2);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy