/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.debugger.streams.resolve;

import com.intellij.debugger.streams.trace.smart.TraceElement;
import com.intellij.debugger.streams.trace.smart.resolve.TraceInfo;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Vitaliy.Bibaev
 */
public class FlatMapResolver implements ValuesOrderResolver {
  @NotNull
  @Override
  public Result resolve(@NotNull TraceInfo info) {
    final Map<Integer, TraceElement> before = info.getValuesOrderBefore();
    final Map<Integer, TraceElement> after = info.getValuesOrderAfter();
    final Map<TraceElement, List<TraceElement>> forward = new LinkedHashMap<>();
    final Map<TraceElement, List<TraceElement>> backward = new LinkedHashMap<>();

    after.values().stream().distinct().forEach(x -> backward.put(x, new ArrayList<>()));

    final Integer[] beforeTimes = new Integer[before.size()];
    final Integer[] afterTimes = new Integer[after.size()];
    before.keySet().toArray(beforeTimes);
    after.keySet().toArray(afterTimes);

    int rightIndex = 0;
    for (int i = 0; i < beforeTimes.length; i++) {
      final TraceElement leftValue = before.get(beforeTimes[i]);
      final List<TraceElement> right = new ArrayList<>();
      final int nextLeftTime = i + 1 < beforeTimes.length ? beforeTimes[i + 1] : Integer.MAX_VALUE;
      while (rightIndex < afterTimes.length && afterTimes[rightIndex] < nextLeftTime) {
        final TraceElement rightValue = after.get(afterTimes[rightIndex]);

        right.add(rightValue);
        backward.get(rightValue).add(leftValue);
        rightIndex++;
      }

      forward.put(leftValue, right);
    }

    return Result.of(forward, backward);
  }
}

@@ -837,94 +837,9 @@
 til.
-ArrayList;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.List;%0Aimport java.util.Map
+*
 ;%0A%0A/
@@ -1209,38 +1209,32 @@
 %3E forward = new 
-Linked
 HashMap%3C%3E();%0A   
@@ -1297,14 +1297,8 @@
 new 
-Linked
 Hash
@@ -1315,154 +1315,96 @@
     
-after.values().stream().distinct().forEach(x -%3E backward.put(x, new ArrayList%3C%3E()));%0A%0A    final Integer%5B%5D beforeTimes = new Integer%5Bbefore.size
+final int%5B%5D beforeTimes = before.keySet().stream().mapToInt(Integer::intValue).toArray
 ()
-%5D
 ;%0A  
@@ -1411,23 +1411,19 @@
   final 
-I
+i
 nt
-eger
 %5B%5D after
@@ -1434,114 +1434,68 @@
 s = 
-new Integer%5Bafter.size()%5D;%0A    before.keySet().toArray(beforeTimes);%0A    after.keySet().toArray(afterTimes
+after.keySet().stream().mapToInt(Integer::intValue).toArray(
 );%0A%0A
@@ -1502,21 +1502,22 @@
     int 
-right
+before
 Index = 
@@ -1595,25 +1595,28 @@
 Element 
-leftValue
+afterElement
  = befor
@@ -1669,21 +1669,29 @@
 lement%3E 
-right
+afterElements
  = new A
@@ -1720,28 +1720,30 @@
 nal int next
-Left
+Before
 Time = i + 1
@@ -1819,21 +1819,22 @@
  while (
-right
+before
 Index %3C 
@@ -1861,29 +1861,30 @@
  afterTimes%5B
-right
+before
 Index%5D %3C nex
@@ -1888,12 +1888,14 @@
 next
-Left
+Before
 Time
@@ -1925,26 +1925,29 @@
 Element 
-rightValue
+beforeElement
  = after
@@ -1962,21 +1962,22 @@
 erTimes%5B
-right
+before
 Index%5D);
@@ -1990,77 +1990,117 @@
     
-right.add(rightValue);%0A        backward.get(rightValue).add(leftValue
+afterElements.add(beforeElement);%0A        backward.put(beforeElement, Collections.singletonList(afterElement)
 );%0A 
@@ -2106,21 +2106,22 @@
         
-right
+before
 Index++;
@@ -2152,24 +2152,35 @@
 put(
-leftValue, right
+afterElement, afterElements
 );%0A 

Refactor code in FlatMapResolver