/*
Copyright 2015 Google Inc. All Rights Reserved.

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.google.security.zynamics.reil.algorithms.mono;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.base.Preconditions;
import com.google.security.zynamics.reil.algorithms.mono.interfaces.ILatticeElementMono1;

/**
 * Used to keep track of the currently known program state during a run of the monotone framework.
 * 
 * @param <LatticeElement> Type of the elements in the lattice.
 */
public final class StateVector<GraphNode, LatticeElement extends ILatticeElementMono1<LatticeElement>>
    implements IStateVector<GraphNode, LatticeElement>, Iterable<GraphNode> {
  /**
   * Keeps track of the current states of all nodes.
   */
  private final Map<GraphNode, LatticeElement> mapping =
      new LinkedHashMap<GraphNode, LatticeElement>();

  /**
   * Returns the state of a node.
   * 
   * @param node The node in question.
   * 
   * @return The state of the node.
   */
  @Override
  public LatticeElement getState(final GraphNode node) {
    return mapping.get(Preconditions.checkNotNull(node, "Error: node argument can not be null"));
  }

  /**
   * Determines whether a given node has a known state.
   * 
   * @param node The node in question.
   * 
   * @return True, if the node has a known state. False, otherwise.
   */
  @Override
  public boolean hasState(final GraphNode node) {
    return mapping.containsKey(Preconditions.checkNotNull(node,
        "Error: node argument can not be null"));
  }

  @Override
  public Iterator<GraphNode> iterator() {
    return mapping.keySet().iterator();
  }

  /**
   * Sets the state of a node.
   * 
   * @param node The node in question.
   * @param element The new state of the node.
   */
  @Override
  public void setState(final GraphNode node, final LatticeElement element) {
    Preconditions.checkNotNull(node, "Error: node argument can not be null");
    Preconditions.checkNotNull(element, "Error: element argument can not be null");
    mapping.put(node, element);
  }

  /**
   * Returns the number of elements in the vector.
   * 
   * @return The number of elements in the vector.
   */
  @Override
  public int size() {
    return mapping.size();
  }

  @Override
  public String toString() {
    final StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append("[\n");

    for (final Entry<GraphNode, LatticeElement> entry : mapping.entrySet()) {
      stringBuilder.append(entry.getKey() + " -> " + entry.getValue());
      stringBuilder.append("\n");
    }

    stringBuilder.append("]");

    return stringBuilder.toString();
  }
}

@@ -740,16 +740,52 @@
 p.Entry;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -1391,22 +1391,16 @@
 apping =
-%0A     
  new Lin
@@ -1414,33 +1414,8 @@
 Map%3C
-GraphNode, LatticeElement
 %3E();
@@ -2842,209 +2842,87 @@
 ) %7B%0A
+%0A
     
-final StringBuilder stringBuilder = new StringBuilder();%0A%0A
+return mapping.entrySet()%0A        
     
+.
 str
-ingBuilder.append(%22%5B%5Cn%22);%0A%0A    for (final Entry%3CGraphNode, LatticeElement%3E entry : mapping.entrySet()) %7B%0A      stringBuilder.append(
+eam()%0A            .map(entry -%3E 
 entr
@@ -2964,117 +2964,68 @@
 e())
-;
 %0A      
-stringBuilder.append(%22%5Cn%22);%0A    %7D%0A%0A    stringBuilder.append(%22%5D%22);%0A%0A    return stringBuilder.toString(
+      .collect(Collectors.joining(%22%5Cn%22, %22%5B%5Cn%22, %22%5Cn%5D%22)
 );%0A 

Implement toString() with Java8's Stream API