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

org.opentripplanner.model.impl.GenerateMissingIds Maven / Gradle / Ivy

package org.opentripplanner.model.impl;

import org.opentripplanner.model.TransitEntity;

import java.util.List;

/**
 * Utility class to help inserting none existing ids.
 * @see #generateNoneExistentIds(List)
 */
class GenerateMissingIds {

    /** private to prevent instantiation of utility class */
    private GenerateMissingIds() {}

    /**
     * Iterate over the entities; First to find the larged numerical id, then
     * set all ids that is {@code 0} or {@code null} to a uniq nuberical id.
     */
    static > void generateNoneExistentIds(List entities) {
        int maxId = 0;

        for (T it : entities) {
            maxId = Math.max(maxId, parseId(it));
        }

        for (T it : entities) {
            if(parseId(it) == 0) {
                it.setId(Integer.toString(++maxId));
            }
        }
    }

    /** pares entity id as int, if not int -1 is resturned, if 0 or null zero is returned. */
    private static int parseId(TransitEntity entity) {
        try {
            String id = entity.getId();
            return id == null ? 0 : Integer.parseInt(id);
        }
        catch (NumberFormatException ignore) {
            return -1;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy