Skip to content
/ flowly Public

Library for transforming model by flow of scheduled actions.

License

Notifications You must be signed in to change notification settings

krix38/flowly

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flowly

Build Status Coverage Status

Library for designing and running synchronous flow of task executions on provided model.

Prerequisites

-Java 8

-Maven

Installation

Inside cloned/downloaded repo run mvn clean install and include dependency in your pom project

    <dependency>
      <groupId>com.github.krix38</groupId>
      <artifactId>flowly</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

Usage

Flowly lets you create and schedule executions (actions) on given input model.

To make it work, yout input model has to extend AbstractModel class.

To create action you want to schedule, define class with method annoted by @FlowAction annotation. This method has to take one argument with model, which can be any object extending AbstractModel.

Action can be scheduled to run by FlowRegister.register(Object). Registered actions are later run in order of register(Object) calls.

FlowRegister.register(Object) can take reference to existing action object or class of action object (in which case such class is instantiated before running @FlowAction method so such class needs to provide default constructor)

To run scheduled actions, call FlowRegister.run(AbstractModel) on your model class.

Example

Account.java (model)

public abstract class Account extends AbstractModel {
    private String username;

    public Account(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }
}

FreeAccount.java (model)

public class FreeAccount extends Account {
    public FreeAccount(String username, Integer daysLeft) {
        super(username);
        this.daysLeft = daysLeft;
    }
    private Integer daysLeft;

    public Integer getDaysLeft() {
        return daysLeft;
    }
}

PremiumAccount.java (model)

public class PremiumAccount extends Account{

    public PremiumAccount(String username, Long availableStorage) {
        super(username);
        this.setAvailableStorage(availableStorage);
    }

    private Long availableStorage;

    public void setAvailableStorage(Long availableStorage) {
        this.availableStorage = availableStorage;
    }

    public Long getAvailableStorage() {
        return availableStorage;
    }
}

AddStorage.java (action)

public class AddStorage {

    private Long storageToAdd;

    public AddStorage(Long storageToAdd) {
        this.storageToAdd = storageToAdd;
    }

    @FlowAction
    public void add(PremiumAccount premiumAccount){
        premiumAccount.setAvailableStorage(premiumAccount.getAvailableStorage() + storageToAdd);
    }
}

MapFreeToPremiumAccount.java (action)

public class MapFreeToPremiumAccount {

    private static final Long DEFAULT_AVAILABLE_STORAGE = 100L;

    @FlowAction
    public PremiumAccount map(FreeAccount freeAccount){
        return new PremiumAccount(freeAccount.getUsername(), DEFAULT_AVAILABLE_STORAGE);
    }
}

UpdateToPremiumService.java

public class UpdateToPremiumService {
    private FlowRegister flowRegister;

    public UpdateToPremiumService() {
        flowRegister = new FlowRegister();
        flowRegister.register(new MapFreeToPremiumAccount());  //can pass MapFreeToPremiumAccount.class as well
        flowRegister.register(new AddStorage(200L));
    }

    public List<PremiumAccount> updateToPremium(List<FreeAccount> freeAccounts) throws ActionExecutionException {
        return flowRegister.runForAll(freeAccounts)
                .stream()
                .filter(account -> !account.hasFailed())
                .map(account -> (PremiumAccount)account)
                .collect(Collectors.toList());
    }
}

For more examples take a look at unit tests

About

Library for transforming model by flow of scheduled actions.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages