<commit_before>/* 
 * 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 net.grinder.util;

import java.util.List;
import java.util.Random;

import net.grinder.common.GrinderProperties;
import net.grinder.script.Grinder;
import net.grinder.script.InternalScriptContext;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.math.NumberUtils;

/**
 * Convenient NGrinder utilities.
 * 
 * @author <NAME>
 * @since 3.2.3
 */
public abstract class GrinderUtil {
	/**
	 * Get this thread uniq id among all threads in the all agents.
	 * 
	 * @return unique id b/w from 0 to total thread count.
	 * @since 3.2.3
	 */
	public static int getThreadUniqId() {
		InternalScriptContext grinder = getGrinderInstance();
		GrinderProperties properties = grinder.getProperties();
		int totalProcessCount = properties.getInt("grinder.processes", 1);
		int totalThreadCount = properties.getInt("grinder.threads", 1);
		int agentNumber = grinder.getAgentNumber();
		int processNumber = grinder.getProcessNumber();
		int threadNumber = grinder.getThreadNumber();
		// Calc the current thread's unique id
		return (agentNumber * totalProcessCount * totalThreadCount) + (processNumber * totalThreadCount) + threadNumber;
	}

	private static InternalScriptContext getGrinderInstance() {
		InternalScriptContext grinder = Grinder.grinder;
		if (grinder == null || grinder.getThreadNumber() == -1) {
			throw new RuntimeException("This method should be called in the worker thread context.");
		}
		return grinder;
	}

	private static Random random = new Random();

	/**
	 * Get the any element from list.
	 * 
	 * @param from
	 *            list
	 * @return any element in the list
	 * @since 3.2.3
	 */
	public static <T> T any(List<T> from) {
		return from.get(random.nextInt(from.size()));
	}

	/**
	 * Get the any element from araay.
	 * 
	 * @param from
	 *            list
	 * @return any element in the list
	 * @since 3.2.3<commit_msg>[NGRINDER-661] Provide convenient utility class to retrieve the grinder info <commit_after>/* 
 * 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 net.grinder.util;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import net.grinder.common.GrinderProperties;
import net.grinder.script.Grinder;
import net.grinder.script.InternalScriptContext;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.math.NumberUtils;

import HTTPClient.NVPair;

/**
 * Convenient NGrinder utilities.
 * 
 * @author <NAME>
 * @since 3.2.3
 */
public abstract class GrinderUtil {
	/**
	 * Get this thread uniq id among all threads in the all agents.
	 * 
	 * @return unique id b/w from 0 to total thread count.
	 * @since 3.2.3
	 */
	public static int getThreadUniqId() {
		InternalScriptContext grinder = getGrinderInstance();
		GrinderProperties properties = grinder.getProperties();
		int totalProcessCount = properties.getInt("grinder.processes", 1);
		int totalThreadCount = properties.getInt("grinder.threads", 1);
		int agentNumber = grinder.getAgentNumber();
		int processNumber = grinder.getProcessNumber();
		int threadNumber = grinder.getThreadNumber();
		// Calc the current thread's unique id
		return (agentNumber * totalProcessCount * totalThreadCount) + (processNumber * totalThreadCount) + threadNumber;
	}

	private static InternalScriptContext getGrinderInstance() {
		InternalScriptContext grinder = Grinder.grinder;
		if (grinder == null || grinder.getThreadNumber() == -1) {
			throw new RuntimeException("This method should be called in the worker thread context.");
		}
		return grinder;
	}

	private static Random random = new Random();

	/**
	 * Convert nvpair map to array. With this user can create NVPair array using
	 * following code.
	 * 
	 * 
	 * <code>
	 * import static net.ngrinder.util.GrinderUtil.*
	 * ...
	 * request1.POST("http://www.google.com", nvs(["key1":"value1", "key2":"value2"]))
	 * </code>
	 * 
	 * @param nvpairMap
	 *            map of the
	 * @return converted array
	 */
	public static NVPair[] nvs(Map<Object, Object> nvpairMap) {
		NVPair[] result = new NVPair[nvpairMap.size()];
		int i = 0;
		for (Entry<Object, Object> each : nvpairMap.entrySet()) {
			result[i++] = new NVPair(each.getKey().toString(), each.getValue().toString());
		}
		return result;
	}

	/**
	 * Get the any element from list.
	 * 
	 * 
	 * <code>
	 * import static net.ngrinder.util.GrinderUtil.*
	 * ...
	 * def values = [1,2,3,4,5]
	 * def selected = any(values)
	 * </code>
	 * 
	 * @param from
	 *            list
	 * @return any element in the list
	 * @since 3.2.3
	 */
	public static <T> T any(List<T> from) {
		return from.get(random.nextInt(from.size()));
	}

	/**
	 * Get the any element from araay.
	 * 
	 * @param from
	 *            list
	 * @return any element in the list
	 * @since 3.2.3
{'fraction_non_alphanumeric': 0.11130049429009715, 'fraction_numerical': 0.009204022498721664, 'mean_word_length': 3.591549295774648, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, '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': '2891447', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1710, 'n_words': 569}