8

I want to force the user to fill in an optional parameter when calling my constructor:

public MyClass(String... params) {
    this.params = params;
}

Currently, the following code is valid:

new MyClass();

I want to prevent it. I thought of this:

public MyClass(String param1, String... otherParams) {
    this.params = new String[1 + otherParams.length];
    this.params[0] = param1;
    // fill 1..N params from otherParams
}

String[] params is not an option, because I need the ability to use comma separated args.

Are there any nice solutions to achieve this? Please don't say that varargs parameters must be optional. The question is not about that.

psmears
  • 26,070
  • 4
  • 40
  • 48
  • otherParams could be null if someone writes ´new MyClass("a", null)´ – wero Jul 13 '15 at 11:30
  • 1
    Could you just throw an exception if the number of arguments you expect isn't there? – Pherion Jul 13 '15 at 11:30
  • 3
    Maybe check [this](http://stackoverflow.com/questions/12751746/requiring-at-least-one-element-in-java-variable-argument-list) solution? – SomeJavaGuy Jul 13 '15 at 11:30

4 Answers4

10

Maybe the following, an extra default constructor?

/**
 * Please provide at least one parameter.
 */
@Deprecated
public MyClass() {
    throw new IllegalStateException("Please provide at least one parameter");
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
7

No, there's no other way to do this at compile-time. What you found is the usual pattern of forcing the client code to pass at least one parameter.

You can read it like this:

// You can pass in some parameters if you want
public MyClass(String... params)

and

// I require one parameter, you can pass some additional if you want
public MyClass(String param1, String... otherParams)
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
2

If you want do it at compile time you need it to do as you suggested in your last code example.

Then you can use the ApacheCommons - ArrayUtils class:

String[] allParams = ArrayUtils.add(otherParams, 0, param1); //Insert the first param at the first position of the array.
Denis Lukenich
  • 3,084
  • 1
  • 20
  • 38
1

Throw a IllegalArgumentException if the caller did not supply a non-empty parameter array.

EDIT: paraphrased the original text as answer

wero
  • 32,544
  • 3
  • 59
  • 84
  • 3
    Because it is much better to check such conditions during compilation. Btw Questions -> Comment. – Tom Jul 13 '15 at 11:34
  • ...for the price of a temporary array creation. btw the comment was added at the same time as my answer – wero Jul 13 '15 at 11:38
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – JNYRanger Jul 13 '15 at 12:54
  • @JNYRanger: formally it is a comment, but actually it is a solution to the question how to force a user to provide a non-empty var-arg parameter. – wero Jul 13 '15 at 12:57
  • 1
    @wero I'd recommend that you'd reword it then to sound more like an answer than a comment & explain the reasoning behind it. – JNYRanger Jul 13 '15 at 13:01