<commit_before>package org.obda.query.domain.imp;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.obda.query.domain.Atom;
import org.obda.query.domain.CQIE;
import org.obda.query.domain.Term;
import org.obda.query.tools.util.EventGeneratingLinkedList;
import org.obda.query.tools.util.ListListener;

public class CQIEImpl implements CQIE, ListListener {

	private Atom		head		= null;
	private List<Atom>	body		= null;
	private boolean		isBoolean	= false;

	private int			hash		= -1;

	private boolean		rehash		= true;

	private String		string		= null;

	// TODO Remove isBoolean from the signature and from any method
	public CQIEImpl(Atom head, List<Atom> body, boolean isBoolean) {

		this.isBoolean = isBoolean;

		// The syntax for CQ may contain no body, thus, this condition will
		// check whether the construction of the link list is possible or not.
		if (body != null) {
			EventGeneratingLinkedList<Atom> eventbody = new EventGeneratingLinkedList<Atom>();
			eventbody.addAll(body);

			this.body = eventbody;

			eventbody.addListener(this);
		}

		// The syntax for CQ may also contain no head, thus, this condition
		// will check whether we can look for the head terms or not.
		if (head != null) {
			this.head = head;

			EventGeneratingLinkedList<Term> headterms = (EventGeneratingLinkedList<Term>) head.getTerms();
			headterms.addListener(this);
		}
	}

	public List<Atom> getBody() {
		return body;
	}

	public Atom getHead() {
		return head;
	}

	public void updateHead(Atom head) {

		EventGeneratingLinkedList<Term> headterms = (EventGeneratingLinkedList<Term>) head.getTerms();
		headterms.removeListener(this);

		this.head = head;

		rehash = true;
		string = null;
	}

	public void updateBody(List<Atom> body) {
		this.body.clear();
		this.body.addAll(body);
	}

	@Override
	public int hashCode() {
		if (rehash) {
			hash = toString().hashCode();
			rehash = false;
		}
		return hash;
	}

	@Override
	public boolean isBoolean() {
		return isBoolean;
	}

	@Override
	public String toString() {
		if (string == null) {
			StringBuilder sb = new StringBuilder();
			sb.append(head.toString());
			sb.append(":-");

			Iterator<Atom> bit = body.iterator();
			while (bit.hasNext()) {
				Atom atom = bit.next();
				if (bit.hasNext()) {
					sb.append(", ");
				}
				sb.append(atom.toString());
			}
			string = sb.toString();
		}
		return string;
	}

	@Override
	public CQIEImpl clone() {
		Atom copyHead = head.copy();
		List<Atom> copyBody = new LinkedList<Atom>();
		for (Atom atom : body) {
			copyBody.add(atom.copy());
		}
		boolean copyIsBoolean = isBoolean;

		return new CQIEImpl(copyHead, copyBody, copyIsBoolean);<commit_msg>Fix the toString() method: a comma sign is printed if there is a next atom after the current one. I also create some constants for the symbols used in printing the string.<commit_after>package org.obda.query.domain.imp;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.obda.query.domain.Atom;
import org.obda.query.domain.CQIE;
import org.obda.query.domain.Term;
import org.obda.query.tools.util.EventGeneratingLinkedList;
import org.obda.query.tools.util.ListListener;

public class CQIEImpl implements CQIE, ListListener {

	private Atom		head		= null;
	private List<Atom>	body		= null;
	private boolean		isBoolean	= false;

	private int			hash		= -1;

	private boolean		rehash		= true;

	private String		string		= null;

	private static final String SPACE = " ";
	private static final String COMMA = ",";
	private static final String INV_IMPLIES = ":-";

	// TODO Remove isBoolean from the signature and from any method
	public CQIEImpl(Atom head, List<Atom> body, boolean isBoolean) {

		this.isBoolean = isBoolean;

		// The syntax for CQ may contain no body, thus, this condition will
		// check whether the construction of the link list is possible or not.
		if (body != null) {
			EventGeneratingLinkedList<Atom> eventbody = new EventGeneratingLinkedList<Atom>();
			eventbody.addAll(body);

			this.body = eventbody;

			eventbody.addListener(this);
		}

		// The syntax for CQ may also contain no head, thus, this condition
		// will check whether we can look for the head terms or not.
		if (head != null) {
			this.head = head;

			EventGeneratingLinkedList<Term> headterms = (EventGeneratingLinkedList<Term>) head.getTerms();
			headterms.addListener(this);
		}
	}

	public List<Atom> getBody() {
		return body;
	}

	public Atom getHead() {
		return head;
	}

	public void updateHead(Atom head) {

		EventGeneratingLinkedList<Term> headterms = (EventGeneratingLinkedList<Term>) head.getTerms();
		headterms.removeListener(this);

		this.head = head;

		rehash = true;
		string = null;
	}

	public void updateBody(List<Atom> body) {
		this.body.clear();
		this.body.addAll(body);
	}

	@Override
	public int hashCode() {
		if (rehash) {
			hash = toString().hashCode();
			rehash = false;
		}
		return hash;
	}

	@Override
	public boolean isBoolean() {
		return isBoolean;
	}

	@Override
	public String toString() {
		if (string == null) {
			StringBuilder sb = new StringBuilder();
			sb.append(head.toString());
			sb.append(SPACE + INV_IMPLIES + SPACE); // print " :- "

			Iterator<Atom> bit = body.iterator();
			while (bit.hasNext()) {
				Atom atom = bit.next();
				sb.append(atom.toString());

				if (bit.hasNext()) { // if there is a next atom.
					sb.append(COMMA + SPACE); // print ", "
				}
			}
			string = sb.toString();
		}
		return string;
	}

	@Override
	public CQIEImpl clone() {
		Atom copyHead = head.copy();
		List<Atom> copyBody = new LinkedList<Atom>();
		for (Atom atom : body) {
			copyBody.add(atom.copy());
		}
		boolean copyIsBoolean = isBoolean;

		return new CQIEImpl(copyHead, copyBody, copyIsBoolean);
{'fraction_non_alphanumeric': 0.11829625797551302, 'fraction_numerical': 0.0003448870494912916, 'mean_word_length': 3.654895666131621, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 29, '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': '6274394', 'n_tokens_mistral': 2137, 'n_tokens_neox': 1920, 'n_words': 532}