Scheduler example

Category.java
package com.sandbox.scheduler.model;


public enum Category {

    RED(1),
    GREEN(2),
    BLUE(3);

    private int value;

    public int getValue() {
        return value;
    }

    private Category(int value) {
        this.value = value;
    }
}

Task.java
package com.sandbox.scheduler.model;

import java.time.LocalDateTime;
import java.util.UUID;
import com.sandbox.scheduler.model.Category;


public class Task {

    public int urgency = -1;
    public Category category;
    public LocalDateTime timestamp = LocalDateTime.now();
    public UUID uuid = UUID.randomUUID();

    public Task(int urgency, Category category) {
        this.urgency = urgency;
        this.category = category;
    }

    public String toString() {

       return "[TASK] UUID: " + uuid.toString() + " URGENCY: " + urgency + " CATEGORY: " + category +" TIMESTAMP: " + timestamp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Task task = (Task) o;

        if (urgency != task.urgency) return false;
        if (!category.toString().equals(task.category.toString())) return false;
        if (timestamp != null ? !timestamp.equals(task.timestamp) : task.timestamp != null) return false;
        return uuid != null ? uuid.equals(task.uuid) : task.uuid == null;
    }

    @Override
    public int hashCode() {
        int result = urgency;
        result = 31 * result + (category != null ? category.toString().hashCode() : 0);
        result = 31 * result + (timestamp != null ? timestamp.hashCode() : 0);
        result = 31 * result + (uuid != null ? uuid.hashCode() : 0);
        return result;
    }
}

App.java
package com.sandbox.scheduler;

import com.sandbox.scheduler.model.Task;
import com.sandbox.scheduler.model.Category;

import java.util.Arrays;
import java.util.List;


public class App {

    public static void main(String[] args) {
        Task task1 = new Task(0, Category.RED);
        Task task2 = new Task(0, Category.BLUE);
        Task task3 = new Task(3, Category.GREEN);
        Task task4 = new Task(4, Category.GREEN);
        Task task5 = new Task(5, Category.RED);
        Task task6 = new Task(0, Category.RED);

        List<Task> unsorted = Arrays.asList(task1, task2, task3, task4, task5, task6);

        unsorted.forEach(t -> Scheduler.scheduleTask(t));

        //Scheduler.printSchedule();
    }
}

Scheduler.java
package com.sandbox.scheduler;

import com.sandbox.scheduler.model.Task;

public class Scheduler{

                              public static void scheduleTask(Task t){
                                            
                                             System.out.println("Task :"+t);
                                            
                              }
              

}

Comments

Popular posts from this blog

OOPS Concepts with realtime examples

What is an interface?

Asynchronous Messaging