001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 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>{@code property_value_or_expression contains a}</i>.
025 * The test applies to string values only.
026 * @author Laurent Cohen
027 */
028public class Contains extends LeftOperandRule {
029  /**
030   * Explicit serialVersionUID.
031   */
032  private static final long serialVersionUID = 1L;
033  /**
034   * A string value to compare with.
035   */
036  private Expression<String> value = null;
037  /**
038   * Determines if the comparison should ignore the string case.
039   */
040  private boolean ignoreCase = false;
041
042  /**
043   * Define an contains test between the string value of a property and another string value.
044   * @param propertyNameOrExpression either a literal string which represents a property name, or an expression resolving to a numeric value.
045   * @param ignoreCase determines if the comparison should ignore the string case.
046   * @param a the value to compare with.
047   */
048  public Contains(final String propertyNameOrExpression, final boolean ignoreCase, final String a) {
049    super(ValueType.STRING, propertyNameOrExpression);
050    this.value = new StringExpression(a);
051    this.ignoreCase = ignoreCase;
052  }
053
054  /**
055   * Determines whether this policy accepts the specified node.
056   * @param info system information for the node on which the tasks will run if accepted.
057   * @return true if the node is accepted, false otherwise.
058   */
059  @Override
060  public boolean accepts(final PropertiesCollection<String> info) {
061    final String value = this.value.evaluate(info);
062    if (value == null) return false;
063    final String s = (String) getLeftOperandValue(info);
064    if (s == null) return false;
065    if (ignoreCase) return s.toLowerCase().contains(value.toLowerCase());
066    return s.contains(value);
067  }
068
069  @Override
070  public String toString(final int n) {
071    return new StringBuilder(indent(n)).append("<Contains ignoreCase=\"").append(ignoreCase).append("\">\n")
072      .append(indent(n + 1)).append("<Property>").append(leftOperand.getExpression()).append("</Property>\n")
073      .append(indent(n + 1)).append("<Value>").append(value.getExpression()).append("</Value>\n")
074      .append(indent(n)).append("</Contains>\n").toString();
075  }
076}