ByteTransition getTransitionForAllBytes()

in src/main/software/amazon/event/ruler/ByteMap.java [151:193]


    ByteTransition getTransitionForAllBytes() {
        Set<SingleByteTransition> candidates = new HashSet<>();
        Iterator<ByteTransition> iterator = map.values().iterator();
        ByteTransition firstByteTransition = iterator.next();
        if (firstByteTransition == null) {
            return ByteMachine.EmptyByteTransition.INSTANCE;
        }
        firstByteTransition.expand().forEach(single -> candidates.add(single));

        while (iterator.hasNext()) {
            ByteTransition nextByteTransition = iterator.next();
            if (nextByteTransition == null) {
                return ByteMachine.EmptyByteTransition.INSTANCE;
            }
            Iterable<SingleByteTransition> singles = nextByteTransition.expand();
            if (singles instanceof Set) {
                candidates.retainAll((Set) singles);
            } else if (singles instanceof SingleByteTransition) {
                SingleByteTransition single = (SingleByteTransition) singles;
                if (candidates.contains(single)) {
                    if (candidates.size() > 1) {
                        candidates.clear();
                        candidates.add(single);
                    }
                } else {
                    if (!candidates.isEmpty()) {
                        candidates.clear();
                    }
                }
            } else {
                // singles should always be a Set or SingleByteTransition. Thus, this "else" is expected to be dead code
                // but it is here for logical correctness if anything changes in the future.
                Set<SingleByteTransition> set = new HashSet<>();
                singles.forEach(single -> set.add(single));
                candidates.retainAll(set);
            }
            if (candidates.isEmpty()) {
                return ByteMachine.EmptyByteTransition.INSTANCE;
            }
        }

        return coalesce(candidates);
    }