0

I am making a SDK using java where a typical method exposed to client would look like this

public void createUser(String name, String email ,int age, String bio, String address, String phoneNumber){
/* 
body
*/
}

So the problem is that only name and email fields are mandatory (not null or empty) and the rest are just optional (null values are allowed). Other than overriding and throwing a run time exception inside the method, Is there any way to properly indicate to the client calling the sdk function that the name and email fields are mandatory ?

curiousredoC
  • 51
  • 1
  • 7
  • You can write another version of the method with "public void createUser(String name, String email) { ... } – NomadMaker Apr 07 '20 at 07:56
  • Please look at this SO question https://stackoverflow.com/questions/4963300/which-notnull-java-annotation-should-i-use – rpc1 Apr 07 '20 at 07:59

3 Answers3

1

A widely used library for that is Project Lombok and the provided annotation @NonNull. Your method would look as follows.

public void createUser(@NonNull String name, @NonNull String email ,int age, String bio, String address, String phoneNumber){
/* 
body
*/
}

If the method is called with null values for those fields a compilation error will occur.

FYI: For your case I would consider using the Builder Design Pattern as not all of your parameters are mandatory. This will make your "SDK" nicer to use.

akuzminykh
  • 4,522
  • 4
  • 15
  • 36
1

Java unfortunately doesn't support optional parameters. One solution to this might be to use a Builder Pattern. You create a separate UserBuilder class, which only takes the required arguments in its constructor. Other arguments are passed through set functions. This also allows you to give some of the parameters default values. When all the parameters are set, you call UserBuilder.getUser() and your User object is created for you. Typically you also make sure the User constructor is not available elsewhere.

class User{

    String name, email, bio, address, phoneNumber;
    int age;

    User(String name, String email, int age, String bio, String address, String phoneNumber){
        this.name = name;
        this.email = email;
        this.age = age;
        this.bio = bio;
        this.address = address;
        this.phoneNumber = phoneNumber;
    }
}
class UserBuilder{
    String name, email, bio, address, phoneNumber;
    int age;
    public UserBuilder(String name, String email){
        this.name = name;
        this.email = email;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public User getUser(){
        return new User(name, email,age,bio,address,phoneNumber);
    }
}

For example, this is how you would create a User object with the builder class if you only wanted to set name, age, and email:

UserBuilder builder = new UserBuilder(name,email);
builder.setAge(38);
builder.getUser();
Dejke
  • 168
  • 1
  • 9
0

You can add @NonNull annotation before name and e-mail id.

@NonNull – The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException.

Datta Diware
  • 602
  • 1
  • 5
  • 16