    001/*
    002 * Copyright (c) 2016 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
    003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
    004 *
    005 * Project and contact information: http://www.cascading.org/
    006 *
    007 * This file is part of the Cascading project.
    008 *
    009 * Licensed under the Apache License, Version 2.0 (the "License");
    010 * you may not use this file except in compliance with the License.
    011 * You may obtain a copy of the License at
    012 *
    013 *     http://www.apache.org/licenses/LICENSE-2.0
    014 *
    015 * Unless required by applicable law or agreed to in writing, software
    016 * distributed under the License is distributed on an "AS IS" BASIS,
    017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    018 * See the License for the specific language governing permissions and
    019 * limitations under the License.
    020 */
    021
    022package cascading.flow.planner.iso.expression;
    023
    024import java.util.Arrays;
    025
    026import cascading.flow.FlowElement;
    027import cascading.flow.planner.PlannerContext;
    028import cascading.flow.planner.graph.ElementGraph;
    029
    030/**
    031 *
    032 */
    033public class OrElementExpression extends ElementExpression
    034  {
    035  public static ElementExpression or( String name, ElementExpression... elementMatchers )
    036    {
    037    return new OrElementExpression( name, elementMatchers );
    038    }
    039
    040  public static ElementExpression or( String name, ElementCapture capture, ElementExpression... elementMatchers )
    041    {
    042    return new OrElementExpression( name, capture, elementMatchers );
    043    }
    044
    045  public static ElementExpression or( ElementExpression... elementMatchers )
    046    {
    047    return new OrElementExpression( elementMatchers );
    048    }
    049
    050  public static ElementExpression or( ElementCapture capture, ElementExpression... elementMatchers )
    051    {
    052    return new OrElementExpression( capture, elementMatchers );
    053    }
    054
    055  String name;
    056  ElementExpression[] matchers;
    057
    058  public OrElementExpression( String name, ElementExpression... matchers )
    059    {
    060    this.name = name;
    061    this.matchers = matchers;
    062    }
    063
    064  public OrElementExpression( String name, ElementCapture capture, ElementExpression... matchers )
    065    {
    066    super( capture );
    067    this.name = name;
    068    this.matchers = matchers;
    069    }
    070
    071  public OrElementExpression( ElementExpression... matchers )
    072    {
    073    this.matchers = matchers;
    074    }
    075
    076  public OrElementExpression( ElementCapture capture, ElementExpression... matchers )
    077    {
    078    super( capture );
    079    this.matchers = matchers;
    080    }
    081
    082  @Override
    083  public boolean applies( PlannerContext plannerContext, ElementGraph elementGraph, FlowElement flowElement )
    084    {
    085    for( ElementExpression matcher : matchers )
    086      {
    087      if( matcher.applies( plannerContext, elementGraph, flowElement ) )
    088        return true;
    089      }
    090
    091    return false;
    092    }
    093
    094  @Override
    095  public String toString()
    096    {
    097    if( name != null )
    098      return name;
    099
    100    final StringBuilder sb = new StringBuilder( "Or{" );
    101    sb.append( Arrays.toString( matchers ) );
    102    sb.append( '}' );
    103    return sb.toString();
    104    }
    105  }