private static List<String> doGetTopics(List<String> allTopics, Optional<String> topicIncludeList, Boolean excludeInternalTopics) {
        if (topicIncludeList.isPresent()) {
            IncludeList topicsFilter = new IncludeList(topicIncludeList.get());
            return allTopics.stream()
                .filter(topic -> topicsFilter.isTopicAllowed(topic, excludeInternalTopics))
                .collect(Collectors.toList());
        } else {
            return allTopics.stream()
                .filter(topic -> !(Topic.isInternal(topic) && excludeInternalTopics))
                .collect(Collectors.toList());
        }
    }
ensures topic existence and throws exception if topic doesn't exist

@param foundTopics        Topics that were found to match the requested topic name.
@param requestedTopic     Name of the topic that was requested.
@param requireTopicExists Indicates if the topic needs to exist for the operation to be successful.
                          If set to true, the command will throw an exception if the topic with the
                          requested name does not exist.
tools/src/main/java/org/apache/kafka/tools/TopicCommand.java