001/*
002 * JPPF.
003 * Copyright (C) 2005-2015 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.node.policy;
020
021import org.jppf.utils.PropertiesCollection;
022
023/**
024 * An execution policy rule that encapsulates a test of type <i>property_value == value</i>.
025 * The test applies to numeric, string and boolean values only.
026 * @author Laurent Cohen
027 */
028public class Equal extends ExecutionPolicy
029{
030  /**
031   * Explicit serialVersionUID.
032   */
033  private static final long serialVersionUID = 1L;
034  /**
035   * The name of the property to compare.
036   */
037  private String propertyName = null;
038  /**
039   * A numeric value to compare with.
040   */
041  private Number numberValue = null;
042  /**
043   * A string value to compare with.
044   */
045  private String stringValue = null;
046  /**
047   * An object value to compare with.
048   */
049  private Boolean booleanValue = null;
050  /**
051   * Determines if the comparison should ignore the string case.
052   */
053  private boolean ignoreCase = false;
054
055  /**
056   * Define an equality comparison between the numeric value of a property and another numeric value.
057   * @param propertyName the name of the property to compare.
058   * @param a the value to compare with.
059   */
060  public Equal(final String propertyName, final double a)
061  {
062    this.propertyName = propertyName;
063    this.numberValue = a;
064  }
065
066  /**
067   * Define an equality comparison between the string value of a property and another string value.
068   * @param propertyName the name of the property to compare.
069   * @param ignoreCase determines if the comparison should ignore the string case.
070   * @param a the value to compare with.
071   */
072  public Equal(final String propertyName, final boolean ignoreCase, final String a)
073  {
074    this.propertyName = propertyName;
075    this.stringValue = a;
076    this.ignoreCase = ignoreCase;
077  }
078
079  /**
080   * Define an equality comparison between the boolean value of a property and another boolean value.
081   * @param propertyName the name of the property to compare.
082   * @param a the value to compare with.
083   */
084  public Equal(final String propertyName, final boolean a)
085  {
086    this.propertyName = propertyName;
087    this.booleanValue = a;
088  }
089
090  /**
091   * Determines whether this policy accepts the specified node.
092   * @param info system information for the node on which the tasks will run if accepted.
093   * @return true if the node is accepted, false otherwise.
094   */
095  @Override
096  public boolean accepts(final PropertiesCollection info)
097  {
098    try
099    {
100      String s = getProperty(info, propertyName);
101      if (numberValue != null) return Double.valueOf(s).doubleValue() == numberValue.doubleValue();
102      else if (stringValue != null)
103      {
104        return ignoreCase ? stringValue.equalsIgnoreCase(s) : stringValue.equals(s);
105      }
106      else if (booleanValue != null) return Boolean.valueOf(s).booleanValue() == booleanValue.booleanValue();
107      else return s == null;
108    }
109    catch(Exception e)
110    {
111    }
112    return false;
113  }
114
115  /**
116   * Print this object to a string.
117   * @return an XML string representation of this object
118   */
119  @Override
120  public String toString()
121  {
122    if (computedToString == null)
123    {
124      synchronized(ExecutionPolicy.class)
125      {
126        StringBuilder sb = new StringBuilder();
127        sb.append(indent()).append("<Equal valueType=\"");
128        if (stringValue != null) sb.append("string");
129        else if (numberValue != null) sb.append("numeric");
130        else if (booleanValue != null) sb.append("boolean");
131        sb.append("\" ignoreCase=\"").append(ignoreCase).append("\">\n");
132        toStringIndent++;
133        sb.append(indent()).append("<Property>").append(propertyName).append("</Property>\n");
134        sb.append(indent()).append("<Value>");
135        if (stringValue != null) sb.append(stringValue);
136        else if (numberValue != null) sb.append(numberValue);
137        else if (booleanValue != null) sb.append(booleanValue);
138        sb.append("</Value>\n");
139        toStringIndent--;
140        sb.append(indent()).append("</Equal>\n");
141        computedToString = sb.toString();
142      }
143    }
144    return computedToString;
145  }
146}