class BlueUrlTokenizer {

    public static @CheckForNull
    BlueUrlTokenizer parse(@Nonnull String url) {
        Iterator<String> urlTokens = extractTokens(url);

        //
        // Yes, the following code is quite ugly, but it's easy enough to understand atm.
        // Unless this gets a lot more detailed, please don't get super clever ideas about using
        // some fancy-pants abstractions/patterns/3rd-party-libs for parsing the URL that, while
        // might make the code look neater structurally, also makes the code logic a lot harder
        // to follow (without using a debugger).
        //
        if (urlTokens.hasNext()) {
            if (urlTokens.next().equalsIgnoreCase("blue")) {
                BlueUrlTokenizer blueUrlTokenizer = new BlueUrlTokenizer();

                if (urlTokens.hasNext()) {
                    String next = urlTokens.next();

                    if (next.equalsIgnoreCase("pipelines")) {
                        // i.e. /blue/pipelines/
                        blueUrlTokenizer.addPart(UrlPart.DASHBOARD_PIPELINES, next);
                    } else if (next.equalsIgnoreCase("organizations")) {
                        // i.e. /blue/organizations/...
                        if (urlTokens.hasNext()) {
                            // e.g. /blue/organizations/jenkins/...
                            blueUrlTokenizer.addPart(UrlPart.ORGANIZATION, urlTokens.next());
                            if (urlTokens.hasNext()) {
                                // e.g. /blue/organizations/jenkins/f1%2Ff3%20with%20spaces%2Ff3%20pipeline/...
                                blueUrlTokenizer.addPart(UrlPart.PIPELINE, urlDecode(urlTokens.next()));
                                if (urlTokens.hasNext()) {
                                    next = urlTokens.next();
                                    if (next.equalsIgnoreCase("detail")) {
                                        // e.g. /blue/organizations/jenkins/f1%2Ff3%20with%20spaces%2Ff3%20pipeline/detail/...
                                        blueUrlTokenizer.addPart(UrlPart.PIPELINE_RUN_DETAIL, next);
                                        if (urlTokens.hasNext()) {
                                            // e.g. /blue/organizations/jenkins/f1%2Ff3%20with%20spaces%2Ff3%20pipeline/detail/magic-branch-X/...
                                            blueUrlTokenizer.addPart(UrlPart.BRANCH, urlDecode(urlTokens.next()));
                                            if (urlTokens.hasNext()) {
                                                // e.g. /blue/organizations/jenkins/f1%2Ff3%20with%20spaces%2Ff3%20pipeline/detail/magic-branch-X/55/...
                                                blueUrlTokenizer.addPart(UrlPart.PIPELINE_RUN_DETAIL_ID, urlDecode(urlTokens.next()));
                                                if (urlTokens.hasNext()) {
                                                    next = urlTokens.next();
                                                    if (PIPELINE_RUN_DETAIL_TABS.contains(next.toLowerCase())) {
                                                        // e.g. /blue/organizations/jenkins/f1%2Ff3%20with%20spaces%2Ff3%20pipeline/detail/magic-branch-X/55/pipeline
                                                        blueUrlTokenizer.addPart(UrlPart.PIPELINE_RUN_DETAIL_TAB, next.toLowerCase());
                                                    }
                                                }
                                            }
                                        }
                                    } else if (PIPELINE_TABS.contains(next.toLowerCase())) {
                                        // e.g. /blue/organizations/jenkins/f1%2Ff3%20with%20spaces%2Ff3%20pipeline/activity/
                                        blueUrlTokenizer.addPart(UrlPart.PIPELINE_TAB, next.toLowerCase());
                                    }
                                }
                            }
                        }
                    }
                }

                return blueUrlTokenizer;
            }
        }

        return null;
    }

    private  BlueUrlTokenizer();

}