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

net.fortuna.ical4j.model.ComponentSequenceComparator Maven / Gradle / Ivy

package net.fortuna.ical4j.model;

import net.fortuna.ical4j.model.property.DtStamp;
import net.fortuna.ical4j.model.property.Sequence;

import java.time.Instant;
import java.util.Comparator;

/**
 * A comparator to determine natural ordering of component instances based on
 * sequence information.
 *
 * See RFC5446 - Message Sequencing
 * for further details.
 */
public class ComponentSequenceComparator implements Comparator {

    @Override
    public int compare(Component o1, Component o2) {
        int retVal = 0;

        Sequence defaultSequence = new Sequence(0);
        Sequence sequence1 = (Sequence) o1.getProperties().getFirst(Property.SEQUENCE).orElse(defaultSequence);
        Sequence sequence2 = (Sequence) o2.getProperties().getFirst(Property.SEQUENCE).orElse(defaultSequence);

        retVal = sequence1.compareTo(sequence2);
        if (retVal == 0) {
            DtStamp defaultDtStamp = new DtStamp(Instant.EPOCH);
            DtStamp dtStamp1 = (DtStamp) o1.getProperties().getFirst(Property.DTSTAMP).orElse(defaultDtStamp);
            DtStamp dtStamp2 = (DtStamp) o2.getProperties().getFirst(Property.DTSTAMP).orElse(defaultDtStamp);

            retVal = dtStamp1.compareTo(dtStamp2);
        }
        return retVal;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy