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

org.opentripplanner.netex.index.hierarchy.ValidOnDate Maven / Gradle / Ivy

package org.opentripplanner.netex.index.hierarchy;


import static org.opentripplanner.netex.support.NetexVersionHelper.firstValidDateTime;
import static org.opentripplanner.netex.support.NetexVersionHelper.versionOf;

import java.time.LocalDateTime;
import org.rutebanken.netex.model.EntityInVersionStructure;


/**
 * Wrapper class to simplify getting the correct version of a Netex versioned
 * entity.
 */
class ValidOnDate {
    private final T entity;
    private final LocalDateTime time;

    /**
     * Wrap the given {@code entity} and find the first point in time the
     * entity is valid after the given {@code timeLimit}.
     */
    ValidOnDate(T entity, LocalDateTime timeLimit) {
        this.entity = entity;
        this.time = firstValidDateTime(entity.getValidBetween(), timeLimit);
    }

    T entity() {
        return entity;
    }

    boolean isValid() { return time != null; }

    /**
     * Return true if this is a better version of the entity based on the time limit passed in
     * on the constructor and, in case of a tie the version number is used.
     */
    boolean bestVersion(ValidOnDate other) {
        if(time.isBefore(other.time)) { return true; }
        if(other.time.isBefore(time)) { return false; }
        return versionOf(entity) > versionOf(other.entity);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy