hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szets...@apache.org
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 GMT
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<String> parse(String[] args, int pos) {
-    List<String> parameters = new ArrayList<String>();
-    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<String> parameters = new ArrayList<String>(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<String> 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<String> of the enabled options
+   */
+  public Set<String> getOpts() {
+    Set<String> optSet = new HashSet<String>();
+    for (Map.Entry<String, Boolean> 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



Mime
View raw message