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 AndElementExpression extends ElementExpression
034  {
035  public static ElementExpression and( String name, ElementExpression... elementMatchers )
036    {
037    return new AndElementExpression( name, elementMatchers );
038    }
039
040  public static ElementExpression and( String name, ElementCapture capture, ElementExpression... elementMatchers )
041    {
042    return new AndElementExpression( name, capture, elementMatchers );
043    }
044
045  public static ElementExpression and( ElementExpression... elementMatchers )
046    {
047    return new AndElementExpression( elementMatchers );
048    }
049
050  public static ElementExpression and( ElementCapture capture, ElementExpression... elementMatchers )
051    {
052    return new AndElementExpression( capture, elementMatchers );
053    }
054
055  String name;
056  ElementExpression[] matchers;
057
058  public AndElementExpression( String name, ElementExpression... matchers )
059    {
060    this.matchers = matchers;
061    }
062
063  public AndElementExpression( String name, ElementCapture capture, ElementExpression... matchers )
064    {
065    super( capture );
066    this.matchers = matchers;
067    }
068
069  public AndElementExpression( ElementExpression... matchers )
070    {
071    this.matchers = matchers;
072    }
073
074  public AndElementExpression( ElementCapture capture, ElementExpression... matchers )
075    {
076    super( capture );
077    this.matchers = matchers;
078    }
079
080  @Override
081  public boolean applies( PlannerContext plannerContext, ElementGraph elementGraph, FlowElement flowElement )
082    {
083    for( ElementExpression matcher : matchers )
084      {
085      if( !matcher.applies( plannerContext, elementGraph, flowElement ) )
086        return false;
087      }
088
089    return true;
090    }
091
092  @Override
093  public String toString()
094    {
095    if( name != null )
096      return name;
097
098    final StringBuilder sb = new StringBuilder( "And{" );
099    sb.append( Arrays.toString( matchers ) );
100    sb.append( '}' );
101    return sb.toString();
102    }
103  }