Return-Path: X-Original-To: apmail-hadoop-common-commits-archive@www.apache.org Delivered-To: apmail-hadoop-common-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 248F54ACC for ; Mon, 23 May 2011 17:48:39 +0000 (UTC) Received: (qmail 65633 invoked by uid 500); 23 May 2011 17:48:38 -0000 Delivered-To: apmail-hadoop-common-commits-archive@hadoop.apache.org Received: (qmail 65609 invoked by uid 500); 23 May 2011 17:48:38 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: common-dev@hadoop.apache.org Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 65602 invoked by uid 99); 23 May 2011 17:48:38 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 May 2011 17:48:38 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 May 2011 17:48:34 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 0ED3C238897F; Mon, 23 May 2011 17:48:13 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1126615 - in /hadoop/common/branches/yahoo-merge: ./ CHANGES.txt src/contrib/ec2/ src/docs/ src/java/ src/java/org/apache/hadoop/fs/shell/CommandFormat.java src/test/core/ src/test/core/org/apache/hadoop/fs/TestCommandFormat.java Date: Mon, 23 May 2011 17:48:12 -0000 To: common-commits@hadoop.apache.org From: szetszwo@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110523174813.0ED3C238897F@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: szetszwo Date: Mon May 23 17:48:12 2011 New Revision: 1126615 URL: http://svn.apache.org/viewvc?rev=1126615&view=rev Log: Merge 1082787 and 1082788 from trunk for HADOOP-7180. Added: hadoop/common/branches/yahoo-merge/src/test/core/org/apache/hadoop/fs/TestCommandFormat.java - copied unchanged from r1082788, hadoop/common/trunk/src/test/core/org/apache/hadoop/fs/TestCommandFormat.java Modified: hadoop/common/branches/yahoo-merge/ (props changed) hadoop/common/branches/yahoo-merge/CHANGES.txt (contents, props changed) hadoop/common/branches/yahoo-merge/src/contrib/ec2/ (props changed) hadoop/common/branches/yahoo-merge/src/docs/ (props changed) hadoop/common/branches/yahoo-merge/src/java/ (props changed) hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/shell/CommandFormat.java hadoop/common/branches/yahoo-merge/src/test/core/ (props changed) Propchange: hadoop/common/branches/yahoo-merge/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon May 23 17:48:12 2011 @@ -1,2 +1,2 @@ -/hadoop/common/trunk:1043117,1078148,1080396,1081598,1082329,1087844,1091618,1092565,1100026 +/hadoop/common/trunk:1043117,1078148,1080396,1081598,1082329,1082787-1082788,1087844,1091618,1092565,1100026 /hadoop/core/branches/branch-0.19/core:713112 Modified: hadoop/common/branches/yahoo-merge/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/common/branches/yahoo-merge/CHANGES.txt?rev=1126615&r1=1126614&r2=1126615&view=diff ============================================================================== --- hadoop/common/branches/yahoo-merge/CHANGES.txt (original) +++ hadoop/common/branches/yahoo-merge/CHANGES.txt Mon May 23 17:48:12 2011 @@ -22,6 +22,9 @@ Trunk (unreleased changes) HADOOP-7175. Add isEnabled() to Trash. (Daryn Sharp via szetszwo) + HADOOP-7180. Better support on CommandFormat on the API and exceptions. + (Daryn Sharp via szetszwo) + OPTIMIZATIONS BUG FIXES Propchange: hadoop/common/branches/yahoo-merge/CHANGES.txt ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon May 23 17:48:12 2011 @@ -1,4 +1,4 @@ -/hadoop/common/trunk/CHANGES.txt:1043117,1078148,1080396,1081598,1082329,1087844,1091618,1092565,1092832,1100026 +/hadoop/common/trunk/CHANGES.txt:1043117,1078148,1080396,1081598,1082329,1082787-1082788,1087844,1091618,1092565,1092832,1100026 /hadoop/core/branches/branch-0.18/CHANGES.txt:727226 /hadoop/core/branches/branch-0.19/CHANGES.txt:713112 /hadoop/core/trunk/CHANGES.txt:776175-785643,785929-786278 Propchange: hadoop/common/branches/yahoo-merge/src/contrib/ec2/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon May 23 17:48:12 2011 @@ -1,3 +1,3 @@ -/hadoop/common/trunk/src/contrib/ec2:1043117,1078148,1081598,1082329,1087844,1091618,1092565,1100026 +/hadoop/common/trunk/src/contrib/ec2:1043117,1078148,1081598,1082329,1082787-1082788,1087844,1091618,1092565,1100026 /hadoop/core/branches/branch-0.19/core/src/contrib/ec2:713112 /hadoop/core/trunk/src/contrib/ec2:776175-784663 Propchange: hadoop/common/branches/yahoo-merge/src/docs/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon May 23 17:48:12 2011 @@ -1,2 +1,2 @@ -/hadoop/common/trunk/src/docs:1043117,1078148,1080396,1081598,1082329,1087844,1091618,1092565,1100026 +/hadoop/common/trunk/src/docs:1043117,1078148,1080396,1081598,1082329,1082787-1082788,1087844,1091618,1092565,1100026 /hadoop/core/branches/branch-0.19/src/docs:713112 Propchange: hadoop/common/branches/yahoo-merge/src/java/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon May 23 17:48:12 2011 @@ -1,3 +1,3 @@ -/hadoop/common/trunk/src/java:1043117,1078148,1080396,1081598,1082329,1087844,1091618,1092565,1100026 +/hadoop/common/trunk/src/java:1043117,1078148,1080396,1081598,1082329,1082787-1082788,1087844,1091618,1092565,1100026 /hadoop/core/branches/branch-0.19/core/src/java:713112 /hadoop/core/trunk/src/core:776175-785643,785929-786278 Modified: hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/shell/CommandFormat.java URL: http://svn.apache.org/viewvc/hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/shell/CommandFormat.java?rev=1126615&r1=1126614&r2=1126615&view=diff ============================================================================== --- hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/shell/CommandFormat.java (original) +++ hadoop/common/branches/yahoo-merge/src/java/org/apache/hadoop/fs/shell/CommandFormat.java Mon May 23 17:48:12 2011 @@ -18,9 +18,12 @@ package org.apache.hadoop.fs.shell; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; /** * Parse the args of a command and check the format of args. @@ -40,28 +43,46 @@ public class CommandFormat { } /** Parse parameters starting from the given position + * Consider using the variant that directly takes a List * * @param args an array of input arguments * @param pos the position at which starts to parse * @return a list of parameters */ public List parse(String[] args, int pos) { - List parameters = new ArrayList(); - for(; pos < args.length; pos++) { - if (args[pos].charAt(0) == '-' && args[pos].length() > 1) { - String opt = args[pos].substring(1); - if (options.containsKey(opt)) - options.put(opt, Boolean.TRUE); - else - throw new IllegalArgumentException("Illegal option " + args[pos]); + List parameters = new ArrayList(Arrays.asList(args)); + parameters.subList(0, pos).clear(); + parse(parameters); + return parameters; + } + + /** Parse parameters from the given list of args. The list is + * destructively modified to remove the options. + * + * @param args as a list of input arguments + */ + public void parse(List args) { + int pos = 0; + while (pos < args.size()) { + String arg = args.get(pos); + if (arg.startsWith("-") && arg.length() > 1) { + String opt = arg.substring(1); + if (!options.containsKey(opt)) { + throw new UnknownOptionException(arg); + } + args.remove(pos); + options.put(opt, Boolean.TRUE); + } else { + pos++; } - else - parameters.add(args[pos]); } - int psize = parameters.size(); - if (psize < minPar || psize > maxPar) - throw new IllegalArgumentException("Illegal number of arguments"); - return parameters; + int psize = args.size(); + if (psize < minPar) { + throw new NotEnoughArgumentsException(minPar, psize); + } + if (psize > maxPar) { + throw new TooManyArgumentsException(maxPar, psize); + } } /** Return if the option is set or not @@ -70,6 +91,84 @@ public class CommandFormat { * @return true is the option is set; false otherwise */ public boolean getOpt(String option) { - return options.get(option); + return options.containsKey(option) ? options.get(option) : false; + } + + /** Returns all the options that are set + * + * @return Set of the enabled options + */ + public Set getOpts() { + Set optSet = new HashSet(); + for (Map.Entry entry : options.entrySet()) { + if (entry.getValue()) { + optSet.add(entry.getKey()); + } + } + return optSet; + } + + /** Used when the arguments exceed their bounds + */ + public static abstract class IllegalNumberOfArgumentsException + extends IllegalArgumentException { + private static final long serialVersionUID = 0L; + protected int expected; + protected int actual; + + protected IllegalNumberOfArgumentsException(int want, int got) { + expected = want; + actual = got; + } + + public String getMessage() { + return "expected " + expected + " but got " + actual; + } + } + + /** Used when too many arguments are supplied to a command + */ + public static class TooManyArgumentsException + extends IllegalNumberOfArgumentsException { + private static final long serialVersionUID = 0L; + + public TooManyArgumentsException(int expected, int actual) { + super(expected, actual); + } + + public String getMessage() { + return "Too many arguments: " + super.getMessage(); + } + } + + /** Used when too few arguments are supplied to a command + */ + public static class NotEnoughArgumentsException + extends IllegalNumberOfArgumentsException { + private static final long serialVersionUID = 0L; + + public NotEnoughArgumentsException(int expected, int actual) { + super(expected, actual); + } + + public String getMessage() { + return "Not enough arguments: " + super.getMessage(); + } + } + + /** Used when an unsupported option is supplied to a command + */ + public static class UnknownOptionException extends IllegalArgumentException { + private static final long serialVersionUID = 0L; + protected String option = null; + + public UnknownOptionException(String unknownOption) { + super("Illegal option " + unknownOption); + option = unknownOption; + } + + public String getOption() { + return option; + } } } Propchange: hadoop/common/branches/yahoo-merge/src/test/core/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Mon May 23 17:48:12 2011 @@ -1,3 +1,3 @@ -/hadoop/common/trunk/src/test/core:1043117,1078148,1080396,1081598,1082329,1087844,1091618,1092565,1100026 +/hadoop/common/trunk/src/test/core:1043117,1078148,1080396,1081598,1082329,1082787-1082788,1087844,1091618,1092565,1100026 /hadoop/core/branches/branch-0.19/core/src/test/core:713112 /hadoop/core/trunk/src/test/core:776175-785643,785929-786278