All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.path.android.jobqueue.nonPersistentQueue.TimeAwareComparator Maven / Gradle / Ivy

Go to download

a Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.

There is a newer version: 1.1.2
Show newest version
package com.path.android.jobqueue.nonPersistentQueue;

import com.path.android.jobqueue.JobHolder;

import java.util.Comparator;

/**
 * A real-time comparator class that checks current time to decide of both jobs are valid or not.
 * Return values from this comparator are inconsistent as time may change.
 */
public class TimeAwareComparator implements Comparator {
    final Comparator baseComparator;

    public TimeAwareComparator(Comparator baseComparator) {
        this.baseComparator = baseComparator;
    }

    @Override
    public int compare(JobHolder jobHolder, JobHolder jobHolder2) {
        long now = System.nanoTime();
        boolean job1Valid = jobHolder.getDelayUntilNs() <= now;
        boolean job2Valid = jobHolder2.getDelayUntilNs() <= now;
        if(job1Valid) {
            return job2Valid ? baseComparator.compare(jobHolder, jobHolder2) : -1;
        }
        if(job2Valid) {
            return job1Valid ? baseComparator.compare(jobHolder, jobHolder2) : 1;
        }
        //if both jobs are invalid, return the job that can run earlier. if the want to run at the same time, use base
        //comparison
        if(jobHolder.getDelayUntilNs() < jobHolder2.getDelayUntilNs()) {
            return -1;
        } else if(jobHolder.getDelayUntilNs() > jobHolder2.getDelayUntilNs()) {
            return 1;
        }
        return baseComparator.compare(jobHolder, jobHolder2);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy