001/* *********************************************************************** *
002 * project: org.matsim.*
003 *                                                                         *
004 * *********************************************************************** *
005 *                                                                         *
006 * copyright       : (C) 2016 by the members listed in the COPYING,        *
007 *                   LICENSE and WARRANTY file.                            *
008 * email           : info at matsim dot org                                *
009 *                                                                         *
010 * *********************************************************************** *
011 *                                                                         *
012 *   This program is free software; you can redistribute it and/or modify  *
013 *   it under the terms of the GNU General Public License as published by  *
014 *   the Free Software Foundation; either version 2 of the License, or     *
015 *   (at your option) any later version.                                   *
016 *   See also COPYING, LICENSE and WARRANTY file                           *
017 *                                                                         *
018 * *********************************************************************** */
019
020package org.matsim.contrib.taxi.util.stats;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
027
028public class TaxiStatsCalculators {
029        public static final String DAILY_STATS_ID = "daily";
030
031        public static int calcHourCount(Iterable<? extends DvrpVehicle> vehicles) {
032                double maxEndTime = 0;
033                for (DvrpVehicle v : vehicles) {
034                        double endTime = v.getSchedule().getEndTime();
035                        if (endTime > maxEndTime) {
036                                maxEndTime = endTime;
037                        }
038                }
039
040                return (int)Math.ceil(maxEndTime / 3600);
041        }
042
043        public static int getHour(double time) {
044                return (int)(time / 3600);
045        }
046
047        public static <T> List<T> createStatsList(T[] hourlyStats, T dailyStats) {
048                List<T> allStats = new ArrayList<>(hourlyStats.length + 1);
049                Collections.addAll(allStats, hourlyStats);
050                allStats.add(dailyStats);
051                return Collections.unmodifiableList(allStats);
052        }
053
054        public static int[] calcHourlyDurations(int from, int to) {
055                int firstHour = (int)from / 3600;
056                int lastHour = (int)to / 3600;
057
058                if (firstHour == lastHour) {
059                        return new int[] { to - from };
060                }
061
062                int[] hourlyDurations = new int[lastHour - firstHour + 1];
063                hourlyDurations[0] = 3600 - from % 3600;
064                hourlyDurations[hourlyDurations.length - 1] = to % 3600;
065                for (int i = 1; i < hourlyDurations.length - 1; i++) {
066                        hourlyDurations[i] = 3600;
067                }
068
069                return hourlyDurations;
070        }
071}