<commit_before>package be.kuleuven.cs.distrinet.rejuse.graph;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableSet;

import be.kuleuven.cs.distrinet.rejuse.java.collections.SafeTransitiveClosure;

/**
 * @author <NAME>
 */
public class Node<V> {
  
  /**
   * Initialize a new node for the given object.
   * 
   * @param object
   *        The object that is put in the graph.
   */
 /*@
   @ public behavior
   @
   @ pre object != null;
   @
   @ post getObject() == object;
   @ post getEdges().isEmpty();
   @*/
  public Node(V object) {
    _object = object;
    _outgoing = new HashSet<>();
    _incoming = new HashSet<>();
  }
  
  /**
   * Return the object of this node.
   */
 /*@
   @ public behavior
   @
   @ post \result != null; 
   @*/
  public V object() {
    return _object;
  }
  
  /**
   * The object of this edge.
   */
  private V _object;
  
  /**
   * Add the given edge to this node.
   * 
   * @param edge
   *        The edge to be added.
   */
 /*@
   @ public behavior
   @
   @ pre edge != null;
   @ // The edge must already haven been connected to this node.
   @ pre edge.startsIn(this) || edge.endsIn(this)
   @
   @ post edge.startsIn(this) ==> getStartEdges().contains(edge)
   @ post edge.endsIn(this) ==> getEndEdges().contains(edge)
   @*/
  void addEdge(Edge<V> edge) {
    if(edge.startsIn(this)) {
      _outgoing.add(edge);
    }
    if(edge.endsIn(this)) {
      _incoming.add(edge);
    }
  }
  
  /**
   * Remove the given edge from this node.
   * 
   * @param edge
   *        The edge to be removed.
   */
 /*@
   @ public behavior
   @
   @ post ! getEdges().contains(edge); 
   @*/
  void removeEdge(Edge<V> edge) {
    _outgoing.remove(edge);
    _incoming.remove(edge);
  }
  
  /**
   * Return all edges starting in this node.
   */
 /*@
   @ public behavior
   @
   @ post \result != null
   @ post ! \result.contains(null);
   @ post (\forall Edge e; e != null; \result.contains(e) == e.startsIn(this)); 
   @*/
  public Set<Edge<V>> outgoingEdges() {
    return new HashSet<>(_outgoing);
  }
  
  /**
   * Return all edges ending in this node.
   */
 /*@
   @ public behavior
   @
   @ post \result != null
   @ post ! \result.contains(null);
   @ post (\forall Edge e; e != null; \result.contains(e) == e.endsIn(this)); 
   @*/
  public Set<Edge<V>> incomingEdges() {
    return new HashSet<>(_outgoing);
  }
  
  /**
   * Return the number of edges starting in this node.
   */
 /*@
   @ public behavior
   @<commit_msg>added auxiliary methods<commit_after>package be.kuleuven.cs.distrinet.rejuse.graph;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.common.collect.ImmutableSet;

import be.kuleuven.cs.distrinet.rejuse.java.collections.SafeTransitiveClosure;

/**
 * @author <NAME>
 */
public class Node<V> {
  
  /**
   * Initialize a new node for the given object.
   * 
   * @param object
   *        The object that is put in the graph.
   */
 /*@
   @ public behavior
   @
   @ pre object != null;
   @
   @ post getObject() == object;
   @ post getEdges().isEmpty();
   @*/
  public Node(V object) {
    _object = object;
    _outgoing = new HashSet<>();
    _incoming = new HashSet<>();
  }
  
  /**
   * Return the object of this node.
   */
 /*@
   @ public behavior
   @
   @ post \result != null; 
   @*/
  public V object() {
    return _object;
  }
  
  /**
   * The object of this edge.
   */
  private V _object;
  
  /**
   * Add the given edge to this node.
   * 
   * @param edge
   *        The edge to be added.
   */
 /*@
   @ public behavior
   @
   @ pre edge != null;
   @ // The edge must already haven been connected to this node.
   @ pre edge.startsIn(this) || edge.endsIn(this)
   @
   @ post edge.startsIn(this) ==> getStartEdges().contains(edge)
   @ post edge.endsIn(this) ==> getEndEdges().contains(edge)
   @*/
  void addEdge(Edge<V> edge) {
    if(edge.startsIn(this)) {
      _outgoing.add(edge);
    }
    if(edge.endsIn(this)) {
      _incoming.add(edge);
    }
  }
  
  /**
   * Remove the given edge from this node.
   * 
   * @param edge
   *        The edge to be removed.
   */
 /*@
   @ public behavior
   @
   @ post ! getEdges().contains(edge); 
   @*/
  void removeEdge(Edge<V> edge) {
    _outgoing.remove(edge);
    _incoming.remove(edge);
  }
  
  /**
   * Return all edges starting in this node.
   */
 /*@
   @ public behavior
   @
   @ post \result != null
   @ post ! \result.contains(null);
   @ post (\forall Edge e; e != null; \result.contains(e) == e.startsIn(this)); 
   @*/
  public Set<Edge<V>> outgoingEdges() {
    return ImmutableSet.copyOf(_outgoing);
  }
  
  public Set<Edge<V>> outgoingEdges(Node<V> target) {
  	Set<Edge<V>> result = new HashSet<>();
  	for(Edge<V> out: _outgoing) {
  		if(out.opposite(this) == target) {
  			result.add(out);
  		}
  	}
  	return result;
  }
  
  public Edge<V> someOutgoingEdge(Node<V> target) {
  	for(Edge<V> out: _outgoing) {
  		if(out.opposite(this) == target) {
  			return out;
  		}
  	}
  	return null;
  }
  
  /**
   * Return all edges ending in this node.
   */
 /*@
   @ public behavior
   @
   @ post \result != null
   @ post ! \result.contains(null);
   @ post (\forall Edge e; e != null; \result.contains(e) == e.endsIn(this)); 
   @*/
  public Set<Edge<V>> incomingEdges() {
    return new HashSet<>(_outgoing);
  }
  
  /**
   * Return the number of edges starting in this node.
   */
 /*@
   @ public behavior
   @
{'fraction_non_alphanumeric': 0.1529897647692584, 'fraction_numerical': 0.0, 'mean_word_length': 2.3017190278601065, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2646442', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1990, 'n_words': 464}