Skip to content

About

Base object classes for any type of applications in Java platform.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

242 Commits

Folders and files

Repository files navigation

JFoundationKit

SetUp

   Step 1. Add the JitPack repository to your build file
    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>
    
    Step 2. Add the dependency
    <dependency>
        <groupId>com.github.itsoulltd</groupId>
        <artifactId>JFoundationKit</artifactId>
        <version>latest</version>
    </dependency>

Programming philosophy behind:

    1. DRY (Don't repeat yourself and Put shared logic in one place).
    2. The Single Responsibility Principle (SRP) (Keep each component focused on one responsibility).
    3. Composition over inheritance (*More flexible; *Easier to modify behavior at runtime; *Reduces tight coupling between classes).

How To Use API:

    ###Let's know about Message.java:
    ###Message confirms to iMessage, Externalizable, Comparable<Message>
    Message message = new Message();
    message.setPayload("My name");

    String str1 = message.toString();
    String str2 = MessageMapper.getJsonSerializer().writeValueAsString(message);
    System.out.println(str1 + " == " + str2);
    
    ###Now recreate Message from Json:
    String remoteJson = message.toString();
    Message myRemoteMessage = MessageMapper.unmarshal(Message.class, remoteJson);
    System.out.println("Both Custom Message is same: " + ( myRemoteMessage.getPayload().equals(message.getPayload()) ? "YES" : "NO" ));
    
    ###Let's know about Response.java:
    ###Response is derived from Message.java
    Response response = new Response().setStatus(200).setMessage("Successful Transmission");
    System.out.println("Response was: " + response.toString());
    
    ###Let's know about PagingQuery.java & SearchQuery.java: [Moved to JSqlKit](https://github.com/itsoulltd/JSqlKit)
    SearchQuery query = Pagination.of(SearchQuery.class
            , 0
            , 10
            , SortOrder.ASC
            , "CLUSTER_NAME","REGION_NAME", "AM_NAME");

    query.add("ROLE_NAME").isEqualTo("Gittu")
            .or("PERSON_MOBILE").isEqualTo("01712645571")
            .and("AGE").isGreaterThen(32);

    System.out.println("Newly-Created: " + query.toString());
    ###Output:
    {
        "page":0,"size":10,
        "descriptors":[{"order":"ASC","keys":["CLUSTER_NAME","REGION_NAME","AM_NAME"]}],
        "properties":[
            {"key":"ROLE_NAME","value":"Gittu","operator":"EQUAL","type":"STRING","nextKey":"PERSON_MOBILE","logic":"OR"},
            {"key":"PERSON_MOBILE","value":"01712645571","operator":"EQUAL","type":"STRING","nextKey":"AGE","logic":"AND"},
            {"key":"AGE","value":"32","operator":"GREATER_THAN","type":"INT"}]
    }
    
    ###Now Assume we have a Json String: (carrying over Http Request @Body)
    String json = "{\"page\":0,\"size\":10,\"descriptors\":[{\"order\":\"ASC\",\"keys\":[\"CLUSTER_NAME\",\"REGION_NAME\",\"AM_NAME\"]}],\"properties\":[{\"key\":\"ROLE_NAME\",\"value\":\"Gittu\",\"operator\":\"EQUAL\",\"type\":\"STRING\",\"nextKey\":\"PERSON_MOBILE\",\"logic\":\"OR\"},{\"key\":\"PERSON_MOBILE\",\"value\":\"01712645571\",\"operator\":\"EQUAL\",\"type\":\"STRING\",\"nextKey\":\"AGE\",\"logic\":\"AND\"},{\"key\":\"AGE\",\"value\":\"32\",\"operator\":\"GREATER_THAN\",\"type\":\"INT\"}]}\n";
    SearchQuery recreated = MessageMapper.unmarshal(SearchQuery.class, json);
    System.out.println("Re-Created: " + recreated.toString());
    ###Output:
    {
        "page":0,"size":10,
        "descriptors":[{"order":"ASC","keys":["CLUSTER_NAME","REGION_NAME","AM_NAME"]}],
        "properties":[
            {"key":"ROLE_NAME","value":"Gittu","operator":"EQUAL","type":"STRING","nextKey":"PERSON_MOBILE","logic":"OR"},
            {"key":"PERSON_MOBILE","value":"01712645571","operator":"EQUAL","type":"STRING","nextKey":"AGE","logic":"AND"},
            {"key":"AGE","value":"32","operator":"GREATER_THAN","type":"INT"}]
    }

Task & Task-Runtime:

How to define a Task:

    public class ExampleTask extends AbstractTask<Message, Response> {
    
        //Either override default constructor:
        public ExampleTask() {super();}
        //OR
        //Provide an custom constructor:
        public ExampleTask(String data) {
            super(new Property("data", data));
        }

        @Override
        public Response execute(Message message) throws RuntimeException {
            String savedData = getPropertyValue("data").toString();
            //....
            //....
            return new Response().setMessage(savedData).setStatus(200);
        }

        @Override
        public Response abort(Message message) throws RuntimeException {
            String reason = message != null ? message.getPayload() : "UnknownError!";
            return new Response().setMessage(reason).setStatus(500);
        }
    }

Create and Running Task Using TaskStack:

    ###Defining a TaskStack:
    private TaskStack stack = new TransactionStack();
    
    stack.push(new SimpleTask("Wow bro! I am Adams"));
    
    stack.push(new SimpleTask("Hello bro! I am Hayes"));
    
    stack.push(new SimpleTask("Hi there! I am Cris", (message) -> {
        Event event = message.getEvent(Event.class);
        event.setMessage("Converted Message");
        event.setStatus(201);
        message.setEvent(event);
        return message;
    }));
    
    stack.push(new SimpleTask("Let's bro! I am James"));
    
    stack.commit(false, (result, state) -> {
        System.out.println("State: " + state.name());
        System.out.println(result.toString());
        latch.countDown();
    });
    
    ### Output:
    Doing jobs...Let's bro! I am James
    Doing jobs...Hi there! I am Cris
    Doing jobs...Hello bro! I am Hayes
    {"message":"Converted Message","status":201}
    Doing jobs...Wow bro! I am Adams
    State: Finished
    {"payload":"{\"message\":\"Converted Message\",\"status\":201}","status":200}
    
    ### Doing Abort
    
    stack.push(new SimpleTask("Wow bro! I am Adams"));
    
    stack.push(new AbortTask("Hello bro! I am Hayes"));
    
    stack.push(new SimpleTask("Hi there! I am Cris"));
    
    stack.push(new SimpleTask("Let's bro! I am James"));
    
    stack.commit(false, (result, state) -> {
        System.out.println("State: " + state.name());
        System.out.println(result.toString());
        latch.countDown();
    });
    
    ### Output:
    Doing jobs...Let's bro! I am James
    Doing jobs...Hi there! I am Cris
    Doing revert ...:Hello bro! I am Hayes
    Doing revert ...:Hi there! I am Cris
    Doing revert ...:Let's bro! I am James
    State: Failed
    {"payload":"{\"status\":500,\"error\":\"I AM Aborting! Critical Error @ (Hello bro! I am Hayes)\"}","status":502}

Make a Registration Task Flow:

    TaskStack regStack = new TransactionStack();
    
    regStack.push(new CheckUserExistTask("ahmed@yahoo.com"));
    
    regStack.push(new RegistrationTask("ahmed@yahoo.com"
            , "5467123879"
            , "ahmed@yahoo.com"
            , "0101991246"
            , new Date()
            , 32));
            
    regStack.push(new SendEmailTask("xbox-support@msn.com"
            , "ahmed@yahoo.com"
            , "Hi There! .... Greetings"
            , "new-reg-email-temp-01"));
            
    regStack.push(new SendSMSTask("01100909001"
            , "01786987908"
            , "Your Registration Completed! Plz check your email."
            , "new-reg-sms-temp-01"));
            
    regStack.commit(true, (message, state) -> {
        System.out.println("Registration Status: " + state.name());
    });
iDataSource & iDataStore Api:
    /**
    * SimpleDataSource is a concreate impl of iDataSource interface.
    */
    //Create an instance of iDataSource from a concreate impl of SimpleDataSource.java
    SimpleDataSource<String, Object> dataSource = new SimpleDataSource<>();
    
    //API: Create and Insert:
    Person person = new Person().setName("John")
                        .setEmail("john@gmail.com").setAge(36)
                        .setGender("male");
    dataSource.put("id-001", person);
    
    person = new Person().setName("Adam")
                         .setEmail("adam@gmail.com").setAge(31)
                         .setGender("male");
    dataSource.put("id-002", person);
    
    //API: Read by Key
    Person found = dataSource.read("id-001");
    
    //API: Paginated read-sync and Convert:
    int maxItem = dataSource.size();
    Object[] items = dataSource.readSync(0, maxItem);
    List<Person> converted = Stream.of(items).map(itm -> (Person) itm).collect(Collectors.toList());
    converted.forEach(person -> System.out.println(person.toString()));
    
    //API: Remove
    Person removed = dataSource.remove("id-002");
    
    //API: Replace
    Person replaced = dataSource.replace("id-002", new Person()...);
Page Vs Offset:
    //Page Vs Offset: When limit/size is given
    public int getOffset(int page, int limit) {
         if (limit <= 0) limit = 10;
         if (page <= 0) page = 1;
         int offset = (page - 1) * limit;
         return offset;
    }
    
    //E.g. Usually in rest-api get-method, page variable being passed with starting value from 1;
    //Where as in database sql-context, we write select query with offset with startting value from 0;
    //So, usually we have to translate page into offset or vice-versa.
    int page = 2;
    int limit = 10;
    int offset = getOffset(page, limit);
    
    //Test Results:
    When (limit:10 & page:2)   Offset: 10
    When (limit:10 & page:-1)  Offset: 0
    When (limit:10 & page:7)   Offset: 60
    When (limit:10 & page:101) Offset: 1000
    When (limit:15 & page:2)   Offset: 15
    When (limit:15 & page:-1)  Offset: 0
    When (limit:20 & page:7)   Offset: 120
    When (limit:-1 & page:-1)  Offset: 0
To know more about Task & TaskStack & TaskQueue, visit test classes. Thank you!

screenshot screenshot screenshot screenshot

Please Contact for extended support.

  email@ m.towhid.islam@gmail.com
  call@  DE(+4917623596979) BD(+8801712645571)
  Available for Hiring (full-time or contractual)
Tech-Experience:
  Spring-5.0, Spring-CoreReactor, SpringBoot 2.0, Redis, ActiveMQ, Cassandra & Kafka, 
  Mysql/PostgresQL/Aws-RDS 
Apps Development:
  iOS, Android, Vaadin-8,10,14
AWS Solution Architect Associate Level (Practitioner)
  EC2, S3, RDS, HA-Architecture, CloudFormation, Multi AG Replication, VPC-Config, Security Analysis, IAM/SecurityGroup/NACL.
Orchestration:
  Docker, Docker-SWARM, kubernetes
Tools:
  Eclipse, IntelliJ Idea, Xcode, AndroidStudio
Language:
  Java, Swift, Objective-C, C/C++, Android-Java, Scala, Kotlin, Phython, Node-JS, JS       

About

Base object classes for any type of applications in Java platform.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages