<commit_before>/*******************************************************************************
 * Copyright 2012 The Regents of the University of California
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.ohmage.domain.campaign.prompt;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;
import org.ohmage.config.grammar.custom.ConditionValuePair;
import org.ohmage.domain.campaign.Prompt;
import org.ohmage.exception.DomainException;

/**
 * This class represents prompts that have a set of choices.
 * 
 * @author <NAME>
 */
public abstract class ChoicePrompt extends Prompt {
	/**
	 * The key to use when exporting this to JSON to indicate the list of 
	 * choices.
	 */
	public static final String JSON_KEY_CHOICE_GLOSSARY = "choice_glossary";
	
	private final Map<Integer, LabelValuePair> choices;
	private final boolean hasValues;

	/**
	 * Creates a new choice prompt.
	 * 
	 * @param id The unique identifier for the prompt within its survey item
	 * 			 group.
	 * 
	 * @param condition The condition determining if this prompt should be
	 * 					displayed.
	 * 
	 * @param unit The unit value for this prompt.
	 * 
	 * @param text The text to be displayed to the user for this prompt.
	 * 
	 * @param abbreviatedText An abbreviated version of the text to be 
	 * 						  displayed to the user for this prompt.
	 * 
	 * @param explanationText A more-verbose version of the text to be 
	 * 						  displayed to the user for this prompt.
	 * 
	 * @param skippable Whether or not this prompt may be skipped.
	 * 
	 * @param skipLabel The text to show to the user indicating that the prompt
	 * 					may be skipped.
	 * 
	 * @param displayType This prompt's
	 * 					 {@link org.ohmage.domain.campaign.Prompt.DisplayType}.
	 * 
	 * @param displayLabel The display label for this prompt.
	 * 
	 * @param choices A map of choices from their integer key value to their
	 * 				  label-value pairs.
	 * 
	 * @param type This prompt's 
	 * 			   {@link org.ohmage.domain.campaign.Prompt.Type}.
	 * 
	 * @param index This prompt's index in its container's list of survey 
	 * 				items.
	 * 
	 * @throws DomainException Thrown if any of the required parameters are 
	 * 						   missing or invalid. 
	 */
	public ChoicePrompt(
			final String id, 
			final String condition, 
			final String unit, 
			final String text, 
			final String abbreviatedText, 
			final String explanationText,
			final boolean skippable, 
			final String skipLabel,
			final DisplayType displayType, 
			final String displayLabel,
			final Map<Integer, LabelValuePair> choices, 
			final Type type,
			final int index) 
			throws DomainException {

		super(id, condition, unit, text, abbreviatedText, explanationText,
				skippable, skipLabel, displayType, displayLabel, 
				type, index);
		
		Set<String> labels = new HashSet<String>();
		for(LabelValuePair lvp : choices.values()) {
			if(! labels.add(lvp.getLabel())) {
				throw new DomainException(
						"Multiple choices have the same label for the prompt: " + 
							id);
			}
		}
		this.choices = new HashMap<Integer, LabelValuePair>(choices);
		
		boolean tHasValues = false;
		Set<String> labels = new HashSet<String>();
		for(LabelValuePair lvp : choices.values()) {
			if(! labels.add(lvp.getLabel())) {
				throw new IllegalArgumentException(
						"Two choices have the same label for prompt '" +
							id +
							"': " +
							lvp.getLabel());
			}
			
			if(lvp.getValue() != null) {
				tHasValues = true;
			}
		}
		hasValues = tHasValues;
	}
	
	/**
	 * Returns an unmodifiable map of the choices.
	 * 
	 * @return An unmodifiable map of the choices where the key is the integer<commit_msg>Fixed a bug from a broken merge.<commit_after>/*******************************************************************************
 * Copyright 2012 The Regents of the University of California
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.ohmage.domain.campaign.prompt;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;
import org.ohmage.config.grammar.custom.ConditionValuePair;
import org.ohmage.domain.campaign.Prompt;
import org.ohmage.exception.DomainException;

/**
 * This class represents prompts that have a set of choices.
 * 
 * @author <NAME>
 */
public abstract class ChoicePrompt extends Prompt {
	/**
	 * The key to use when exporting this to JSON to indicate the list of 
	 * choices.
	 */
	public static final String JSON_KEY_CHOICE_GLOSSARY = "choice_glossary";
	
	private final Map<Integer, LabelValuePair> choices;
	private final boolean hasValues;

	/**
	 * Creates a new choice prompt.
	 * 
	 * @param id The unique identifier for the prompt within its survey item
	 * 			 group.
	 * 
	 * @param condition The condition determining if this prompt should be
	 * 					displayed.
	 * 
	 * @param unit The unit value for this prompt.
	 * 
	 * @param text The text to be displayed to the user for this prompt.
	 * 
	 * @param abbreviatedText An abbreviated version of the text to be 
	 * 						  displayed to the user for this prompt.
	 * 
	 * @param explanationText A more-verbose version of the text to be 
	 * 						  displayed to the user for this prompt.
	 * 
	 * @param skippable Whether or not this prompt may be skipped.
	 * 
	 * @param skipLabel The text to show to the user indicating that the prompt
	 * 					may be skipped.
	 * 
	 * @param displayType This prompt's
	 * 					 {@link org.ohmage.domain.campaign.Prompt.DisplayType}.
	 * 
	 * @param displayLabel The display label for this prompt.
	 * 
	 * @param choices A map of choices from their integer key value to their
	 * 				  label-value pairs.
	 * 
	 * @param type This prompt's 
	 * 			   {@link org.ohmage.domain.campaign.Prompt.Type}.
	 * 
	 * @param index This prompt's index in its container's list of survey 
	 * 				items.
	 * 
	 * @throws DomainException Thrown if any of the required parameters are 
	 * 						   missing or invalid. 
	 */
	public ChoicePrompt(
			final String id, 
			final String condition, 
			final String unit, 
			final String text, 
			final String abbreviatedText, 
			final String explanationText,
			final boolean skippable, 
			final String skipLabel,
			final DisplayType displayType, 
			final String displayLabel,
			final Map<Integer, LabelValuePair> choices, 
			final Type type,
			final int index) 
			throws DomainException {

		super(id, condition, unit, text, abbreviatedText, explanationText,
				skippable, skipLabel, displayType, displayLabel, 
				type, index);
		

		boolean tHasValues = false;
		Set<String> labels = new HashSet<String>();
		for(LabelValuePair lvp : choices.values()) {
			if(! labels.add(lvp.getLabel())) {
				throw new DomainException(
						"Two choices have the same label for prompt '" +
								id +
								"': " +
								lvp.getLabel());
			}
			
			if(lvp.getValue() != null) {
				tHasValues = true;
			}
		}
		this.choices = new HashMap<Integer, LabelValuePair>(choices);
		hasValues = tHasValues;
	}
	
	/**
	 * Returns an unmodifiable map of the choices.
	 * 
	 * @return An unmodifiable map of the choices where the key is the integer
{'fraction_non_alphanumeric': 0.11481177436378562, 'fraction_numerical': 0.001876392635158907, 'mean_word_length': 3.2682682682682684, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2399867', 'n_tokens_mistral': 2766, 'n_tokens_neox': 2260, 'n_words': 905}