Coverage Report - org.dishevelled.commandline.argument.AbstractArgument
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractArgument
100%
36/36
95%
19/20
1.857
 
 1  
 /*
 2  
 
 3  
     dsh-commandline  Command line parser based on typed arguments.
 4  
     Copyright (c) 2004-2012 held jointly by the individual authors.
 5  
 
 6  
     This library is free software; you can redistribute it and/or modify it
 7  
     under the terms of the GNU Lesser General Public License as published
 8  
     by the Free Software Foundation; either version 3 of the License, or (at
 9  
     your option) any later version.
 10  
 
 11  
     This library is distributed in the hope that it will be useful, but WITHOUT
 12  
     ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
 13  
     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 14  
     License for more details.
 15  
 
 16  
     You should have received a copy of the GNU Lesser General Public License
 17  
     along with this library;  if not, write to the Free Software Foundation,
 18  
     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 19  
 
 20  
     > http://www.fsf.org/licensing/licenses/lgpl.html
 21  
     > http://www.opensource.org/licenses/lgpl-license.php
 22  
 
 23  
 */
 24  
 package org.dishevelled.commandline.argument;
 25  
 
 26  
 import org.dishevelled.commandline.Argument;
 27  
 import org.dishevelled.commandline.CommandLine;
 28  
 
 29  
 /**
 30  
  * Abstract implementation of Argument.
 31  
  *
 32  
  * @param <E> argument type
 33  
  * @author  Michael Heuer
 34  
  * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
 35  
  */
 36  
 public abstract class AbstractArgument<E>
 37  
     implements Argument<E>
 38  
 {
 39  
     /** Short name. */
 40  
     private final String shortName;
 41  
 
 42  
     /** Long name. */
 43  
     private final String longName;
 44  
 
 45  
     /** Description. */
 46  
     private final String description;
 47  
 
 48  
     /** Is required. */
 49  
     private final boolean required;
 50  
 
 51  
     /** Value. */
 52  
     private E value;
 53  
 
 54  
     /** True if this argument was found. */
 55  
     private boolean found;
 56  
 
 57  
 
 58  
     /**
 59  
      * Create a new abstract argument.
 60  
      *
 61  
      * @param shortName short argument name
 62  
      * @param longName long argument name
 63  
      * @param description argument description
 64  
      * @param required <code>true</code> if this argument is required
 65  
      */
 66  
     protected AbstractArgument(final String shortName,
 67  
                                final String longName,
 68  
                                final String description,
 69  
                                final boolean required)
 70  233
     {
 71  233
         this.shortName = shortName;
 72  233
         this.longName = longName;
 73  233
         this.description = description;
 74  233
         this.required = required;
 75  233
         this.found = false;
 76  233
     }
 77  
 
 78  
 
 79  
     /** {@inheritDoc} */
 80  
     public final void visit(final String current, final CommandLine commandLine)
 81  
         throws Exception
 82  
     {
 83  911
         if (isArgumentString(current))
 84  
         {
 85  405
             found = true;
 86  405
             return;
 87  
         }
 88  
 
 89  506
         if (isValueString(current))
 90  
         {
 91  500
             if (commandLine.hasPrevious())
 92  
             {
 93  500
                 commandLine.previous();
 94  500
                 if (commandLine.hasPrevious())
 95  
                 {
 96  446
                     String previous = commandLine.previous();
 97  446
                     if (isArgumentString(previous))
 98  
                     {
 99  396
                         value = convert(current);
 100  
                     }
 101  366
                     commandLine.next();
 102  
                 }
 103  420
                 commandLine.next();
 104  
             }
 105  
         }
 106  426
     }
 107  
 
 108  
     /**
 109  
      * Return true if the specified string is an argument name.
 110  
      *
 111  
      * @param s string
 112  
      * @return true if the specified string is an argument name
 113  
      */
 114  
     protected boolean isArgumentString(final String s)
 115  
     {
 116  1357
         return ("-" + getShortName()).equals(s)
 117  
             || ("--" + getLongName()).equals(s);
 118  
     }
 119  
 
 120  
     /**
 121  
      * Return true if the specified string is a value.
 122  
      *
 123  
      * @param s string
 124  
      * @return true if the specified string is a value
 125  
      */
 126  
     protected boolean isValueString(final String s)
 127  
     {
 128  506
         if (s.startsWith("-"))
 129  
         {
 130  58
             char next = s.charAt(1);
 131  58
             if (Character.isDigit(next))
 132  
             {
 133  
                 // allow negative numbers
 134  52
                 return true;
 135  
             }
 136  6
             return false;
 137  
         }
 138  448
         return true;
 139  
     }
 140  
 
 141  
     /**
 142  
      * Convert the specified string value into an instance
 143  
      * of class <code>E</code>.
 144  
      *
 145  
      * @param s string value
 146  
      * @return an instance of class <code>E</code> converted
 147  
      *    from the specified string value
 148  
      * @throws Exception if an error occurs
 149  
      */
 150  
     protected abstract E convert(final String s)
 151  
         throws Exception;
 152  
 
 153  
 
 154  
     /** {@inheritDoc} */
 155  
     public final String getShortName()
 156  
     {
 157  1805
         return shortName;
 158  
     }
 159  
 
 160  
     /** {@inheritDoc} */
 161  
     public final String getLongName()
 162  
     {
 163  1400
         return longName;
 164  
     }
 165  
 
 166  
     /** {@inheritDoc} */
 167  
     public final String getDescription()
 168  
     {
 169  31
         return description;
 170  
     }
 171  
 
 172  
     /** {@inheritDoc} */
 173  
     public final boolean isRequired()
 174  
     {
 175  437
         return required;
 176  
     }
 177  
 
 178  
     /** {@inheritDoc} */
 179  
     public final boolean wasFound()
 180  
     {
 181  412
         return found;
 182  
     }
 183  
 
 184  
     /** {@inheritDoc} */
 185  
     public final E getValue()
 186  
     {
 187  382
         return value;
 188  
     }
 189  
 
 190  
     /** {@inheritDoc} */
 191  
     public final E getValue(final E defaultValue)
 192  
     {
 193  3
         return found ? value : defaultValue;
 194  
     }
 195  
 
 196  
     /** {@inheritDoc} */
 197  
     public final boolean equals(final Object o)
 198  
     {
 199  28
         return super.equals(o);
 200  
     }
 201  
 
 202  
     /** {@inheritDoc} */
 203  
     public final int hashCode()
 204  
     {
 205  24
         return super.hashCode();
 206  
     }
 207  
 }