<commit_before>package jp.gr.java_conf.daisy.ajax_mutator.mutator;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import jp.gr.java_conf.daisy.ajax_mutator.Util;
import jp.gr.java_conf.daisy.ajax_mutator.mutatable.Mutatable;

import org.mozilla.javascript.ast.AstNode;

/**
 * Abstract implementation of {@code Mutator} which replacing part of Mutatable
 * by something.
 *
 * @author <NAME>
 */
public abstract class AbstractMutator<T extends Mutatable> implements Mutator {
	protected PrintStream stream;
	protected List<T> mutationTargets;
	protected int targetIndex = 0;
	protected static PrintStream DEFAULT_STREAM = System.out;

	protected AbstractMutator(Collection<T> mutationTargets) {
		this(mutationTargets, DEFAULT_STREAM);
	}

	protected AbstractMutator(Collection<T> mutationTargets,
			PrintStream printStream) {
		this.stream = printStream;
		this.mutationTargets = new ArrayList<T>(mutationTargets);
	}

	/**
	 * @param parent
	 *            Mutatable part of which to be mutated
	 * @param replacingNode
	 *            node that replace part of {@code parent}
	 */
	abstract protected void replaceFocusedNodeOf(T parent,
			AstNode replacingNodwe);

	/**
	 * @return node that can replace a part of mutation target. When appropriate
	 *         node do not exist or cannot be found, returns null.
	 */
	abstract protected AstNode selectReplacingCandidate(T mutationTarget);

	@Override
	public String applyMutation() {
		T mutationTarget = mutationTargets.get(targetIndex);
		AstNode replacingNode = selectReplacingCandidate(mutationTarget);
		if (replacingNode == null) {
			System.out.println("mutation is not applied to: ");
			System.out.println(mutationTarget.toString());
			return null;
		}
		String mutationInformation = mutationInformation(mutationTarget,
				replacingNode);
		if (stream != null)
			stream.println(mutationInformation);
		replaceFocusedNodeOf(mutationTarget, replacingNode);
		return mutationInformation;
	}

	@Override
	public void undoMutation() {
		Mutatable mutationTarget = mutationTargets.get(targetIndex);
		mutationTarget.undoLastReplace();
		targetIndex++;
	}

	@Override
	public boolean isFinished() {
		return mutationTargets.size() <= targetIndex;
	}

	@Override
	public int numberOfMutation() {<commit_msg>workaround for avoiding infiinite loop<commit_after>package jp.gr.java_conf.daisy.ajax_mutator.mutator;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import jp.gr.java_conf.daisy.ajax_mutator.Util;
import jp.gr.java_conf.daisy.ajax_mutator.mutatable.Mutatable;

import org.mozilla.javascript.ast.AstNode;

/**
 * Abstract implementation of {@code Mutator} which replacing part of Mutatable
 * by something.
 *
 * @author <NAME>
 */
public abstract class AbstractMutator<T extends Mutatable> implements Mutator {
	protected PrintStream stream;
	protected List<T> mutationTargets;
	protected int targetIndex = 0;
	protected static PrintStream DEFAULT_STREAM = System.out;

	protected AbstractMutator(Collection<T> mutationTargets) {
		this(mutationTargets, DEFAULT_STREAM);
	}

	protected AbstractMutator(Collection<T> mutationTargets,
			PrintStream printStream) {
		this.stream = printStream;
		this.mutationTargets = new ArrayList<T>(mutationTargets);
	}

	/**
	 * @param parent
	 *            Mutatable part of which to be mutated
	 * @param replacingNode
	 *            node that replace part of {@code parent}
	 */
	abstract protected void replaceFocusedNodeOf(T parent,
			AstNode replacingNodwe);

	/**
	 * @return node that can replace a part of mutation target. When appropriate
	 *         node do not exist or cannot be found, returns null.
	 */
	abstract protected AstNode selectReplacingCandidate(T mutationTarget);

	@Override
	public String applyMutation() {
		T mutationTarget = mutationTargets.get(targetIndex);
		AstNode replacingNode = selectReplacingCandidate(mutationTarget);
		if (replacingNode == null) {
			System.out.println("mutation is not applied to: ");
			System.out.println(mutationTarget.toString());
			targetIndex++;
			return null;
		}
		String mutationInformation = mutationInformation(mutationTarget,
				replacingNode);
		if (stream != null)
			stream.println(mutationInformation);
		replaceFocusedNodeOf(mutationTarget, replacingNode);
		return mutationInformation;
	}

	@Override
	public void undoMutation() {
		Mutatable mutationTarget = mutationTargets.get(targetIndex);
		mutationTarget.undoLastReplace();
		targetIndex++;
	}

	@Override
	public boolean isFinished() {
		return mutationTargets.size() <= targetIndex;
	}

	@Override
	public int numberOfMutation() {
{'fraction_non_alphanumeric': 0.09133489461358314, 'fraction_numerical': 0.0004258037044922291, 'mean_word_length': 4.77859778597786, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '5044733', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1336, 'n_words': 353}