package org.psjava;

import org.psjava.util.Assertion;

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

public class BipartiteGraph<V> {

    private Set<V> lefts = new HashSet<>();
    private Set<V> rights = new HashSet<>();
    private List<BipartiteGraphEdge<V>> edges = new ArrayList<>();

    public void addLeft(V v) {
        Assertion.ensure(!rights.contains(v));
        lefts.add(v);
    }

    public void addRight(V v) {
        Assertion.ensure(!lefts.contains(v));
        rights.add(v);
    }

    public void addEdge(final V left, final V right) {
        assertVertexExist(left, lefts);
        assertVertexExist(right, rights);
        edges.add(new BipartiteGraphEdge<>(left, right));
    }

    private void assertVertexExist(V v, Set<V> set) {
        Assertion.ensure(set.contains(v), "vertex is not in graph");
    }

    Collection<V> getLeftVertices() {
        return Collections.unmodifiableCollection(lefts);
    }

    Collection<V> getRightVertices() {
        return Collections.unmodifiableCollection(rights);
    }

    Collection<BipartiteGraphEdge<V>> getEdges() {
        return Collections.unmodifiableCollection(edges);
    }
}

@@ -466,32 +466,76 @@
 ghts.contains(v)
+, () -%3E %22already in right side. vertex=%22 + v
 );%0A        lefts
@@ -625,16 +625,59 @@
 tains(v)
+, () -%3E %22already in left size. vertex=%22 + v
 );%0A     
@@ -998,16 +998,22 @@
 ains(v),
+ () -%3E
  %22vertex
@@ -1028,17 +1028,30 @@
 in graph
-%22
+. vertex=%22 + v
 );%0A    %7D

improve assertion message in BipartiteGraph