001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  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 */
018package org.apache.hadoop.fs.shell.find;
019
020import java.io.IOException;
021import java.util.Deque;
022
023import org.apache.hadoop.conf.Configurable;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.shell.PathData;
026
027/**
028 * Provides an abstract composition filter for the {@link Expression} interface.
029 * Allows other {@link Expression} implementations to be reused without
030 * inheritance.
031 */
032public abstract class FilterExpression implements Expression, Configurable {
033  protected Expression expression;
034
035  protected FilterExpression(Expression expression) {
036    this.expression = expression;
037  }
038
039  @Override
040  public void setOptions(FindOptions options) throws IOException {
041    if (expression != null) {
042      expression.setOptions(options);
043    }
044  }
045
046  @Override
047  public void prepare() throws IOException {
048    if (expression != null) {
049      expression.prepare();
050    }
051  }
052
053  @Override
054  public Result apply(PathData item, int depth) throws IOException {
055    if (expression != null) {
056      return expression.apply(item, -1);
057    }
058    return Result.PASS;
059  }
060
061  @Override
062  public void finish() throws IOException {
063    if (expression != null) {
064      expression.finish();
065    }
066  }
067
068  @Override
069  public String[] getUsage() {
070    if (expression != null) {
071      return expression.getUsage();
072    }
073    return null;
074  }
075
076  @Override
077  public String[] getHelp() {
078    if (expression != null) {
079      return expression.getHelp();
080    }
081    return null;
082  }
083
084  @Override
085  public boolean isAction() {
086    if (expression != null) {
087      return expression.isAction();
088    }
089    return false;
090  }
091
092  @Override
093  public boolean isOperator() {
094    if (expression != null) {
095      return expression.isOperator();
096    }
097    return false;
098  }
099
100  @Override
101  public int getPrecedence() {
102    if (expression != null) {
103      return expression.getPrecedence();
104    }
105    return -1;
106  }
107
108  @Override
109  public void addChildren(Deque<Expression> expressions) {
110    if (expression != null) {
111      expression.addChildren(expressions);
112    }
113  }
114
115  @Override
116  public void addArguments(Deque<String> args) {
117    if (expression != null) {
118      expression.addArguments(args);
119    }
120  }
121
122  @Override
123  public void setConf(Configuration conf) {
124    if (expression instanceof Configurable) {
125      ((Configurable) expression).setConf(conf);
126    }
127  }
128
129  @Override
130  public Configuration getConf() {
131    if (expression instanceof Configurable) {
132      return ((Configurable) expression).getConf();
133    }
134    return null;
135  }
136
137  @Override
138  public String toString() {
139    if (expression != null) {
140      return getClass().getSimpleName() + "-" + expression.toString();
141    }
142    return getClass().getSimpleName();
143  }
144}