views:

610

answers:

3

I would like to create an initialisation method for a Java class that accepts 3 parameters:

Employee[] method( String[] employeeNames, Integer[] employeeAges, float[] employeeSalaries )
{
    Employee myEmployees[] = new Employee[SIZE];// dont know what size is

    for ( int count = 0; count < SIZE; count++)
    {
        myEmployees[count] = new Employee( employeeNames[count], employeeAges[count], employeeSalaries[count] );
    }
    return myEmployees;
}

You may notice that this code is wrong. THe SIZE variable is not defined. My problem is that I would like to pass in 3 arrays, but I would like to know if I can ensure that the three arrays are ALL of the same array size. This way the for loop will not fail, as the constructor in the for loop uses all the parameters of the arrays.

Perhaps Java has a different feature that can enforce a solution to my problem. I could accept another parameter called SIZE which will be used in the for loop, but that doesn't solve my problem if parameters 1 and 2 are of size 10 and the 3rd parameter is an array of size 9.

So just to rehash incase I wasn't clear. How can I enforce that the 3 arguments are all arrays that contain the exact same number of elements?

Using an extra parameter that specifies the array sizes isn't very elegant and kind of dirty. It also doesn't solve the problem the array parameters contain different sized arrays.

+8  A: 

You can't enforce that at compile-time. You basically have to check it at execution time, and throw an exception if the constraint isn't met:

Employee[] method(String[] employeeNames,
                  Integer[] employeeAges,
                  float[] employeeSalaries)
{
    if (employeeNames == null
        || employeeAges == null 
        || employeeSalaries == null)
    {
        throw new NullPointerException();
    }
    int size = employeeNames.length;
    if (employeesAges.length != size || employeeSalaries.length != size)
    {
        throw new IllegalArgumentException
            ("Names/ages/salaries must be the same size");
    }
    ...
}
Jon Skeet
Thanks Jon, excellent answer!
Brock Woolf
THis is a good example of what I consider the wrong way to use NullPointerException. I would make an individual check for each of the passed parameters if (employeeNames == null) { throw new IllegalArgumentException("employeeNames == null"); }This makes the stacktrace much more helpful for diagnosing the problem.
Thorbjørn Ravn Andersen
I think it's just as acceptable to throw a NPE in this case (while no one would disagree with throwing an IAE); but perhaps as a bit of a compromise in this debate/controversy it would be better to include a message with the NPE.
matt b
I used NullPointerException to follow the convention shown in Effective Java 2nd edition, item 60. To do it really properly I'd use a separate check for each argument, so I could put the right name in - but I wanted to keep the code short for this example :)
Jon Skeet
Ditto on the NullPointerException. To quote the javadoc a NullPointerException is "Thrown when an application attempts to use null in a case where an object is required." An IllegalArgumentException is "Thrown to indicate that a method has been passed an illegal or inappropriate argument."In this case, the null array is clearly an illegal argument. It is not being accessed.
Mr Jacques
+2  A: 

Since the arrays being passed in aren't generated until runtime, it is not possible to prevent the method call from completing depending upon the characteristics of the array being passed in as a compile-time check.

As Jon Skeet has mentioned, the only way to indicate a problem is to throw an IllegalArgumentException or the like at runtime to stop the processing when the method is called with the wrong parameters.

In any case, the documentation should clearly note the expectations and the "contract" for using the method -- passing in of three arrays which have the same lengths. It would probably be a good idea to note this in the Javadocs for the method.

coobird
A: 

A way to skirt around the problem is to create a builder, e.g., EmployeeArrayBuilder,

public class EmployeeArrayBuilder {
   private Integer arraySize = null;
   private String[] employeeNames;
   public EmployeeArrayBuilder addName(String[] employeeNames) {
      if (arraySize == null) {
         arraySize = employeeNames.length;
      } else if (arraySize != employeeNames.length) {
         throw new IllegalArgumentException("employeeNames needs to be " + arraySize + " in length");
      }
      this.employeeNames = employeeNames;
      return this;
   }
   public EmployeeArrayBuilder addSalaries(float[] employeeSalaries) {/* similar to above */}
   public EmployeeArrayBuilder addAges(Integer[] employeeAges) {/* similar  */}
   public Employee[] build() {
       // here, you can do what you needed to do in the constructor in question, and be sure that the members are correctly sized.
       Employee myEmployees[] = new Employee[arraySize ];// dont know what size is            
       for ( int count = 0; count < arraySize ; count++) {
            myEmployees[count] = new Employee( employeeNames[count], employeeAges[count], employeeSalaries[count] );
       }
       return myEmployees;
   }
}
Chii
I don't think a builder is appropriate here because there is nothing in the API to suggest that ages, names, and salaries are all required. Builder is appropriate when you can have optional arguments. It is also rather complex compared to Jon Skeet's answer.
Ken Liu
@Ken Liu:Both solves the same problem - i posted this because the other solution is already posted so why duplicate? and this is an alternative that warrants some consideration. Indeed there is nothing in the API to suggest these are required arguments, but that doesnt take anything away from the builder pattern.
Chii