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 java.util.*;
022
023import org.jppf.JPPFRuntimeException;
024
025/**
026 * An execution policy that realizes a binary logical combination of the policies specified as operands.
027 */
028public abstract class LogicalRule extends ExecutionPolicy {
029  /**
030   * Explicit serialVersionUID.
031   */
032  private static final long serialVersionUID = 1L;
033
034  /**
035   * Initialize this binary logical operator with the specified operands.
036   * @param rules the first operand.
037   */
038  public LogicalRule(final ExecutionPolicy...rules) {
039    super(rules);
040  }
041
042  /**
043   * Print this object to a string.
044   * @return an XML string representation of this object
045   */
046  @Override
047  public String toString(final int indentLevel) {
048    final StringBuilder sb = new StringBuilder();
049    if (children == null) sb.append(indent(indentLevel)).append("null\n");
050    else {
051      for (final ExecutionPolicy ep: children) sb.append(ep.toString(indentLevel));
052    }
053    return sb.toString();
054  }
055
056  @Override
057  protected ExecutionPolicy[] checkRules(final ExecutionPolicy...rules) throws JPPFRuntimeException {
058    if ((rules != null) && (rules.length > 0)) {
059      final List<ExecutionPolicy> result = new ArrayList<>();
060      for (final ExecutionPolicy rule: rules) {
061        if (rule != null) result.add(rule);
062      }
063      if (result.size() == 1) result.add(result.get(0));
064      if (result.size() > 0) return result.toArray(new ExecutionPolicy[result.size()]);
065    }
066    final String message = String.format("the execution policy rule '%s' must have at lest one non-null argument", getClass().getSimpleName());
067    throw new JPPFRuntimeException(message);
068  }
069}