001/* *********************************************************************** *
002 * project: org.matsim.*
003 * ArrayBasedTravelTimeInfoProvider.java
004 *                                                                         *
005 * *********************************************************************** *
006 *                                                                         *
007 * copyright       : (C) 2013 by the members listed in the COPYING,        *
008 *                   LICENSE and WARRANTY file.                            *
009 * email           : info at matsim dot org                                *
010 *                                                                         *
011 * *********************************************************************** *
012 *                                                                         *
013 *   This program is free software; you can redistribute it and/or modify  *
014 *   it under the terms of the GNU General Public License as published by  *
015 *   the Free Software Foundation; either version 2 of the License, or     *
016 *   (at your option) any later version.                                   *
017 *   See also COPYING, LICENSE and WARRANTY file                           *
018 *                                                                         *
019 * *********************************************************************** */
020
021package org.matsim.withinday.trafficmonitoring;
022
023import java.util.Map;
024
025import org.matsim.api.core.v01.Id;
026import org.matsim.api.core.v01.network.Link;
027import org.matsim.api.core.v01.network.Network;
028import org.matsim.core.router.priorityqueue.HasIndex;
029import org.matsim.withinday.trafficmonitoring.WithinDayTravelTime.TravelTimeInfo;
030
031public class ArrayBasedTravelTimeInfoProvider implements TravelTimeInfoProvider {
032
033        private final TravelTimeInfo[] arrayLinkData;
034        private final TravelTimeInfoProvider delegate;
035        
036        public ArrayBasedTravelTimeInfoProvider(Map<Id<Link>, TravelTimeInfo> linkData, Network network) {
037                this.delegate = new MapBasedTravelTimeInfoProvider(linkData);
038                this.arrayLinkData = new TravelTimeInfo[linkData.size()];
039        }
040        
041        /*
042         * This method is called from the EventHandler part of the WithinDayTravelTime.
043         * There, only link ids are available. We cannot optimize this. 
044         */
045        @Override
046        public TravelTimeInfo getTravelTimeInfo(final Id<Link> linkId) {
047                return this.delegate.getTravelTimeInfo(linkId);
048        }
049        
050        /*
051         * This method is called from the TravelTime part of the WithinDayTravelTime.
052         * There, link are available. we can optimize this by using an array instead of a map.
053         */
054        @Override
055        public TravelTimeInfo getTravelTimeInfo(Link link) {
056                if (link instanceof HasIndex) {
057                        int index = ((HasIndex) link).getArrayIndex();
058                        TravelTimeInfo data = this.arrayLinkData[index];
059                        if (data == null) {
060                                data = this.delegate.getTravelTimeInfo(link);
061                                this.arrayLinkData[index] = data;
062                        }
063                        return data;
064                } else {
065                        return this.delegate.getTravelTimeInfo(link);
066                }
067        }
068        
069}