package net.contargo.iris.connection.service;

import com.google.common.collect.Lists;

import net.contargo.iris.connection.AbstractSubConnection;
import net.contargo.iris.connection.MainRunConnection;
import net.contargo.iris.route.SubRoutePart;
import net.contargo.iris.seaport.Seaport;

import java.util.List;


/**
 * Checks whether a list of {@link MainRunConnection}s contains one such connection that matches with a given list of
 * {@link SubRoutePart} in regard of origin, destination and direction.
 *
 * @author  Sandra Thieme - thieme@synyx.de
 */
public class BargeRailConnectionFinderService {

    MainRunConnection findMatchingBargeRailConnection(List<MainRunConnection> connections,
        List<SubRoutePart> subRouteParts) {

        for (MainRunConnection connection : connections) {
            if (connection.getSubConnections().size() != subRouteParts.size()) {
                continue;
            }

            List<AbstractSubConnection> subConnectionList = connection.getSubConnections();

            boolean reverse = false;

            if (!(subRouteParts.get(0).getOrigin() instanceof Seaport)) {
                subConnectionList = Lists.reverse(connection.getSubConnections());
                reverse = true;
            }

            if (subRoutePartsAndSubconnectionsMatch(subRouteParts, subConnectionList, reverse)) {
                return connection;
            }
        }

        return null;
    }


    private boolean subRoutePartsAndSubconnectionsMatch(List<SubRoutePart> subRouteParts,
        List<AbstractSubConnection> subConnections, boolean reverse) {

        int i = 0;

        while (i < subRouteParts.size()) {
            SubRoutePart subRoutePart = subRouteParts.get(i);
            AbstractSubConnection subConnection = subConnections.get(i);

            if (!subConnection.matchesOriginAndDestination(subRoutePart.getOrigin(), subRoutePart.getDestination(),
                        reverse)) {
                return false;
            }

            i++;
        }

        return true;
    }
}

@@ -314,16 +314,239 @@
 %0A%0A/**%0A *
+ Service providing functionality to find a %7B@link MainRunConnection%7D corresponding to %7B@link SubRoutePart%7Ds.%0A *%0A * @author  Sandra Thieme - thieme@synyx.de%0A */%0Apublic class BargeRailConnectionFinderService %7B%0A%0A    /**%0A     *
  Checks 
@@ -653,14 +653,18 @@
 list
+%0A     *
  of
-%0A *
  %7B@l
@@ -680,16 +680,17 @@
 utePart%7D
+s
  in rega
@@ -735,106 +735,257 @@
 n.%0A 
-*%0A * @author  Sandra Thieme - thieme@synyx.de%0A */%0Apublic class BargeRailConnectionFinderService %7B%0A
+    *%0A     * @param  connections  a list of %7B@link MainRunConnection%7D candidates to be checked%0A     * @param  subRouteParts  a list of %7B@link SubRoutePart%7Ds to check against%0A     *%0A     * @return  a matching %7B@link MainRunConnection%7D or null%0A     */
 %0A   

Improve javadoc for BargeRailConnectionFinderService