/*
 * MIT License
 * <p>
 * Copyright (c) 2017 David Krebs
 * <p>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p>
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * <p>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.dkarv.jdcallgraph.callgraph.writer;

import com.dkarv.jdcallgraph.util.StackItem;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;

/**
 * A writer that can wrap another writer and forwards nodes and edges
 * only if they did not happen before.
 */
public class RemoveDuplicatesWriter implements GraphWriter {
  private final GraphWriter parentWriter;
  private final HashSet<StackItem> nodes = new HashSet<>();
  private final HashMap<StackItem, HashSet<StackItem>> edges = new HashMap<>();

  public RemoveDuplicatesWriter(GraphWriter parentWriter) {
    this.parentWriter = parentWriter;
  }

  @Override
  public void start(String identifier) throws IOException {
    parentWriter.start(identifier);
  }

  @Override
  public void node(StackItem method, boolean isTest) throws IOException {
    if (!nodes.contains(method)) {
      parentWriter.node(method, isTest);
      nodes.add(method);
    }
  }

  @Override
  public void edge(StackItem from, StackItem to) throws IOException {
    boolean duplicate = false;
    HashSet<StackItem> set = edges.get(from);
    if(set != null){
      if(set.contains(to)){
        duplicate = true;
      } else {
        set.add(to);
      }
    } else {
      set = new HashSet<>();
      set.add(to);
      edges.put(from, set);
    }

    if(!duplicate){
      parentWriter.edge(from, to);
    }
  }

  @Override
  public void end() throws IOException {
    parentWriter.end();
  }

  @Override
  public void close() throws IOException {
    parentWriter.close();
  }
}

@@ -1543,68 +1543,8 @@
 er;%0A
-  private final HashSet%3CStackItem%3E nodes = new HashSet%3C%3E();%0A
   pr
@@ -1619,16 +1619,16 @@
 ap%3C%3E();%0A
+
 %0A  publi
@@ -1926,45 +1926,8 @@
 n %7B%0A
-    if (!nodes.contains(method)) %7B%0A  
     
@@ -1965,39 +1965,8 @@
 t);%0A
-      nodes.add(method);%0A    %7D%0A
   %7D%0A
@@ -2127,24 +2127,25 @@
 rom);%0A    if
+ 
 (set != null
@@ -2145,16 +2145,17 @@
 != null)
+ 
 %7B%0A      
@@ -2156,16 +2156,17 @@
       if
+ 
 (set.con
@@ -2175,16 +2175,17 @@
 ins(to))
+ 
 %7B%0A      
@@ -2350,16 +2350,17 @@
 %0A%0A    if
+ 
 (!duplic
@@ -2363,16 +2363,17 @@
 plicate)
+ 
 %7B%0A      
@@ -2457,32 +2457,32 @@
 s IOException %7B%0A
-
     parentWriter
@@ -2485,24 +2485,43 @@
 iter.end();%0A
+    edges.clear();%0A
   %7D%0A%0A  @Over

clear duplicate list when new graph starts