amiram
amiram

Reputation: 1

is exist the standard way to check valid input of constructor

i have try to write java class and i also i created constructor and i try to create unit test for class and also for check valid input of constructor but for valid input of constructor i do check multiple situation of fields for validation for example check not null and not blank for string and regex also and because my class is immutable for check every situation of validation we should new a object of class my question is there is no problem if i write a static method that package-access and then in constructor i invoked them ??

anyway thank you for your help ...

public class Address {
    private final String street;
    private final String city;
    private final String pin;//postal index number.

    public Address(String street, String city, String pin) {
        if (street.isBlank())
            throw new IllegalArgumentException("the street input cannot be blank");
        if (city.isBlank())
            throw new IllegalArgumentException("the city input cannot be blank");
        if (pin.isBlank())
            throw new IllegalArgumentException("the pin input cannot be blank");
        if (!street.matches("^\\w(?:\\w+\\-?)\\w+$"))
            throw new IllegalArgumentException("the street input must be character number and -");
        if (!city.matches("^[a-zA-Z]{2,20}$"))
            throw new IllegalArgumentException("the city name must be character and length between 2 and 20");
        if (!pin.matches("^\\d{5}\\-?\\d{5}$"))
            throw new IllegalArgumentException("the pin must be digit and can be a - between 5'th and 6'th of character");
        this.street = Objects.requireNonNull(street,"the street cannot be null");
        this.city = Objects.requireNonNull(city,"the city cannot be null");
        this.pin = Objects.requireNonNull(pin,"the pin cannot be null");
    }
}
public class Address {
    private final String street;
    private final String city;
    private final String pin;//postal index number.

    public Address(String street, String city, String pin) {
        checkValidInput(street,city,pin);
        this.street = Objects.requireNonNull(street,"the street cannot be null");
        this.city = Objects.requireNonNull(city,"the city cannot be null");
        this.pin = Objects.requireNonNull(pin,"the pin cannot be null");
    }
    
    static void checkValidInput(String street,String city,String pin){
        Objects.requireNonNull(street,"the street cannot be null");
        Objects.requireNonNull(city,"the city cannot be null");
        Objects.requireNonNull(pin,"the pin cannot be null");
        
        if (street.isBlank())
            throw new IllegalArgumentException("the street input cannot be blank");
        if (city.isBlank())
            throw new IllegalArgumentException("the city input cannot be blank");
        if (pin.isBlank())
            throw new IllegalArgumentException("the pin input cannot be blank");
        if (!street.matches("^\\w(?:\\w+\\-?)\\w+$"))
            throw new IllegalArgumentException("the street input must be character number and -");
        if (!city.matches("^[a-zA-Z]{2,20}$"))
            throw new IllegalArgumentException("the city name must be character and length between 2 and 20");
        if (!pin.matches("^\\d{5}\\-?\\d{5}$"))
            throw new IllegalArgumentException("the pin must be digit and can be a - between 5'th and 6'th of character");
        
    }
}

Upvotes: 0

Views: 209

Answers (0)

Related Questions