001/* *********************************************************************** *
002 * project: org.matsim.*
003 * *********************************************************************** *
004 *                                                                         *
005 * copyright       : (C) 2018 by the members listed in the COPYING,        *
006 *                   LICENSE and WARRANTY file.                            *
007 * email           : info at matsim dot org                                *
008 *                                                                         *
009 * *********************************************************************** *
010 *                                                                         *
011 *   This program is free software; you can redistribute it and/or modify  *
012 *   it under the terms of the GNU General Public License as published by  *
013 *   the Free Software Foundation; either version 2 of the License, or     *
014 *   (at your option) any later version.                                   *
015 *   See also COPYING, LICENSE and WARRANTY file                           *
016 *                                                                         *
017 * *********************************************************************** */
018
019package org.matsim.contrib.util;
020
021import java.util.function.Function;
022
023import org.matsim.api.core.v01.BasicLocation;
024import org.matsim.api.core.v01.Coord;
025import org.matsim.api.core.v01.Identifiable;
026import org.matsim.contrib.util.XYDataCollector.XYDataCalculator;
027
028/**
029 * @author michalm
030 */
031public class XYDataCollectors {
032        public static <T extends BasicLocation> XYDataCalculator<T> createCalculator(String[] header,
033                        Function<T, Object[]> valueCalculator) {
034                return createCalculator(header, BasicLocation::getCoord, valueCalculator);
035        }
036
037        public static <T extends BasicLocation> XYDataCalculator<T> createCalculator(String[] header,
038                        Function<T, Coord> coordGetter, Function<T, Object[]> valueCalculator) {
039                return new XYDataCalculator<T>() {
040                        @Override
041                        public String[] getHeader() {
042                                return header;
043                        }
044
045                        @Override
046                        public Coord getCoord(T object) {
047                                return coordGetter.apply(object);
048                        }
049
050                        @Override
051                        public Object[] calculate(T object) {
052                                return valueCalculator.apply(object);
053                        }
054                };
055        }
056}