Coverage report

  %line %branch
nl.toolforge.karma.core.cmd.CommandDescriptor
70% 
85% 

 1  
 /*
 2  
 Karma core - Core of the Karma application
 3  
 Copyright (C) 2004  Toolforge <www.toolforge.nl>
 4  
 
 5  
 This library is free software; you can redistribute it and/or
 6  
 modify it under the terms of the GNU Lesser General Public
 7  
 License as published by the Free Software Foundation; either
 8  
 version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
 This library is distributed in the hope that it will be useful,
 11  
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 Lesser General Public License for more details.
 14  
 
 15  
 You should have received a copy of the GNU Lesser General Public
 16  
 License along with this library; if not, write to the Free Software
 17  
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 */
 19  
 package nl.toolforge.karma.core.cmd;
 20  
 
 21  
 import org.apache.commons.cli.Options;
 22  
 
 23  
 import java.util.HashSet;
 24  
 import java.util.Iterator;
 25  
 import java.util.Set;
 26  
 import java.util.StringTokenizer;
 27  
 
 28  
 /**
 29  
  * A <code>CommandDescriptor</code> is the object representation of a command as it is specified in a
 30  
  * <code>command.xml</code> file.
 31  
  *
 32  
  * @author D.A. Smedes
 33  
  * @version $Id: CommandDescriptor.java,v 1.21 2004/11/10 23:53:07 asmedes Exp $
 34  
  */
 35  
 public final class CommandDescriptor {
 36  
 
 37  477
   private String name = null;
 38  477
   private String description = null;
 39  477
   private String helpText = null;
 40  477
   private String className = null;
 41  477
   private String aliasString = null;
 42  
 
 43  477
   private Set aliasList = null;
 44  477
   private Options options = null;
 45  
 
 46  477
   public CommandDescriptor(String name, String aliasString) {
 47  477
     this.name = name;
 48  477
     this.aliasString = aliasString;
 49  
 
 50  477
     aliasList = new HashSet();
 51  477
     aliasList.add(name);
 52  
 
 53  477
     createAliasList(aliasString);
 54  477
   }
 55  
 
 56  
   /**
 57  
    * Returns the name of the command represented by this <code>CommandDescriptor</code>.
 58  
    *
 59  
    * @return The name of the command represented by this <code>CommandDescriptor</code>
 60  
    */
 61  
   public String getName() {
 62  260
     return this.name;
 63  
   }
 64  
 
 65  
   /**
 66  190
    * The aliasses of a command are all Strings by which the command can be referenced (including its name).
 67  
    * 
 68  
    * @return
 69  
    */
 70  
   public Set getAliasList() {
 71  393
     return aliasList;
 72  190
   }
 73  760
 
 74  570
   private void createAliasList(String aliasString) {
 75  
 
 76  154
     if (aliasString.indexOf(" ") != -1) {
 77  238
       StringTokenizer tokenizer = new StringTokenizer(aliasString, " ");
 78  420
       while (tokenizer.hasMoreTokens()) {
 79  638
         aliasList.add(tokenizer.nextToken());
 80  
       }
 81  
     } else {
 82  49
       aliasList.add(aliasString);
 83  
     }
 84  154
   }
 85  
 
 86  
   public String getAlias() {
 87  0
     return aliasString;
 88  
   }
 89  
 
 90  38
   public String getDescription() {
 91  38
     return description;
 92  
   }
 93  
 
 94  
   public void setDescription(String description) {
 95  14
     this.description = description;
 96  14
   }
 97  38
 
 98  38
   /**
 99  
    * @param options The name of the command (the lt;options&gt;-child-element attribute of the &lt;command&gt;-element).
 100  
    */
 101  
   public void addOptions(Options options) {
 102  14
     this.options = options;
 103  14
   }
 104  
 
 105  
   public Options getOptions() {
 106  0
     if (options == null) {
 107  0
       return new Options();
 108  
     } else {
 109  0
       return this.options;
 110  
     }
 111  
   }
 112  
 
 113  38
   public String getClassName() {
 114  38
     return className;
 115  
   }
 116  
 
 117  38
   public void setClassName(String className) {
 118  52
     this.className = className;
 119  14
   }
 120  
 
 121  
   public void setHelp(String helpText) {
 122  14
     this.helpText = helpText;
 123  14
   }
 124  
 
 125  
   public String getHelp() {
 126  0
     return this.helpText;
 127  
   }
 128  
 
 129  
   /**
 130  
    * Commands are equal when their names are equal or any alias equals an alias from <code>o</code> or the other way
 131  
    * around.
 132  
    *
 133  
    * @param o The object instance that should be compared with <code>this</code>.
 134  190
    * @return <code>true</code> if this command descriptor is equal to <code>o</code> or <code>null</code> when
 135  
    *   <code>o</code> is not a <code>CommandDescriptor</code> instance or when it is not the same object.
 136  190
    */
 137  190
   public boolean equals(Object o) {
 138  
 
 139  295
     if (o instanceof CommandDescriptor) {
 140  247
 
 141  276
       Set s1 = aliasList;
 142  105
       Set s2 = ((CommandDescriptor) o).aliasList;
 143  
 
 144  105
       for (Iterator i = s1.iterator(); i.hasNext();) {
 145  173
         if (s2.contains((String) i.next())) {
 146  167
           return true;
 147  
         }
 148  
       }
 149  
 
 150  33
       for (Iterator i = s2.iterator(); i.hasNext();) {
 151  56
         if (s1.contains((String) i.next())) {
 152  0
           return true;
 153  
         }
 154  
       }
 155  14
       return false;
 156  
     } else {
 157  0
       return false;
 158  
     }
 159  
   }
 160  
 
 161  
 //
 162  
 //  public int hashCode() {
 163  
 //    return (aliasList == null ? 0 : aliasList.hashCode());
 164  
 //  }
 165  
 
 166  
   public String toString() {
 167  
 
 168  0
     String a = "";
 169  0
     for (Iterator i = aliasList.iterator(); i.hasNext();) {
 170  0
       a += (String) i.next();
 171  0
       if (i.hasNext()) {
 172  0
         a += ", ";
 173  
       }
 174  
     }
 175  0
     return name + " (" + a + ")";
 176  
   }
 177  
 }
 178  
 
 179  
 

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.