<commit_before>package wicket.extensions.markup.html.form.palette.component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import wicket.extensions.markup.html.form.palette.Palette;
import wicket.markup.html.form.HiddenField;
import wicket.markup.html.form.IChoiceRenderer;
import wicket.model.Model;

/**
 * Component to keep track of selections on the html side. Also used for
 * encoding and decoding those selections between html and java.
 * 
 * @author <NAME> ( ivaynberg )
 */
public class Recorder extends HiddenField
{
	private static final long serialVersionUID = 1L;

	/** conviniently maintained array of selected ids */
	private String[] ids;

	/** parent palette object */
	private Palette palette;

	/**
	 * @return parent Palette object
	 */
	public Palette getPalette()
	{
		return palette;
	}

	/**
	 * @param id
	 *            component id
	 * @param palette
	 *            parent palette object
	 */
	public Recorder(String id, Palette palette)
	{
		super(id);
		this.palette = palette;

		// construct the model string based on selection collection
		IChoiceRenderer renderer = getPalette().getChoiceRenderer();
		StringBuffer modelStringBuffer = new StringBuffer();
		Iterator selection = getPalette().getModelCollection().iterator();
		while (selection.hasNext())
		{
			modelStringBuffer.append(renderer.getIdValue(selection.next(), 0));
			if (selection.hasNext())
			{
				modelStringBuffer.append(",");
			}
		}

		// set model and update ids array
		String modelString = modelStringBuffer.toString();
		setModel(new Model(modelString));
		ids = modelString.split(",");
	}

	protected void onValid()
	{
		super.onValid();
		updateIds();
	}

	/**
	 * @return iterator over selected choices
	 */
	public Iterator getSelectedChoices()
	{
		IChoiceRenderer renderer = getPalette().getChoiceRenderer();

		if (ids.length == 0)
		{
			return Collections.EMPTY_LIST.iterator();
		}

		List selected = new ArrayList(ids.length);
		for (int i = 0; i < ids.length; i++)
		{
			Iterator it = getPalette().getChoices().iterator();
			while (it.hasNext())
			{
				final Object choice = it.next();
				if (renderer.getIdValue(choice, 0).equals(ids[i]))
				{
					selected.add(choice);
					break;
				}
			}
		}
		return selected.iterator();
	}

	/**
	 * @return iterator over unselected choices
	 */
	public Iterator getUnselectedChoices()
	{
		IChoiceRenderer renderer = getPalette().getChoiceRenderer();
		Collection choices = getPalette().getChoices();

		if (choices.size() - ids.length == 0)
		{
			return Collections.EMPTY_LIST.iterator();
		}

		List unselected = new ArrayList(Math.max(1, choices.size() - ids.length));
		Iterator it = choices.iterator();
		while (it.hasNext())
		{
			final Object choice = it.next();
			final String choiceId = renderer.getIdValue(choice, 0);
			boolean selected = false;
			for (int i = 0; i < ids.length; i++)
			{
				if (ids[i].equals(choiceId))
				{
					selected = true;
					break;
				}
			}
			if (!selected)
			{
				unselected.add(choice);
			}
		}
		return unselected.iterator();
	}


	protected void onInvalid()
	{
		super.onInvalid();
		updateIds();
	}

	private void updateIds()
	{
		ids = getValue().split(",");
	}

}<commit_msg>fixed [ 1473231 ] Palette doesn't show item when choicesModel has only 1<commit_after>package wicket.extensions.markup.html.form.palette.component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import wicket.extensions.markup.html.form.palette.Palette;
import wicket.markup.html.form.HiddenField;
import wicket.markup.html.form.IChoiceRenderer;
import wicket.model.Model;
import wicket.util.string.Strings;

/**
 * Component to keep track of selections on the html side. Also used for
 * encoding and decoding those selections between html and java.
 * 
 * @author <NAME> ( ivaynberg )
 */
public class Recorder extends HiddenField
{
	private static final long serialVersionUID = 1L;

	private static final String[] EMPTY_IDS=new String[0];
	
	/** conviniently maintained array of selected ids */
	private String[] ids;

	/** parent palette object */
	private Palette palette;

	/**
	 * @return parent Palette object
	 */
	public Palette getPalette()
	{
		return palette;
	}

	/**
	 * @param id
	 *            component id
	 * @param palette
	 *            parent palette object
	 */
	public Recorder(String id, Palette palette)
	{
		super(id);
		this.palette = palette;

		// construct the model string based on selection collection
		IChoiceRenderer renderer = getPalette().getChoiceRenderer();
		StringBuffer modelStringBuffer = new StringBuffer();
		Iterator selection = getPalette().getModelCollection().iterator();
		while (selection.hasNext())
		{
			modelStringBuffer.append(renderer.getIdValue(selection.next(), 0));
			if (selection.hasNext())
			{
				modelStringBuffer.append(",");
			}
		}

		// set model and update ids array
		String modelString = modelStringBuffer.toString();
		setModel(new Model(modelString));
		updateIds(modelString);
	}

	
	protected void onValid()
	{
		super.onValid();
		updateIds();
	}

	/**
	 * @return iterator over selected choices
	 */
	public Iterator getSelectedChoices()
	{
		IChoiceRenderer renderer = getPalette().getChoiceRenderer();

		if (ids.length == 0)
		{
			return Collections.EMPTY_LIST.iterator();
		}

		List selected = new ArrayList(ids.length);
		for (int i = 0; i < ids.length; i++)
		{
			Iterator it = getPalette().getChoices().iterator();
			while (it.hasNext())
			{
				final Object choice = it.next();
				if (renderer.getIdValue(choice, 0).equals(ids[i]))
				{
					selected.add(choice);
					break;
				}
			}
		}
		return selected.iterator();
	}

	/**
	 * @return iterator over unselected choices
	 */
	public Iterator getUnselectedChoices()
	{
		IChoiceRenderer renderer = getPalette().getChoiceRenderer();
		Collection choices = getPalette().getChoices();

		if (choices.size() - ids.length == 0)
		{
			return Collections.EMPTY_LIST.iterator();
		}

		List unselected = new ArrayList(Math.max(1, choices.size() - ids.length));
		Iterator it = choices.iterator();
		while (it.hasNext())
		{
			final Object choice = it.next();
			final String choiceId = renderer.getIdValue(choice, 0);
			boolean selected = false;
			for (int i = 0; i < ids.length; i++)
			{
				if (ids[i].equals(choiceId))
				{
					selected = true;
					break;
				}
			}
			if (!selected)
			{
				unselected.add(choice);
			}
		}
		return unselected.iterator();
	}


	protected void onInvalid()
	{
		super.onInvalid();
		updateIds();
	}

	private void updateIds()
	{
		updateIds(getValue());
	}

	private void updateIds(String value) {
		if (Strings.isEmpty(value)) {
			ids=EMPTY_IDS;
		} else {
			ids = value.split(",");
		}
	}

}
{'fraction_non_alphanumeric': 0.12856935787795332, 'fraction_numerical': 0.003913610668212784, 'mean_word_length': 3.4892648015614833, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '2020858', 'n_tokens_mistral': 2465, 'n_tokens_neox': 2132, 'n_words': 534}