package com.graphhopper.routing.util;

import com.graphhopper.reader.ReaderWay;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FerrySpeedCalculator {

    private static final Logger LOGGER = LoggerFactory.getLogger(FerrySpeedCalculator.class);
    private final double speedFactor;
    private final double unknownSpeed, longSpeed, shortSpeed, maxSpeed;

    public FerrySpeedCalculator(double speedFactor, double maxSpeed, double longSpeed, double shortSpeed, double unknownSpeed) {
        this.speedFactor = speedFactor;
        this.unknownSpeed = unknownSpeed;
        this.longSpeed = longSpeed;
        this.shortSpeed = shortSpeed;
        this.maxSpeed = maxSpeed;
    }

    /**
     * Special handling for ferry ways.
     */
    public double getSpeed(ReaderWay way) {
        // During the reader process we have converted the duration value into an artificial tag called "duration:seconds".
        Long duration = way.getTag("duration:seconds", 0L);
        // seconds to hours
        double durationInHours = duration / 60d / 60d;
        // Check if our graphhopper specific artificially created estimated_distance way tag is present
        Number estimatedLength = way.getTag("estimated_distance", null);
        if (durationInHours > 0)
            try {
                if (estimatedLength != null) {
                    double estimatedLengthInKm = estimatedLength.doubleValue() / 1000;
                    // If duration AND distance is available we can calculate the speed more precisely
                    // and set both speed to the same value. Factor 1.4 slower because of waiting time!
                    double calculatedTripSpeed = estimatedLengthInKm / durationInHours / 1.4;
                    // Plausibility check especially for the case of wrongly used PxM format with the intention to
                    // specify the duration in minutes, but actually using months
                    if (calculatedTripSpeed > 0.01d) {
                        if (calculatedTripSpeed > maxSpeed) {
                            return maxSpeed;
                        }
                        // If the speed is lower than the speed we can store, we have to set it to the minSpeed, but > 0
                        if (Math.round(calculatedTripSpeed) < speedFactor / 2) {
                            return speedFactor / 2;
                        }

                        return Math.round(calculatedTripSpeed);
                    } else {
                        long lastId = way.getNodes().isEmpty() ? -1 : way.getNodes().get(way.getNodes().size() - 1);
                        long firstId = way.getNodes().isEmpty() ? -1 : way.getNodes().get(0);
                        if (firstId != lastId)
                            LOGGER.warn("Unrealistic long duration ignored in way with way ID=" + way.getId() + " : Duration tag value="
                                    + way.getTag("duration") + " (=" + Math.round(duration / 60d) + " minutes)");
                        durationInHours = 0;
                    }
                }
            } catch (Exception ex) {
            }

        if (durationInHours == 0) {
            if (estimatedLength != null && estimatedLength.doubleValue() <= 300)
                return speedFactor / 2;
            // unknown speed -> put penalty on ferry transport
            return unknownSpeed;
        } else if (durationInHours > 1) {
            // lengthy ferries should be faster than short trip ferry
            return longSpeed;
        } else {
            return shortSpeed;
        }
    }
}

@@ -1279,31 +1279,11 @@
 %3E 0)
-%0A            try %7B%0A    
+ %7B%0A
     
@@ -1333,28 +1333,24 @@
             
-    
 double estim
@@ -1404,20 +1404,16 @@
 / 1000;%0A
-    
         
@@ -1519,20 +1519,16 @@
         
-  
-  
 // and s
@@ -1615,28 +1615,24 @@
             
-    
 double calcu
@@ -1693,20 +1693,16 @@
  / 1.4;%0A
-    
         
@@ -1816,28 +1816,24 @@
             
-    
 // specify t
@@ -1890,36 +1890,32 @@
                 
-    
 if (calculatedTr
@@ -1945,36 +1945,32 @@
                 
-  
-  
 if (calculatedTr
@@ -2011,28 +2011,24 @@
             
-    
 return maxSp
@@ -2028,28 +2028,24 @@
 n maxSpeed;%0A
-    
             
@@ -2070,28 +2070,24 @@
             
-    
 // If the sp
@@ -2187,28 +2187,24 @@
             
-    
 if (Math.rou
@@ -2264,36 +2264,32 @@
                 
-  
-  
 return speedFact
@@ -2312,34 +2312,27 @@
             
-    %7D%0A%0A   
+%7D%0A%0A
             
@@ -2335,25 +2335,24 @@
             
- 
 return Math.
@@ -2391,28 +2391,24 @@
             
-    
 %7D else %7B%0A   
@@ -2396,36 +2396,32 @@
        %7D else %7B%0A
-    
                 
@@ -2533,28 +2533,24 @@
             
- 
-   
 long firstId
@@ -2623,28 +2623,24 @@
             
-    
 if (firstId 
@@ -2674,20 +2674,16 @@
         
-   
- 
 LOGGER.w
@@ -2815,20 +2815,16 @@
         
-    
 + way.ge
@@ -2913,20 +2913,16 @@
         
-    
 duration
@@ -2946,34 +2946,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -2970,49 +2970,8 @@
   %7D%0A
-            %7D catch (Exception ex) %7B%0A    
     

Remove unnecessary try/catch