protected Optional topicCandidate()

in src/main/java/com/awslabs/aws/greengrass/provisioner/implementations/helpers/BasicSubscriptionHelper.java [82:187]


    protected Optional<String> topicCandidate(String topic1, String topic2) {
        List<String> splitTopic1 = Arrays.asList(topic1.split("/"));
        List<String> splitTopic2 = Arrays.asList(topic2.split("/"));
        int splitTopic1Length = splitTopic1.size();
        int splitTopic2Length = splitTopic2.size();

        int shortestLength = Math.min(splitTopic1Length, splitTopic2Length);

        List<String> output = new ArrayList<>();

        for (int loop = 0; loop < shortestLength - 1; loop++) {
            String input1 = splitTopic1.get(loop);
            String input2 = splitTopic2.get(loop);

            if (isMultilevelWildcard(input1) || isMultilevelWildcard(input2)) {
                // Input 1 or 2 contains a multilevel wildcard in the middle, this is an error
                throw new RuntimeException("Invalid pattern #1, multilevel wildcards can only be used at the last topic hierarchy level");
            }

            Optional<String> optionalSingleTopicLevel = getSingleTopicLevel(input1, input2);

            if (!optionalSingleTopicLevel.isPresent()) {
                // No match
                return Optional.empty();
            }

            output.add(optionalSingleTopicLevel.get());
        }

        // This is the final level for one or both of these
        int finalIndex = shortestLength - 1;

        String input1 = splitTopic1.get(finalIndex);
        String input2 = splitTopic2.get(finalIndex);

        boolean input1End = ((splitTopic1Length - 1) == finalIndex);
        boolean input2End = ((splitTopic2Length - 1) == finalIndex);

        if (input1End && input2End && input1.equals(input2)) {
            // Both are at their last level and both are equal, use the last level of input 1
            output.add(input1);

            return outputCandidate(output);
        }

        if (isSingleLevelWildcard(input1)) {
            if (!input2End) {
                // Input 1 is a single level wildcard but input 2 has more than one level left, no match
                return Optional.empty();
            }

            // Use the final level of input 2
            output.add(input2);

            return outputCandidate(output);
        }

        if (isSingleLevelWildcard(input2)) {
            if (!input1End) {
                // Input 2 is a single level wildcard but input 1 has more than one level left, no match
                return Optional.empty();
            }

            // Use the final level of input 1
            output.add(input1);

            return outputCandidate(output);
        }

        boolean input1MultilevelWildcard = isMultilevelWildcard(input1);
        boolean input2MultilevelWildcard = isMultilevelWildcard(input2);

        if (input1MultilevelWildcard && !input1End) {
            // Input 1 contains a multilevel wildcard but it isn't at the end, this is an error
            throw new RuntimeException("Invalid pattern #2, multilevel wildcards can only be used at the last topic hierarchy level");
        }

        if (input2MultilevelWildcard && !input2End) {
            // Input 2 contains a multilevel wildcard but it isn't at the end, this is an error
            throw new RuntimeException("Invalid pattern #3, multilevel wildcards can only be used at the last topic hierarchy level");
        }

        if (input1MultilevelWildcard && input2MultilevelWildcard) {
            // Both end in a multilevel wildcard, use a multilevel wildcard as the final part of the topic
            output.add("#");

            return outputCandidate(output);
        }

        if (input1MultilevelWildcard) {
            // Input 1 ends in a multilevel wildcard as its very last level, use the rest of the input 2 topic
            output.addAll(splitTopic2.subList(finalIndex, splitTopic2Length));

            return outputCandidate(output);
        }

        if (input2MultilevelWildcard) {
            // Input 2 ends in a multilevel wildcard as its very last level, use the rest of the input 1 topic
            output.addAll(splitTopic1.subList(finalIndex, splitTopic1Length));

            return outputCandidate(output);
        }

        // Doesn't look like a match
        return Optional.empty();
    }