Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StateGuard #3

Closed
Adeynack opened this issue Dec 19, 2017 · 0 comments · Fixed by #11
Closed

StateGuard #3

Adeynack opened this issue Dec 19, 2017 · 0 comments · Fixed by #11
Assignees
Labels
Projects

Comments

@Adeynack
Copy link
Owner

Adeynack commented Dec 19, 2017

Idea

Ensures that state mutation is thread safe by holding the state value itself internally and making it available only when a task is dispatched to it.

This works better if the state is an immutable class using immutable collections, for total encapsulation.

To be done AFTER

Main features

Those are main ideas and not final interfaces.

mutate

User submits a lambda receiving the actual state and returning the new value of the state. The method itself returns a CompletionStage<Void> to allow to caller to react upon the completion of the submitted task.

public CompletionStage<Void> addUser(User user) {
    return stateGuard.mutate(actual -> {
        log.info("Adding user " + user.getName());
        List<User> updatedUserList = new ArrayList<User>(actual.getUserList());
        updatedUserList.add(user);
        UserServiceState newState = new UserServiceState(updatedUserList);
        return newState;
    });    
}

provide

User submits a lambda receiving the actual state and returning a value to pass down to the caller of the method.

public CompletionStage<List<String>> getUserNames() {
    return stateGuard.provide((actual) -> {
       new ArrayList(actual.getUserList().stream().map(u -> u.getName())
    });    
}

mutateAndProvide

User submits a lambda receiving the actual state and returning both the value to return as a result of the operation and the new value of the state.

return stateGuard.mutateAndProvide(actual -> {
  log.info("Adding user " + user.getName());
  
  List<User> updatedUserList = new ArrayList<User>(actual.getUserList());
  updatedUserList.add(user);
  UserServiceState newState = new UserServiceState(updatedUserList);

  List<String> userNames = new ArrayList(actual.getUserList().stream().map(u -> u.getName());

  return new MutateAndProvideDecision(newState, userNames);
});
This was referenced Dec 19, 2017
@Adeynack Adeynack added this to Grooming in Main Dec 19, 2017
@Adeynack Adeynack moved this from Grooming to In progress in Main Dec 20, 2017
@Adeynack Adeynack self-assigned this Dec 20, 2017
@Adeynack Adeynack changed the title StateGuard - mutate StateGuard Dec 22, 2017
Adeynack pushed a commit that referenced this issue Dec 27, 2017
+ better tests for SequencedExecutor
@Adeynack Adeynack mentioned this issue Dec 27, 2017
Adeynack pushed a commit that referenced this issue Dec 27, 2017
Adeynack pushed a commit that referenced this issue Dec 27, 2017
Adeynack pushed a commit that referenced this issue Dec 27, 2017
@Adeynack Adeynack moved this from In progress to In review in Main Jan 1, 2018
Main automation moved this from In review to Done Jan 18, 2018
Adeynack added a commit that referenced this issue Jan 18, 2018
* Create `SequencedExecutor`.

* #3 implement StateGuard's first method 'mutate'

* #8 configure `error-prone` and apply its suggestions

* #1 add tests

* #1 nice catch, M. Error-Prone!

* #1 more strict Java compiling rules + consider warnings as errors

* #1 use @GuardedBy for better error-prone coverage.

* #3 complete StateGuard + tests
+ better tests for SequencedExecutor

* #3 clean up

* #8 avoid raw types (thanks error-prone)

* #3 add failure tests

* #3 add CODEOWNERS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Main
  
Done
Development

Successfully merging a pull request may close this issue.

1 participant