Skip to content

Archinamon/TaskSequencer

Repository files navigation

TaskSequencer

Small, simple and powerful annotation-based wrapper will beautifully organize and handle common background runnable tasks. This library supports a pre- and post-conditions, declaring as simple methods. Tune every single task in way you need to use it and don't worry about context references by disallowing synthetic class declaration!

Designed to be useful in building and configuring Ui where always a lot of asynchronous tasks which needs a delivery point into ui-thread. Organizing as sequences will help you to hold all logic apart within only one class per chain.

Usage

First you have to add maven repo web-link to your build.gradle file:

buildscript {
    repositories {
        maven { url 'https://github.com/Archinamon/TaskSequencer/raw/master/' }
    }
}

repositories {
    maven { url 'https://github.com/Archinamon/TaskSequencer/raw/master/' }
}

And then add a dependency:

dependencies {
    compile 'com.archinamon:libsequencer:1.0'
}

That's all you need to start!

Simple sequence looks as a trivial synthetic inner class, annotated as @Sequence. All methods annotated as @SequenceTask will execute as a separate Runnable task synchronous to Ui thread group in order methods declared in class. To manipulate the order and execution (Ui or background) — simply add corresponding values to annotations.

package com.example.sequence;

import android.app.Activity;

import com.archinamon.Mode;
import com.archinamon.ISequencer;
import com.archinamon.Sequence;
import com.archinamon.SequenceBuilder;
import com.archinamon.SequenceTask;
import com.archinamon.SequenceTask.Order;
import com.archinamon.SequenceTask.Type;

import org.jetbrains.annotations.Contract;

public class MyActivity extends Activity {

    @Sequence
    final class FillUi {

        @SequenceTask
        void fillViews() {}

        @SequenceTask(Type.ASYNC)
        void extractDb() {}

        @SequenceTask(exec_order = Order.POST_COMPILE)
        void updateUi() {}

        @Contract("-> !null")
        private Object helperMethod() {return new Object();}
    }
    
    private ISequencer mSequencer;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCrate(savedInstanceState);
        
        mSequencer = new SequenceBuilder()
                .parseSequence(new FillUi())
                .build(Mode.COHERENCE);
    }
    
    protected void onResume() {
        super.onResume();
        
        mSequencer.exec();
    }
}

In this example I've used a separate class as seuence. You can use an easier way — adding to SequenceBuilder method references (or runnables if you use 1.7 and lower Java version) via SequenceBuilder.addTask methods. There're two additional methods (and parameters within @SequenceTask annotation too) to define a pre- and post-execution task order. I.e. to configurate a view or prepare some types before all other tasks will run. These two tasks have an absolute exec. order over all others.

After you fill the sequence you have to choose what mode to use to execute these tasks. The most common way is to build ISequencer with Mode.COHERENCE — in this case all tasks will be executed one-by-one, every task will await the previous. A Mode.ONEWAY will be useful if all the tasks could be executed in parallel mode.

There's an additional parameter for @Sequence annotation — boolean synthetic() default true;. This value is used to guard a class definition as inner one. If you're worry about memory usage and wanna be sure that a Sequence-class won't hold your activity against GC — declare this class as static or in a separate file and change a bit an annotation: @Sequence(synthetic = false). If you'll try to declare this sequence as an inner class without static keyword — SequenceBuilder will throw an exception.

License

Copyright 2015 Eduard "Archinamon" Matsukov.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

lightweight thread-sequencer based on annotation mapping mechanics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages