begin_unit|revision:0.9.5;language:Java;cregit-version:0.0.1
begin_comment
comment|/**  * Licensed to the Apache Software Foundation (ASF) under one  * or more contributor license agreements.  See the NOTICE file  * distributed with this work for additional information  * regarding copyright ownership.  The ASF licenses this file  * to you under the Apache License, Version 2.0 (the  * "License"); you may not use this file except in compliance  * with the License.  You may obtain a copy of the License at  *  *     http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */
end_comment

begin_package
DECL|package|org.apache.hadoop.fs.shell.find
package|package
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|shell
operator|.
name|find
package|;
end_package

begin_import
import|import
name|java
operator|.
name|io
operator|.
name|IOException
import|;
end_import

begin_import
import|import
name|java
operator|.
name|util
operator|.
name|Deque
import|;
end_import

begin_import
import|import
name|java
operator|.
name|util
operator|.
name|LinkedList
import|;
end_import

begin_import
import|import
name|java
operator|.
name|util
operator|.
name|List
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|conf
operator|.
name|Configurable
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|conf
operator|.
name|Configuration
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|FileStatus
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|FileSystem
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|Path
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|shell
operator|.
name|PathData
import|;
end_import

begin_comment
comment|/**  * Abstract expression for use in the  * {@link org.apache.hadoop.fs.shell.find.Find} command. Provides default  * behavior for a no-argument primary expression.  */
end_comment

begin_class
DECL|class|BaseExpression
specifier|public
specifier|abstract
class|class
name|BaseExpression
implements|implements
name|Expression
implements|,
name|Configurable
block|{
DECL|field|usage
specifier|private
name|String
index|[]
name|usage
init|=
block|{
literal|"Not yet implemented"
block|}
decl_stmt|;
DECL|field|help
specifier|private
name|String
index|[]
name|help
init|=
block|{
literal|"Not yet implemented"
block|}
decl_stmt|;
comment|/** Sets the usage text for this {@link Expression} */
DECL|method|setUsage (String[] usage)
specifier|protected
name|void
name|setUsage
parameter_list|(
name|String
index|[]
name|usage
parameter_list|)
block|{
name|this
operator|.
name|usage
operator|=
name|usage
expr_stmt|;
block|}
comment|/** Sets the help text for this {@link Expression} */
DECL|method|setHelp (String[] help)
specifier|protected
name|void
name|setHelp
parameter_list|(
name|String
index|[]
name|help
parameter_list|)
block|{
name|this
operator|.
name|help
operator|=
name|help
expr_stmt|;
block|}
annotation|@
name|Override
DECL|method|getUsage ()
specifier|public
name|String
index|[]
name|getUsage
parameter_list|()
block|{
return|return
name|this
operator|.
name|usage
return|;
block|}
annotation|@
name|Override
DECL|method|getHelp ()
specifier|public
name|String
index|[]
name|getHelp
parameter_list|()
block|{
return|return
name|this
operator|.
name|help
return|;
block|}
annotation|@
name|Override
DECL|method|setOptions (FindOptions options)
specifier|public
name|void
name|setOptions
parameter_list|(
name|FindOptions
name|options
parameter_list|)
throws|throws
name|IOException
block|{
name|this
operator|.
name|options
operator|=
name|options
expr_stmt|;
for|for
control|(
name|Expression
name|child
range|:
name|getChildren
argument_list|()
control|)
block|{
name|child
operator|.
name|setOptions
argument_list|(
name|options
argument_list|)
expr_stmt|;
block|}
block|}
annotation|@
name|Override
DECL|method|prepare ()
specifier|public
name|void
name|prepare
parameter_list|()
throws|throws
name|IOException
block|{
for|for
control|(
name|Expression
name|child
range|:
name|getChildren
argument_list|()
control|)
block|{
name|child
operator|.
name|prepare
argument_list|()
expr_stmt|;
block|}
block|}
annotation|@
name|Override
DECL|method|finish ()
specifier|public
name|void
name|finish
parameter_list|()
throws|throws
name|IOException
block|{
for|for
control|(
name|Expression
name|child
range|:
name|getChildren
argument_list|()
control|)
block|{
name|child
operator|.
name|finish
argument_list|()
expr_stmt|;
block|}
block|}
comment|/** Options passed in from the {@link Find} command. */
DECL|field|options
specifier|private
name|FindOptions
name|options
decl_stmt|;
comment|/** Hadoop configuration. */
DECL|field|conf
specifier|private
name|Configuration
name|conf
decl_stmt|;
comment|/** Arguments for this expression. */
DECL|field|arguments
specifier|private
name|LinkedList
argument_list|<
name|String
argument_list|>
name|arguments
init|=
operator|new
name|LinkedList
argument_list|<
name|String
argument_list|>
argument_list|()
decl_stmt|;
comment|/** Children of this expression. */
DECL|field|children
specifier|private
name|LinkedList
argument_list|<
name|Expression
argument_list|>
name|children
init|=
operator|new
name|LinkedList
argument_list|<
name|Expression
argument_list|>
argument_list|()
decl_stmt|;
comment|/** Return the options to be used by this expression. */
DECL|method|getOptions ()
specifier|protected
name|FindOptions
name|getOptions
parameter_list|()
block|{
return|return
operator|(
name|this
operator|.
name|options
operator|==
literal|null
operator|)
condition|?
operator|new
name|FindOptions
argument_list|()
else|:
name|this
operator|.
name|options
return|;
block|}
annotation|@
name|Override
DECL|method|setConf (Configuration conf)
specifier|public
name|void
name|setConf
parameter_list|(
name|Configuration
name|conf
parameter_list|)
block|{
name|this
operator|.
name|conf
operator|=
name|conf
expr_stmt|;
block|}
annotation|@
name|Override
DECL|method|getConf ()
specifier|public
name|Configuration
name|getConf
parameter_list|()
block|{
return|return
name|this
operator|.
name|conf
return|;
block|}
annotation|@
name|Override
DECL|method|toString ()
specifier|public
name|String
name|toString
parameter_list|()
block|{
name|StringBuilder
name|sb
init|=
operator|new
name|StringBuilder
argument_list|()
decl_stmt|;
name|sb
operator|.
name|append
argument_list|(
name|getClass
argument_list|()
operator|.
name|getSimpleName
argument_list|()
argument_list|)
operator|.
name|append
argument_list|(
literal|"("
argument_list|)
expr_stmt|;
name|boolean
name|firstArg
init|=
literal|true
decl_stmt|;
for|for
control|(
name|String
name|arg
range|:
name|getArguments
argument_list|()
control|)
block|{
if|if
condition|(
operator|!
name|firstArg
condition|)
block|{
name|sb
operator|.
name|append
argument_list|(
literal|","
argument_list|)
expr_stmt|;
block|}
else|else
block|{
name|firstArg
operator|=
literal|false
expr_stmt|;
block|}
name|sb
operator|.
name|append
argument_list|(
name|arg
argument_list|)
expr_stmt|;
block|}
name|sb
operator|.
name|append
argument_list|(
literal|";"
argument_list|)
expr_stmt|;
name|firstArg
operator|=
literal|true
expr_stmt|;
for|for
control|(
name|Expression
name|child
range|:
name|getChildren
argument_list|()
control|)
block|{
if|if
condition|(
operator|!
name|firstArg
condition|)
block|{
name|sb
operator|.
name|append
argument_list|(
literal|","
argument_list|)
expr_stmt|;
block|}
else|else
block|{
name|firstArg
operator|=
literal|false
expr_stmt|;
block|}
name|sb
operator|.
name|append
argument_list|(
name|child
operator|.
name|toString
argument_list|()
argument_list|)
expr_stmt|;
block|}
name|sb
operator|.
name|append
argument_list|(
literal|")"
argument_list|)
expr_stmt|;
return|return
name|sb
operator|.
name|toString
argument_list|()
return|;
block|}
annotation|@
name|Override
DECL|method|isAction ()
specifier|public
name|boolean
name|isAction
parameter_list|()
block|{
for|for
control|(
name|Expression
name|child
range|:
name|getChildren
argument_list|()
control|)
block|{
if|if
condition|(
name|child
operator|.
name|isAction
argument_list|()
condition|)
block|{
return|return
literal|true
return|;
block|}
block|}
return|return
literal|false
return|;
block|}
annotation|@
name|Override
DECL|method|isOperator ()
specifier|public
name|boolean
name|isOperator
parameter_list|()
block|{
return|return
literal|false
return|;
block|}
comment|/**    * Returns the arguments of this expression    *    * @return list of argument strings    */
DECL|method|getArguments ()
specifier|protected
name|List
argument_list|<
name|String
argument_list|>
name|getArguments
parameter_list|()
block|{
return|return
name|this
operator|.
name|arguments
return|;
block|}
comment|/**    * Returns the argument at the given position (starting from 1).    *    * @param position    *          argument to be returned    * @return requested argument    * @throws IOException    *           if the argument doesn't exist or is null    */
DECL|method|getArgument (int position)
specifier|protected
name|String
name|getArgument
parameter_list|(
name|int
name|position
parameter_list|)
throws|throws
name|IOException
block|{
if|if
condition|(
name|position
operator|>
name|this
operator|.
name|arguments
operator|.
name|size
argument_list|()
condition|)
block|{
throw|throw
operator|new
name|IOException
argument_list|(
literal|"Missing argument at "
operator|+
name|position
argument_list|)
throw|;
block|}
name|String
name|argument
init|=
name|this
operator|.
name|arguments
operator|.
name|get
argument_list|(
name|position
operator|-
literal|1
argument_list|)
decl_stmt|;
if|if
condition|(
name|argument
operator|==
literal|null
condition|)
block|{
throw|throw
operator|new
name|IOException
argument_list|(
literal|"Null argument at position "
operator|+
name|position
argument_list|)
throw|;
block|}
return|return
name|argument
return|;
block|}
comment|/**    * Returns the children of this expression.    *    * @return list of child expressions    */
DECL|method|getChildren ()
specifier|protected
name|List
argument_list|<
name|Expression
argument_list|>
name|getChildren
parameter_list|()
block|{
return|return
name|this
operator|.
name|children
return|;
block|}
annotation|@
name|Override
DECL|method|getPrecedence ()
specifier|public
name|int
name|getPrecedence
parameter_list|()
block|{
return|return
literal|0
return|;
block|}
annotation|@
name|Override
DECL|method|addChildren (Deque<Expression> exprs)
specifier|public
name|void
name|addChildren
parameter_list|(
name|Deque
argument_list|<
name|Expression
argument_list|>
name|exprs
parameter_list|)
block|{
comment|// no children by default, will be overridden by specific expressions.
block|}
comment|/**    * Add a specific number of children to this expression. The children are    * popped off the head of the expressions.    *    * @param exprs    *          deque of expressions from which to take the children    * @param count    *          number of children to be added    */
DECL|method|addChildren (Deque<Expression> exprs, int count)
specifier|protected
name|void
name|addChildren
parameter_list|(
name|Deque
argument_list|<
name|Expression
argument_list|>
name|exprs
parameter_list|,
name|int
name|count
parameter_list|)
block|{
for|for
control|(
name|int
name|i
init|=
literal|0
init|;
name|i
operator|<
name|count
condition|;
name|i
operator|++
control|)
block|{
name|addChild
argument_list|(
name|exprs
operator|.
name|pop
argument_list|()
argument_list|)
expr_stmt|;
block|}
block|}
comment|/**    * Add a single argument to this expression. The argument is popped off the    * head of the expressions.    *    * @param expr    *          child to add to the expression    */
DECL|method|addChild (Expression expr)
specifier|private
name|void
name|addChild
parameter_list|(
name|Expression
name|expr
parameter_list|)
block|{
name|children
operator|.
name|push
argument_list|(
name|expr
argument_list|)
expr_stmt|;
block|}
annotation|@
name|Override
DECL|method|addArguments (Deque<String> args)
specifier|public
name|void
name|addArguments
parameter_list|(
name|Deque
argument_list|<
name|String
argument_list|>
name|args
parameter_list|)
block|{
comment|// no children by default, will be overridden by specific expressions.
block|}
comment|/**    * Add a specific number of arguments to this expression. The children are    * popped off the head of the expressions.    *    * @param args    *          deque of arguments from which to take the argument    * @param count    *          number of children to be added    */
DECL|method|addArguments (Deque<String> args, int count)
specifier|protected
name|void
name|addArguments
parameter_list|(
name|Deque
argument_list|<
name|String
argument_list|>
name|args
parameter_list|,
name|int
name|count
parameter_list|)
block|{
for|for
control|(
name|int
name|i
init|=
literal|0
init|;
name|i
operator|<
name|count
condition|;
name|i
operator|++
control|)
block|{
name|addArgument
argument_list|(
name|args
operator|.
name|pop
argument_list|()
argument_list|)
expr_stmt|;
block|}
block|}
comment|/**    * Add a single argument to this expression. The argument is popped off the    * head of the expressions.    *    * @param arg    *          argument to add to the expression    */
DECL|method|addArgument (String arg)
specifier|protected
name|void
name|addArgument
parameter_list|(
name|String
name|arg
parameter_list|)
block|{
name|arguments
operator|.
name|add
argument_list|(
name|arg
argument_list|)
expr_stmt|;
block|}
comment|/**    * Returns the {@link FileStatus} from the {@link PathData} item. If the    * current options require links to be followed then the returned file status    * is that of the linked file.    *    * @param item    *          PathData    * @param depth    *          current depth in the process directories    * @return FileStatus    */
DECL|method|getFileStatus (PathData item, int depth)
specifier|protected
name|FileStatus
name|getFileStatus
parameter_list|(
name|PathData
name|item
parameter_list|,
name|int
name|depth
parameter_list|)
throws|throws
name|IOException
block|{
name|FileStatus
name|fileStatus
init|=
name|item
operator|.
name|stat
decl_stmt|;
if|if
condition|(
name|fileStatus
operator|.
name|isSymlink
argument_list|()
condition|)
block|{
if|if
condition|(
name|options
operator|.
name|isFollowLink
argument_list|()
operator|||
operator|(
name|options
operator|.
name|isFollowArgLink
argument_list|()
operator|&&
operator|(
name|depth
operator|==
literal|0
operator|)
operator|)
condition|)
block|{
name|Path
name|linkedFile
init|=
name|item
operator|.
name|fs
operator|.
name|resolvePath
argument_list|(
name|fileStatus
operator|.
name|getSymlink
argument_list|()
argument_list|)
decl_stmt|;
name|fileStatus
operator|=
name|getFileSystem
argument_list|(
name|item
argument_list|)
operator|.
name|getFileStatus
argument_list|(
name|linkedFile
argument_list|)
expr_stmt|;
block|}
block|}
return|return
name|fileStatus
return|;
block|}
comment|/**    * Returns the {@link Path} from the {@link PathData} item.    *    * @param item    *          PathData    * @return Path    */
DECL|method|getPath (PathData item)
specifier|protected
name|Path
name|getPath
parameter_list|(
name|PathData
name|item
parameter_list|)
throws|throws
name|IOException
block|{
return|return
name|item
operator|.
name|path
return|;
block|}
comment|/**    * Returns the {@link FileSystem} associated with the {@link PathData} item.    *    * @param item PathData    * @return FileSystem    */
DECL|method|getFileSystem (PathData item)
specifier|protected
name|FileSystem
name|getFileSystem
parameter_list|(
name|PathData
name|item
parameter_list|)
throws|throws
name|IOException
block|{
return|return
name|item
operator|.
name|fs
return|;
block|}
block|}
end_class

end_unit

