0

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");
        
    }
}
amiram
  • 1
  • 2
  • 1
    are there packages for validation? most likely, and yes. do they mean you don't have to provide anything yourself? no. either you add a number of validations, or you just want to add one. understand that your validations are based on your requirements, and those creating those libraries didn't know of/targeted your requirements. – Stultuske Nov 18 '20 at 11:49
  • use `@Nonnull` annotation for your input parameters – Jude Niroshan Nov 18 '20 at 11:52
  • @JudeNiroshan well that can help with documentation, but does not by itself prevent runtime exceptions. Some static analyzers can use it to identify many (but not all) problematic situations. – Hulk Nov 18 '20 at 11:56
  • Be careful with some of your validations that you don't invalidate real names. For example, the city of "West Chester" would fail and yet be valid input. – NomadMaker Nov 18 '20 at 12:00

0 Answers0