Search in sources :

Example 6 with Counter

use of org.onebusaway.collections.Counter in project onebusaway-gtfs-modules by OneBusAway.

the class CalendarSimplicationLibrary method getSummaryForServiceDates.

public ServiceCalendarSummary getSummaryForServiceDates(Set<ServiceDate> allServiceDates) {
    ServiceCalendarSummary summary = new ServiceCalendarSummary();
    summary.allServiceDates = allServiceDates;
    summary.serviceDatesInOrder = new ArrayList<ServiceDate>(summary.allServiceDates);
    Collections.sort(summary.serviceDatesInOrder);
    if (summary.serviceDatesInOrder.isEmpty()) {
        return summary;
    }
    Calendar c = Calendar.getInstance();
    Counter<Integer> daysOfTheWeekCounts = new Counter<Integer>();
    for (ServiceDate serviceDate : summary.serviceDatesInOrder) {
        c.setTime(serviceDate.getAsDate());
        // Move the service date to "noon" to avoid problems with DST and the
        // day-of-the-week calculation.
        c.add(Calendar.HOUR_OF_DAY, 12);
        int dayOfTheWeek = c.get(Calendar.DAY_OF_WEEK);
        daysOfTheWeekCounts.increment(dayOfTheWeek);
        summary.mostRecentServiceDateByDayOfWeek.put(dayOfTheWeek, serviceDate);
    }
    Integer maxKey = daysOfTheWeekCounts.getMax();
    summary.maxDayOfWeekCount = daysOfTheWeekCounts.getCount(maxKey);
    for (Integer dayOfTheWeek : daysOfTheWeekCounts.getKeys()) {
        int count = daysOfTheWeekCounts.getCount(dayOfTheWeek);
        if (count < summary.maxDayOfWeekCount * _dayOfTheWeekInclusionRatio)
            continue;
        summary.daysOfTheWeekToUse.add(dayOfTheWeek);
    }
    return summary;
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Counter(org.onebusaway.collections.Counter) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Calendar(java.util.Calendar)

Aggregations

Counter (org.onebusaway.collections.Counter)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)2 TimeZone (java.util.TimeZone)2 FactoryMap (org.onebusaway.collections.FactoryMap)2 Route (org.onebusaway.gtfs.model.Route)2 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)2 ProjectedPoint (org.onebusaway.transit_data_federation.model.ProjectedPoint)2 DoubleArrayList (cern.colt.list.DoubleArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 List (java.util.List)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Max (org.onebusaway.collections.Max)1 Cacheable (org.onebusaway.container.cache.Cacheable)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1