package de.tudresden.inf.lat.born.core.term;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * Default implementation of a clause.
 * 
 * @author Julian Mendez
 *
 */
public class ClauseImpl implements Clause {

	public static final String TAB = "   ";

	private final Term head;
	private final List<Term> body = new ArrayList<>();

	/**
	 * Constructs an empty clause.
	 */
	public ClauseImpl() {
		this.head = new TermImpl();
	}

	/**
	 * Constructs a new clause.
	 * 
	 * @param head
	 *            head
	 * @param body
	 *            body
	 */
	public ClauseImpl(Term head, List<Term> body) {
		Objects.requireNonNull(head);
		Objects.requireNonNull(body);
		this.head = head;
		this.body.addAll(body);
	}

	@Override
	public Term getHead() {
		return this.head;
	}

	@Override
	public List<Term> getBody() {
		return this.body;
	}

	@Override
	public String asString() {
		if (this.body.isEmpty()) {
			return this.head.asString() + Symbol.POINT_CHAR + Symbol.NEW_LINE_CHAR;
		} else {
			StringBuffer sbuf = new StringBuffer();
			sbuf.append(this.head.asString() + Symbol.SPACE_CHAR + Symbol.IF_SYMBOL + Symbol.SPACE_CHAR);
			if (this.body.size() == 1) {
				sbuf.append(this.body.iterator().next().asString() + Symbol.POINT_CHAR + Symbol.NEW_LINE_CHAR);
				return sbuf.toString();
			} else {
				Iterator<Term> it = body.iterator();
				while (it.hasNext()) {
					sbuf.append(Symbol.NEW_LINE_CHAR + TAB);
					sbuf.append(it.next().asString());
					if (it.hasNext()) {
						sbuf.append(Symbol.COMMA_CHAR);
					}
				}
				sbuf.append(Symbol.POINT_CHAR);
				sbuf.append(Symbol.NEW_LINE_CHAR);
				return sbuf.toString();
			}
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		} else if (!(obj instanceof Clause)) {
			return false;
		} else {
			Clause other = (Clause) obj;
			return getHead().equals(other.getHead()) && getBody().equals(other.getBody());
		}
	}

	@Override
	public int hashCode() {
		return this.head.hashCode() + 0x1F * this.body.hashCode();
	}

	@Override
	public String toString() {
		return asString();
	}

}

@@ -71,35 +71,8 @@
 st;%0A
-import java.util.Iterator;%0A
 impo
@@ -1035,17 +1035,16 @@
 ngBu
-ff
+ild
 er sb
-uf
  = n
@@ -1058,10 +1058,11 @@
 ngBu
-ff
+ild
 er()
@@ -1064,26 +1064,24 @@
 der();%0A%09%09%09sb
-uf
 .append(this
@@ -1192,26 +1192,24 @@
  1) %7B%0A%09%09%09%09sb
-uf
 .append(this
@@ -1293,34 +1293,32 @@
 );%0A%09%09%09%09return sb
-uf
 .toString();%0A%09%09%09
@@ -1334,180 +1334,151 @@
 %09%09%09%09
-Iterator%3CTerm%3E it = body.iterator();%0A%09%09%09%09while (it.hasNext()) %7B%0A%09%09%09%09%09sbuf.append(Symbol.NEW_LINE_CHAR + TAB);%0A%09%09%09%09%09sbuf.append(it.next().asString());%0A%09%09%09%09%09if (it.hasNext())
+boolean%5B%5D first = new boolean%5B1%5D;%0A%09%09%09%09first%5B0%5D = true;%0A%09%09%09%09this.body.forEach(term -%3E %7B%0A%09%09%09%09%09if (first%5B0%5D) %7B%0A%09%09%09%09%09%09first%5B0%5D = false;%0A%09%09%09%09%09%7D else
  %7B%0A%09
@@ -1480,34 +1480,32 @@
 e %7B%0A%09%09%09%09%09%09sb
-uf
 .append(
 Symbol.COMMA
@@ -1488,24 +1488,29 @@
 %09%09sb.append(
+%22%22 + 
 Symbol.COMMA
@@ -1518,32 +1518,85 @@
 CHAR
-);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
+ + Symbol.SPACE_CHAR);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09sb.append(term.asString());%0A%09%09%09%09%7D);
 %0A%09%09%09%09sb
-uf
 .app
@@ -1625,18 +1625,16 @@
 ;%0A%09%09%09%09sb
-uf
 .append(
@@ -1673,10 +1673,8 @@
 n sb
-uf
 .toS

Change rendering of clauses