Skip to content

Share parameter validation between methods and callers.

Notifications You must be signed in to change notification settings

sedstrom/Bouncer

Repository files navigation

Bouncer

Share parameter validation between method and caller.

Disclaimer

This library is in a very early stage and will probably change a lot in the nearest future :)

Usage

Provide methods through bouncer

    // Actual method is private
    private Void register(RegistrationParams params) {
        params.getListener().onRegistrationSuccessful(params);
        return null;
    }
   
   // Provide through bouncer
   public Bouncer<RegistrationParams, Void> register() {
        return new Bouncer<RegistrationParams, Void>() {
            @Override
            protected Void welcome(RegistrationParams params) {
                return register(params);
            }
        };
    }

Define paramter validation:

public class LoginParams extends Params {
    
    ...

     public RegistrationParams(Param<String> username, Param<String> password, Param<RegistrationListener> listener) {
        super();
        
         addValidator(new EmailAddressValidator(username, "Username needs to be an email address."));
        
        ...
    }
}

Pass validation before getting access to actual method

  RegistrationParams params = createRegistrationParams(username.text().toString(), password.text().toString());
  CheckResult<Void> result = api.register().check(params);
  if(result.isOk()) {
    result.call(); // Calls encapsulated method
  } else {
    // Buhuu..
  }

Get validation result per paramter

 private RegistrationParams createRegistrationParams(String username, String password) {
        return new RegistrationParams(
                new Param<String>(username) {

                    @Override
                    public void onValidationPassed() {
                        usernameTitle.setText("");
                        usernameTitle.setTextColor(Color.GREEN);
                    }

                    @Override
                    public void onValidationFailed(String reason) {
                        usernameTitle.setText(reason);
                        usernameTitle.setTextColor(Color.RED);
                    }
                },
                ...
        );
    }

About

Share parameter validation between methods and callers.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages