Coverage Report - org.dishevelled.commandline.Switch
 
Classes in this File Line Coverage Branch Coverage Complexity
Switch
94%
16/17
100%
6/6
1.333
 
 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;
 25  
 
 26  
 /**
 27  
  * Switch.
 28  
  *
 29  
  * @author  Michael Heuer
 30  
  * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
 31  
  */
 32  0
 public final class Switch
 33  
     implements Argument<Boolean>
 34  
 {
 35  
     /** Short name. */
 36  
     private final String shortName;
 37  
 
 38  
     /** Long name. */
 39  
     private final String longName;
 40  
 
 41  
     /** Description. */
 42  
     private final String description;
 43  
 
 44  
     /** True if this switch was found. */
 45  
     private boolean found;
 46  
 
 47  
 
 48  
     /**
 49  
      * Create a new switch.
 50  
      *
 51  
      * @param shortName short switch name
 52  
      * @param longName long switch name
 53  
      * @param description switch description
 54  
      */
 55  
     public Switch(final String shortName,
 56  
                   final String longName,
 57  
                   final String description)
 58  9
     {
 59  9
         this.shortName = shortName;
 60  9
         this.longName = longName;
 61  9
         this.description = description;
 62  9
         this.found = false;
 63  9
     }
 64  
 
 65  
 
 66  
     /** {@inheritDoc} */
 67  
     public void visit(final String current, final CommandLine commandLine)
 68  
         throws Exception
 69  
     {
 70  4
         if (("-" + getShortName()).equals(current) || ("--" + getLongName()).equals(current))
 71  
         {
 72  2
             found = true;
 73  
         }
 74  4
     }
 75  
 
 76  
     /** {@inheritDoc} */
 77  
     public String getShortName()
 78  
     {
 79  11
         return shortName;
 80  
     }
 81  
 
 82  
     /** {@inheritDoc} */
 83  
     public String getLongName()
 84  
     {
 85  10
         return longName;
 86  
     }
 87  
 
 88  
     /** {@inheritDoc} */
 89  
     public String getDescription()
 90  
     {
 91  1
         return description;
 92  
     }
 93  
 
 94  
     /** {@inheritDoc} */
 95  
     public boolean isRequired()
 96  
     {
 97  4
         return false;
 98  
     }
 99  
 
 100  
     /** {@inheritDoc} */
 101  
     public boolean wasFound()
 102  
     {
 103  1
         return found;
 104  
     }
 105  
 
 106  
     /** {@inheritDoc} */
 107  
     public Boolean getValue()
 108  
     {
 109  4
         return Boolean.valueOf(found);
 110  
     }
 111  
 
 112  
     /** {@inheritDoc} */
 113  
     public Boolean getValue(final Boolean defaultValue)
 114  
     {
 115  4
         return found ? Boolean.TRUE : defaultValue;
 116  
     }
 117  
 }