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

org.jppf.node.policy.Equal Maven / Gradle / Ivy

There is a newer version: 6.3-alpha
Show newest version
/*
 * JPPF.
 * Copyright (C) 2005-2015 JPPF Team.
 * http://www.jppf.org
 *
 * 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 org.jppf.node.policy;

import org.jppf.utils.PropertiesCollection;

/**
 * An execution policy rule that encapsulates a test of type property_value == value.
 * The test applies to numeric, string and boolean values only.
 * @author Laurent Cohen
 */
public class Equal extends ExecutionPolicy
{
  /**
   * Explicit serialVersionUID.
   */
  private static final long serialVersionUID = 1L;
  /**
   * The name of the property to compare.
   */
  private String propertyName = null;
  /**
   * A numeric value to compare with.
   */
  private Number numberValue = null;
  /**
   * A string value to compare with.
   */
  private String stringValue = null;
  /**
   * An object value to compare with.
   */
  private Boolean booleanValue = null;
  /**
   * Determines if the comparison should ignore the string case.
   */
  private boolean ignoreCase = false;

  /**
   * Define an equality comparison between the numeric value of a property and another numeric value.
   * @param propertyName the name of the property to compare.
   * @param a the value to compare with.
   */
  public Equal(final String propertyName, final double a)
  {
    this.propertyName = propertyName;
    this.numberValue = a;
  }

  /**
   * Define an equality comparison between the string value of a property and another string value.
   * @param propertyName the name of the property to compare.
   * @param ignoreCase determines if the comparison should ignore the string case.
   * @param a the value to compare with.
   */
  public Equal(final String propertyName, final boolean ignoreCase, final String a)
  {
    this.propertyName = propertyName;
    this.stringValue = a;
    this.ignoreCase = ignoreCase;
  }

  /**
   * Define an equality comparison between the boolean value of a property and another boolean value.
   * @param propertyName the name of the property to compare.
   * @param a the value to compare with.
   */
  public Equal(final String propertyName, final boolean a)
  {
    this.propertyName = propertyName;
    this.booleanValue = a;
  }

  /**
   * Determines whether this policy accepts the specified node.
   * @param info system information for the node on which the tasks will run if accepted.
   * @return true if the node is accepted, false otherwise.
   */
  @Override
  public boolean accepts(final PropertiesCollection info)
  {
    try
    {
      String s = getProperty(info, propertyName);
      if (numberValue != null) return Double.valueOf(s).doubleValue() == numberValue.doubleValue();
      else if (stringValue != null)
      {
        return ignoreCase ? stringValue.equalsIgnoreCase(s) : stringValue.equals(s);
      }
      else if (booleanValue != null) return Boolean.valueOf(s).booleanValue() == booleanValue.booleanValue();
      else return s == null;
    }
    catch(Exception e)
    {
    }
    return false;
  }

  /**
   * Print this object to a string.
   * @return an XML string representation of this object
   */
  @Override
  public String toString()
  {
    if (computedToString == null)
    {
      synchronized(ExecutionPolicy.class)
      {
        StringBuilder sb = new StringBuilder();
        sb.append(indent()).append("\n");
        toStringIndent++;
        sb.append(indent()).append("").append(propertyName).append("\n");
        sb.append(indent()).append("");
        if (stringValue != null) sb.append(stringValue);
        else if (numberValue != null) sb.append(numberValue);
        else if (booleanValue != null) sb.append(booleanValue);
        sb.append("\n");
        toStringIndent--;
        sb.append(indent()).append("\n");
        computedToString = sb.toString();
      }
    }
    return computedToString;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy