<commit_before>package trikita.anvil;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import java.util.WeakHashMap;

import static trikita.anvil.Nodes.*;

/**
 * <p>
 * This class consists of the static methods to contrl UI rendering process.
 * </p>
 * <p>
 * Inside it manages a collection of {@link Renderable} components. Every
 * component that is now visible there is a mount point (e.g. a parent
 * viewgroup into which the component is rendered).
 * </p>
 * <p>
 * Because the components can be nested there is also a current render stack
 * which helps to prevent infinite recursive rendering. So during the render
 * cycle each component is rendered only once.
 * </p>
 */
public final class Anvil {

	private Anvil() {}

	/**
	 * A map of active renderables and their actual view nodes. References to the
	 * components are weak, so when the component is removed from the views
	 * hierarchy - the map item will be removed as well
	 */
	private static Map<Renderable, ViewNode> mounts =
		new WeakHashMap<Renderable, ViewNode>();
	/**
	 * A stack of the components being rendering during the current rendering
	 * cycle. After rendering iteration is complete - the stack must be empty.
	 */
	private static Deque<Renderable> renderStack = new ArrayDeque<Renderable>();

	/** A flag that tells the renderer to skip the next cycle */
	private static boolean skipNextRender = false;

	/** Returns the current renderable component. This method is intended to be
	 * called from inside the {@link Renderable#view} method. From the current
	 * renderable the root view and associated resources can be extracted.
	 * @return a component being rendered right now
	 */
	public static Renderable getCurrentRenderable() {
		return renderStack.peek();
	}

	/**
	 * A low-level method to start rendering cycle for the certain renderable.
	 * Useful for components that use their own rendering algorithm (e.g.
	 * AdapterView)
	 * @param r the component that needs to be rendered
	 * @return true if the component is not being rendered at the moment,
	 *         false otherwise
	 */
	public static boolean startRendering(Renderable r) {
		if (renderStack.contains(r)) {
			return false;
		}
		renderStack.push(r);
		return true;
	}

	/**
	 * A low-level method to stop the current rendering cycle
	 */
	public static void stopRendering() {
		renderStack.pop();
	}

	/**
	 * Tells rendered to skip the next rendering cycle
	 */
	public static void skipRender() {
		skipNextRender = true;
	}

	/**
	 * Perform rendering of a single renderable
	 * @param r the component that needs to be rendered
	 */
	public static void render(Renderable r) {
		if (!startRendering(r)) {
			return; // Don't render same renderer recursively
		}
		ViewNode oldValue = mounts.get(r);
		ViewNode virt = r.view();
		mounts.put(r, virt);

		inflateNode(r.getRootView().getContext(), virt, oldValue, new ViewMount(r.getRootView(), 0));
		stopRendering();
	}

	/** Perform rendering of all active renderables */
	public static void render() {
		if (skipNextRender) {
			skipNextRender = false;
			return;
		}
		for (Renderable r: mounts.keySet()) {
			render(r);
		}
	}
<commit_msg>fixed renderable iteration in Anvil to avoid concurrent modification exceptions<commit_after>package trikita.anvil;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import java.util.WeakHashMap;

import static trikita.anvil.Nodes.*;
import java.util.HashSet;
import java.util.Set;

/**
 * <p>
 * This class consists of the static methods to contrl UI rendering process.
 * </p>
 * <p>
 * Inside it manages a collection of {@link Renderable} components. Every
 * component that is now visible there is a mount point (e.g. a parent
 * viewgroup into which the component is rendered).
 * </p>
 * <p>
 * Because the components can be nested there is also a current render stack
 * which helps to prevent infinite recursive rendering. So during the render
 * cycle each component is rendered only once.
 * </p>
 */
public final class Anvil {

	private Anvil() {}

	/**
	 * A map of active renderables and their actual view nodes. References to the
	 * components are weak, so when the component is removed from the views
	 * hierarchy - the map item will be removed as well
	 */
	private static Map<Renderable, ViewNode> mounts =
		new WeakHashMap<Renderable, ViewNode>();
	/**
	 * A stack of the components being rendering during the current rendering
	 * cycle. After rendering iteration is complete - the stack must be empty.
	 */
	private static Deque<Renderable> renderStack = new ArrayDeque<Renderable>();

	/** A flag that tells the renderer to skip the next cycle */
	private static boolean skipNextRender = false;

	/** Returns the current renderable component. This method is intended to be
	 * called from inside the {@link Renderable#view} method. From the current
	 * renderable the root view and associated resources can be extracted.
	 * @return a component being rendered right now
	 */
	public static Renderable getCurrentRenderable() {
		return renderStack.peek();
	}

	/**
	 * A low-level method to start rendering cycle for the certain renderable.
	 * Useful for components that use their own rendering algorithm (e.g.
	 * AdapterView)
	 * @param r the component that needs to be rendered
	 * @return true if the component is not being rendered at the moment,
	 *         false otherwise
	 */
	public static boolean startRendering(Renderable r) {
		if (renderStack.contains(r)) {
			return false;
		}
		renderStack.push(r);
		return true;
	}

	/**
	 * A low-level method to stop the current rendering cycle
	 */
	public static void stopRendering() {
		renderStack.pop();
	}

	/**
	 * Tells rendered to skip the next rendering cycle
	 */
	public static void skipRender() {
		skipNextRender = true;
	}

	/**
	 * Perform rendering of a single renderable
	 * @param r the component that needs to be rendered
	 */
	public static void render(Renderable r) {
		if (!startRendering(r)) {
			return; // Don't render same renderer recursively
		}
		ViewNode oldValue = mounts.get(r);
		ViewNode virt = r.view();
		mounts.put(r, virt);

		inflateNode(r.getRootView().getContext(), virt, oldValue, new ViewMount(r.getRootView(), 0));
		stopRendering();
	}

	/** Perform rendering of all active renderables */
	public static void render() {
		if (skipNextRender) {
			skipNextRender = false;
			return;
		}
		// We need to copy the keyset, otherwise a concurrent modification may
		// happen if Renderables are nested
		Set<Renderable> set = new HashSet<Renderable>();
		set.addAll(mounts.keySet());
		for (Renderable r: set) {
			render(r);
		}
	}

{'fraction_non_alphanumeric': 0.09114470842332613, 'fraction_numerical': 0.00028797696184305253, 'mean_word_length': 3.7219578518014957, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '1076663', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1912, 'n_words': 802}