001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 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 &gt; value</i>.
025 * The test applies to numeric values only.
026 * @author Laurent Cohen
027 */
028public class MoreThan extends ExecutionPolicy {
029  /**
030   * Explicit serialVersionUID.
031   */
032  private static final long serialVersionUID = 1L;
033  /**
034   * The name of the property to compare.
035   */
036  private String propertyName = null;
037  /**
038   * A numeric value to compare with.
039   */
040  private Number numberValue = null;
041
042  /**
043   * Define a comparison between the numeric value of a property and another numeric value.
044   * @param propertyName the name of the property to compare.
045   * @param a the value to compare with.
046   */
047  public MoreThan(final String propertyName, final double a) {
048    this.propertyName = propertyName;
049    this.numberValue = a;
050  }
051
052  /**
053   * Determines whether this policy accepts the specified node.
054   * @param info system information for the node on which the tasks will run if accepted.
055   * @return true if the node is accepted, false otherwise.
056   */
057  @Override
058  public boolean accepts(final PropertiesCollection info) {
059    try {
060      String s = getProperty(info, propertyName);
061      if (numberValue != null) return Double.valueOf(s).doubleValue() > numberValue.doubleValue();
062    } catch (Exception e) {
063    }
064    return false;
065  }
066
067  /**
068   * Print this object to a string.
069   * @return an XML string representation of this object
070   */
071  @Override
072  public String toString() {
073    if (computedToString == null) {
074      synchronized (ExecutionPolicy.class) {
075        StringBuilder sb = new StringBuilder();
076        sb.append(indent()).append("<MoreThan>\n");
077        toStringIndent++;
078        sb.append(indent()).append("<Property>").append(propertyName).append("</Property>\n");
079        sb.append(indent()).append("<Value>").append(numberValue).append("</Value>\n");
080        toStringIndent--;
081        sb.append(indent()).append("</MoreThan>\n");
082        computedToString = sb.toString();
083      }
084    }
085    return computedToString;
086  }
087}