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

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

There is a newer version: 3.0.22
Show newest version
package net.fortuna.ical4j.model;

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

import java.util.Comparator;
import java.util.Optional;

/**
 * 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 = Optional.ofNullable((Sequence) o1.getProperty(Property.SEQUENCE)).orElse(defaultSequence);
        Sequence sequence2 = Optional.ofNullable((Sequence) o2.getProperty(Property.SEQUENCE)).orElse(defaultSequence);

        retVal = sequence1.compareTo(sequence2);
        if (retVal == 0) {
            DtStamp defaultDtStamp = new DtStamp(new DateTime(0));
            DtStamp dtStamp1 = Optional.ofNullable((DtStamp) o1.getProperty(Property.DTSTAMP)).orElse(defaultDtStamp);
            DtStamp dtStamp2 = Optional.ofNullable((DtStamp) o2.getProperty(Property.DTSTAMP)).orElse(defaultDtStamp);

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy