Java tutorial
/* MIT License Copyright (c) 2017 Hamdi Kavak Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.hamdikavak.humanmobility.modeling; import java.util.List; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; import org.joda.time.DateTime; import org.joda.time.Duration; import com.hamdikavak.humanmobility.modeling.exception.InsufficientLocationTraceException; import com.hamdikavak.humanmobility.modeling.exception.TimeUnitNotSupportedException; import com.hamdikavak.humanmobility.modeling.spatial.LocationTrace; /** * This class characterizes the temporal regularities of location reporting. * * @author Hamdi Kavak * @version 0.3.0 * */ public class LocationReport { protected final static Logger logger = Logger.getLogger(LocationReport.class); /** * Used to determine minimum number of location traces to be needed for * location report pattern calculations. */ private int minimumNumberOfTraces = 2; public int getMinimumNumberOfTraces() { return minimumNumberOfTraces; } public void setMinimumNumberOfTraces(int minimumNumberOfTraces) { this.minimumNumberOfTraces = minimumNumberOfTraces; } /** * This method calculates location inter-report times in minutes. * @param traces location traces belong to a person * @return inter-report times * @throws InsufficientLocationTraceException * @throws TimeUnitNotSupportedException */ public long[] calculateLocationReport(List<LocationTrace> traces) throws InsufficientLocationTraceException, TimeUnitNotSupportedException { return calculateLocationReport(traces, TimeUnit.MINUTES); } /** * This method calculates location inter-report times. * @param traces location traces belong to a person * @param unit time unit * @return inter-report times * @throws InsufficientLocationTraceException * @throws TimeUnitNotSupportedException */ public long[] calculateLocationReport(List<LocationTrace> traces, TimeUnit unit) throws InsufficientLocationTraceException, TimeUnitNotSupportedException { if (traces == null || traces.size() < minimumNumberOfTraces) { throw new InsufficientLocationTraceException( "LocationTrace list " + "need to have at least " + minimumNumberOfTraces + " elements."); } if (unit == TimeUnit.NANOSECONDS || unit == TimeUnit.MICROSECONDS || unit == TimeUnit.MILLISECONDS) { throw new TimeUnitNotSupportedException( unit + " is not supported. Please pass seconds or above as time unit."); } long[] locationReportTimes = new long[traces.size() - 1]; DateTime date1 = traces.get(0).getUTCTime(); DateTime date2; for (int i = 1; i < traces.size(); i++) { date2 = traces.get(i).getUTCTime(); locationReportTimes[i - 1] = calculateDuration(date1, date2, unit); date1 = date2; } return locationReportTimes; } /** * * @param date1 first event date * @param date2 second event date * @param unit time unit * @return duration duration (default millisecond) */ private long calculateDuration(DateTime date1, DateTime date2, TimeUnit unit) { if (unit == TimeUnit.SECONDS) { return Math.abs(new Duration(date1, date2).getStandardSeconds()); } else if (unit == TimeUnit.MINUTES) { return Math.abs(new Duration(date1, date2).getStandardMinutes()); } else if (unit == TimeUnit.HOURS) { return Math.abs(new Duration(date1, date2).getStandardHours()); } else if (unit == TimeUnit.DAYS) { return Math.abs(new Duration(date1, date2).getStandardDays()); } return 0; } }