private static void mergeTo()

in cassandra/src/main/java/org/apache/ignite/activestore/impl/subscriber/lead/LeadPlanner.java [128:152]


    private static <T> void mergeTo(List<T> fromList, LinkedList<T> toList, Comparator<T> comparator) {
        if (fromList.isEmpty()) {
            return;
        }
        Iterator<T> fromIterator = fromList.iterator();
        ListIterator<T> toIterator = toList.listIterator();
        T currentFromElement = null;

        if (toIterator.hasNext()) {
            currentFromElement = fromIterator.next();
            while (currentFromElement != null && toIterator.hasNext()) {
                if (comparator.compare(toIterator.next(), currentFromElement) > 0) {
                    toIterator.previous();
                    toIterator.add(currentFromElement);
                    currentFromElement = fromIterator.hasNext() ? fromIterator.next() : null;
                }
            }
        }
        if (currentFromElement != null) {
            toList.add(currentFromElement);
        }
        while (fromIterator.hasNext()) {
            toList.add(fromIterator.next());
        }
    }