<commit_before>package edu.ucla.cens.awserver.validator.prompt;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import edu.ucla.cens.awserver.util.JsonUtils;

/**
 * Some JSON restrictions for prompt responses are defined by maps. The phone/device must send a valid key based on the restriction
 * for each particular map type. 
 * 
 * @author selsky
 */
public class MapValidator extends NullValidator {
	private List<Integer> _keyList;
	
	/**
	 * @throws IllegalArgumentException if the provided string is not parseable as JSON
	 */
	public MapValidator(String restriction) {
		JSONObject jsonObject = JsonUtils.getJsonObjectFromString(restriction);
		
		if(null == jsonObject) {
			throw new IllegalArgumentException("restriction is not a valid JSON object");
		}
		
		// All that is needed are the object "keys"
		
		String[] keys = JSONObject.getNames(jsonObject);
		
		if(null == keys) {
			throw new IllegalArgumentException("no keys found in JSONObject");
		}
		
		_keyList = new ArrayList<Integer>();
		
		for(String key : keys) {
			_keyList.add(Integer.parseInt(key));
		}
	}
	
	/**
	 * @return true if the response consists of a known key or is the string "null"
	 * @return false otherwise
	 */
	public boolean validate(String response) {
		if(super.validate(response)) {
			return true;
		}
		
		return _keyList.contains(Integer.parseInt(response));
		
	}

}<commit_msg>now correctly handles non-integer keys (error case)<commit_after>package edu.ucla.cens.awserver.validator.prompt;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import edu.ucla.cens.awserver.util.JsonUtils;

/**
 * Some JSON restrictions for prompt responses are defined by maps. The phone/device must send a valid key based on the restriction
 * for each particular map type. All map keys for prompt responses are defined to be integers.
 * 
 * @author selsky
 */
public class MapValidator extends NullValidator {
	private List<Integer> _keyList;
	
	/**
	 * @throws IllegalArgumentException if the provided string is not parseable as JSON
	 */
	public MapValidator(String restriction) {
		JSONObject jsonObject = JsonUtils.getJsonObjectFromString(restriction);
		
		if(null == jsonObject) {
			throw new IllegalArgumentException("restriction is not a valid JSON object");
		}
		
		// All that is needed are the object "keys"
		
		String[] keys = JSONObject.getNames(jsonObject);
		
		if(null == keys) {
			throw new IllegalArgumentException("no keys found in JSONObject");
		}
		
		_keyList = new ArrayList<Integer>();
		
		for(String key : keys) {
			_keyList.add(Integer.parseInt(key));
		}
	}
	
	/**
	 * @return true if the response consists of a known integer key or is the string "null"
	 * @return false otherwise
	 */
	public boolean validate(String response) {
		if(super.validate(response)) {
			return true;
		}
		
		int val = 0;
		
		try {
			
			val = Integer.parseInt(response);
			
		} catch(NumberFormatException nfe) {
			
			return false;
		}
		
		return _keyList.contains(val);
		
	}

}
{'fraction_non_alphanumeric': 0.09581425768476128, 'fraction_numerical': 0.0003270111183780249, 'mean_word_length': 3.720679012345679, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6035045', 'n_tokens_mistral': 1000, 'n_tokens_neox': 853, 'n_words': 284}