class Constraint {

    @SuppressWarnings("DuplicateStringLiteralInspection")
    public static Constraint parse(String input) throws IllegalArgumentException {
        if (!CONSTRAINT_REGEX.test(input)) {
            throw new IllegalArgumentException("Invalid constraint: " + input);
        }
        MatchResult result = CONSTRAINT_REGEX.exec(input);
        if (result.getGroupCount() != 5) {
            throw new IllegalArgumentException("Invalid constraint: " + input);
        }
        return new Constraint(AddressTemplate.of(result.getGroup(2)), result.getGroup(4),
                Target.parse(result.getGroup(3)), Permission.valueOf(result.getGroup(1).toUpperCase()));
    }

    private  Constraint(AddressTemplate template, String name, Target target, Permission permission);

}