Home | History | Annotate | Download | only in command
      1 /*
      2  * Copyright 2007 the original author or authors.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.mockftpserver.core.command;
     17 
     18 import java.util.Arrays;
     19 
     20 import org.mockftpserver.core.util.Assert;
     21 
     22 /**
     23  * Represents a command received from an FTP client, containing a command name and parameters.
     24  * Objects of this class are immutable.
     25  *
     26  * @version $Revision$ - $Date$
     27  *
     28  * @author Chris Mair
     29  */
     30 public final class Command {
     31 
     32     private String name;
     33     private String[] parameters;
     34 
     35     /**
     36      * Construct a new immutable instance with the specified command name and parameters
     37      * @param name - the command name; may not be null
     38      * @param parameters - the command parameters; may be empty; may not benull
     39      */
     40     public Command(String name, String[] parameters) {
     41         Assert.notNull(name, "name");
     42         Assert.notNull(parameters, "parameters");
     43         this.name = name;
     44         this.parameters = copy(parameters);
     45     }
     46 
     47     /**
     48      * @return the name
     49      */
     50     public String getName() {
     51         return name;
     52     }
     53 
     54     /**
     55      * @return the parameters
     56      */
     57     public String[] getParameters() {
     58         return copy(parameters);
     59     }
     60 
     61     /**
     62      * Get the String value of the parameter at the specified index
     63      * @param index - the index
     64      * @return the parameter value as a String
     65      * @throws AssertFailedException if the parameter index is invalid or the value is not a valid String
     66      */
     67     public String getRequiredString(int index) {
     68         assertValidIndex(index);
     69         return parameters[index];
     70     }
     71 
     72     /**
     73      * Get the String value of the parameter at the specified index; return null if no parameter exists for the index
     74      * @param index - the index
     75      * @return the parameter value as a String, or null if this Command does not have a parameter for that index
     76      */
     77     public String getOptionalString(int index) {
     78         return (parameters.length > index) ? parameters[index] : null;
     79     }
     80 
     81     /**
     82      * @see java.lang.Object#equals(java.lang.Object)
     83      */
     84     public boolean equals(Object obj) {
     85         if (this == obj) {
     86             return true;
     87         }
     88         if (obj == null || !(obj instanceof Command)) {
     89             return false;
     90         }
     91         return this.hashCode() == obj.hashCode();
     92     }
     93 
     94     /**
     95      * @see java.lang.Object#hashCode()
     96      */
     97     public int hashCode() {
     98         String str = name + Arrays.asList(parameters);
     99         return str.hashCode();
    100     }
    101 
    102     /**
    103      * Return the String representation of this object
    104      * @see java.lang.Object#toString()
    105      */
    106     public String toString() {
    107         return "Command[" + name + ": " + Arrays.asList(parameters) + "]";
    108     }
    109 
    110     /**
    111      * Return the name, normalized to a common format - convert to upper case.
    112      * @return the name converted to upper case
    113      */
    114     public static String normalizeName(String name) {
    115         return name.toUpperCase();
    116     }
    117 
    118     /**
    119      * Construct a shallow copy of the specified array
    120      * @param array - the array to copy
    121      * @return a new array with the same contents
    122      */
    123     private static String[] copy(String[] array) {
    124         String[] newArray = new String[array.length];
    125         System.arraycopy(array, 0, newArray, 0, array.length);
    126         return newArray;
    127     }
    128 
    129     /**
    130      * Assert that the index is valid
    131      * @param index - the index
    132      * @throws AssertFailedException if the parameter index is invalid
    133      */
    134     private void assertValidIndex(int index) {
    135         Assert.isTrue(index >= 0 && index < parameters.length, "The parameter index " + index + " is not valid for " + this);
    136     }
    137 
    138 }
    139