<commit_before>
package com.badlogic.gdx.utils;

import com.badlogic.gdx.utils.JsonWriter.OutputType;

import java.util.Iterator;
import java.util.NoSuchElementException;

/** Container for a JSON object, array, string, double, long, boolean, or null.
 * <p>
 * JsonValue children are a linked list. Iteration of arrays or objects is easily done using a for loop, either with the enhanced
 * for loop syntactic sugar or like the example below. This is much more efficient than accessing children by index when there are
 * many children.<br>
 * 
 * <pre>
 * JsonValue map = ...;
 * for (JsonValue entry = map.child(); entry != null; entry = entry.next())
 * 	System.out.println(entry.name() + " = " + entry.asString());
 * </pre>
 * @author <NAME> */
public class JsonValue implements Iterable<JsonValue> {
	private ValueType type;

	private String stringValue;
	private double doubleValue;
	private long longValue;

	public String name;
	/** May be null. */
	public JsonValue child, next, prev;
	public int size;

	public JsonValue (ValueType type) {
		this.type = type;
	}

	/** @param value May be null. */
	public JsonValue (String value) {
		set(value);
	}

	public JsonValue (double value) {
		set(value);
	}

	public JsonValue (long value) {
		set(value);
	}

	public JsonValue (boolean value) {
		set(value);
	}

	/** Returns the child at the specified index.
	 * @return May be null. */
	public JsonValue get (int index) {
		JsonValue current = child;
		while (current != null && index > 0) {
			index--;
			current = current.next;
		}
		return current;
	}

	/** Returns the child with the specified name.
	 * @return May be null. */
	public JsonValue get (String name) {
		JsonValue current = child;
		while (current != null && !current.name.equalsIgnoreCase(name))
			current = current.next;
		return current;
	}

	/** Returns the child at the specified index.
	 * @throws IllegalArgumentException if the child was not found. */
	public JsonValue require (int index) {
		JsonValue current = child;
		while (current != null && index > 0) {
			index--;
			current = current.next;
		}
		if (current == null) throw new IllegalArgumentException("Child not found with index: " + index);
		return current;
	}

	/** Returns the child with the specified name.
	 * @throws IllegalArgumentException if the child was not found. */
	public JsonValue require (String name) {
		JsonValue current = child;
		while (current != null && !current.name.equalsIgnoreCase(name))
			current = current.next;
		if (current == null) throw new IllegalArgumentException("Child not found with name: " + name);
		return current;
	}

	/** Removes the child with the specified name.
	 * @return May be null. */
	public JsonValue remove (int index) {
		JsonValue child = get(index);
		if (child == null) return null;
		if (child.prev == null) {
			this.child = child.next;
			if (this.child != null) this.child.prev = null;
		} else {
			child.prev.next = child.next;
			if (child.next != null) child.next.prev = child.prev;
		}
		size--;
		return child;
	}

	/** Removes the child with the specified name.
	 * @return May be null. */
	public JsonValue remove (String name) {
		JsonValue child = get(name);
		if (child == null) return null;
		if (child.prev == null) {
			this.child = child.next;
			if (this.child != null) this.child.prev = null;
		} else {
			child.prev.next = child.next;
			if (child.next != null) child.next.prev = child.prev;
		}
		size--;
		return child;
	}

	/** @deprecated Use the size property instead. Returns this number of children in the array or object. */
	public int size () {
		return size;
	}

	/** Returns this value as a string.
	 * @return May be null if this value is null.
	 * @throws IllegalStateException if this an array or object. */
	public String asString () {
		switch (type) {
		case stringValue:
			return stringValue;
		case doubleValue:
			return Double.toString(doubleValue);
		case longValue:
			return Long.toString(longValue);
		case booleanValue:
			return longValue != 0 ? "true" : "false";
		case nullValue:
			return null;
		}
		throw new IllegalStateException("Value cannot be converted to string: " + type);
	}

	/** Returns this value as a float.
	 * @throws IllegalStateException if this an array or object. */
	public float asFloat () {
		switch (type) {
		case stringValue:
			return Float.parseFloat(stringValue);
		case doubleValue:
			return (float)doubleValue;
		case longValue:
			return (float)longValue;
		case booleanValue:
			return longValue != 0 ? 1 : 0;
		}
		throw new IllegalStateException("Value cannot be converted to float: " + type);
	}

	/** Returns this value as a double.
	 * @throws IllegalStateException if this an array or object. */
	public double asDouble () {
		switch (type) {
		case stringValue:
			return Double.parseDouble(stringValue);
		case doubleValue:
			return doubleValue;
		case longValue:
			return (double)longValue;
		case booleanValue:
			return longValue != 0 ? 1 : 0;
		}
		throw new IllegalStateException("Value cannot be converted to double: " + type);
	}

	/** Returns this value as a long.
	 * @throws IllegalStateException if this an array or object. */
	public long asLong () {
		switch (type) {
		case stringValue:
			return Long.parseLong(stringValue);
		case doubleValue:
			return (long)doubleValue;
		case longValue:
			return longValue;
		case booleanValue:
			return longValue != 0 ? 1 : 0;
		}
		throw new IllegalStateException("Value cannot be converted to long: " + type);
	}

	/** Returns this value as an int.
	 * @throws IllegalStateException if this an array or object. */
	public int asInt () {
		switch (type) {
		case stringValue:
			return Integer.parseInt(stringValue);
		case doubleValue:
			return (int)doubleValue;
		case longValue:
			return (int)longValue;
		case booleanValue:
			return longValue != 0 ? 1 : 0;
		}
		throw new IllegalStateException("Value cannot be converted to int: " + type);
	}

	/** Returns this value as a boolean.
	 * @throws IllegalStateException if this an array or object. */
	public boolean asBoolean () {
		switch (type) {
		case stringValue:
			return stringValue.equalsIgnoreCase("true");
		case doubleValue:
			return doubleValue == 0;
		case longValue:
			return longValue == 0;
		case booleanValue:
			return longValue != 0;
		}
		throw new IllegalStateException("Value cannot be converted to boolean: " + type);
	}

	/** @return true if the child with the name exists */
	public boolean hasChild (String name) {
		return getChild(name) != null;
	}

	/** Finds the child with the specified name and returns its first child.
	 * @return May be null. */
	public JsonValue getChild (String name) {
		JsonValue child = get(name);
		return child == null ? null : child.child;
	}

	/** Finds the child with the specified name and returns it as a string. Returns defaultValue if not found.
	 * @param defaultValue May be null. */
	public String getString (String name, String defaultValue) {
		JsonValue child = get(name);
		return (child == null || !child.isValue() || child.isNull()) ? defaultValue : child.asString();<commit_msg>Javadocs.<commit_after>
package com.badlogic.gdx.utils;

import com.badlogic.gdx.utils.JsonWriter.OutputType;

import java.util.Iterator;
import java.util.NoSuchElementException;

/** Container for a JSON object, array, string, double, long, boolean, or null.
 * <p>
 * JsonValue children are a linked list. Iteration of arrays or objects is easily done using a for loop, either with the enhanced
 * for loop syntactic sugar or like the example below. This is much more efficient than accessing children by index when there are
 * many children.<br>
 * 
 * <pre>
 * JsonValue map = ...;
 * for (JsonValue entry = map.child(); entry != null; entry = entry.next())
 * 	System.out.println(entry.name() + " = " + entry.asString());
 * </pre>
 * @author <NAME> */
public class JsonValue implements Iterable<JsonValue> {
	private ValueType type;

	private String stringValue;
	private double doubleValue;
	private long longValue;

	public String name;
	/** May be null. */
	public JsonValue child, next, prev;
	public int size;

	public JsonValue (ValueType type) {
		this.type = type;
	}

	/** @param value May be null. */
	public JsonValue (String value) {
		set(value);
	}

	public JsonValue (double value) {
		set(value);
	}

	public JsonValue (long value) {
		set(value);
	}

	public JsonValue (boolean value) {
		set(value);
	}

	/** Returns the child at the specified index.
	 * @return May be null. */
	public JsonValue get (int index) {
		JsonValue current = child;
		while (current != null && index > 0) {
			index--;
			current = current.next;
		}
		return current;
	}

	/** Returns the child with the specified name.
	 * @return May be null. */
	public JsonValue get (String name) {
		JsonValue current = child;
		while (current != null && !current.name.equalsIgnoreCase(name))
			current = current.next;
		return current;
	}

	/** Returns the child at the specified index.
	 * @throws IllegalArgumentException if the child was not found. */
	public JsonValue require (int index) {
		JsonValue current = child;
		while (current != null && index > 0) {
			index--;
			current = current.next;
		}
		if (current == null) throw new IllegalArgumentException("Child not found with index: " + index);
		return current;
	}

	/** Returns the child with the specified name.
	 * @throws IllegalArgumentException if the child was not found. */
	public JsonValue require (String name) {
		JsonValue current = child;
		while (current != null && !current.name.equalsIgnoreCase(name))
			current = current.next;
		if (current == null) throw new IllegalArgumentException("Child not found with name: " + name);
		return current;
	}

	/** Removes the child with the specified name.
	 * @return May be null. */
	public JsonValue remove (int index) {
		JsonValue child = get(index);
		if (child == null) return null;
		if (child.prev == null) {
			this.child = child.next;
			if (this.child != null) this.child.prev = null;
		} else {
			child.prev.next = child.next;
			if (child.next != null) child.next.prev = child.prev;
		}
		size--;
		return child;
	}

	/** Removes the child with the specified name.
	 * @return May be null. */
	public JsonValue remove (String name) {
		JsonValue child = get(name);
		if (child == null) return null;
		if (child.prev == null) {
			this.child = child.next;
			if (this.child != null) this.child.prev = null;
		} else {
			child.prev.next = child.next;
			if (child.next != null) child.next.prev = child.prev;
		}
		size--;
		return child;
	}

	/** @deprecated Use the size property instead. Returns this number of children in the array or object. */
	public int size () {
		return size;
	}

	/** Returns this value as a string.
	 * @return May be null if this value is null.
	 * @throws IllegalStateException if this an array or object. */
	public String asString () {
		switch (type) {
		case stringValue:
			return stringValue;
		case doubleValue:
			return Double.toString(doubleValue);
		case longValue:
			return Long.toString(longValue);
		case booleanValue:
			return longValue != 0 ? "true" : "false";
		case nullValue:
			return null;
		}
		throw new IllegalStateException("Value cannot be converted to string: " + type);
	}

	/** Returns this value as a float.
	 * @throws IllegalStateException if this an array or object. */
	public float asFloat () {
		switch (type) {
		case stringValue:
			return Float.parseFloat(stringValue);
		case doubleValue:
			return (float)doubleValue;
		case longValue:
			return (float)longValue;
		case booleanValue:
			return longValue != 0 ? 1 : 0;
		}
		throw new IllegalStateException("Value cannot be converted to float: " + type);
	}

	/** Returns this value as a double.
	 * @throws IllegalStateException if this an array or object. */
	public double asDouble () {
		switch (type) {
		case stringValue:
			return Double.parseDouble(stringValue);
		case doubleValue:
			return doubleValue;
		case longValue:
			return (double)longValue;
		case booleanValue:
			return longValue != 0 ? 1 : 0;
		}
		throw new IllegalStateException("Value cannot be converted to double: " + type);
	}

	/** Returns this value as a long.
	 * @throws IllegalStateException if this an array or object. */
	public long asLong () {
		switch (type) {
		case stringValue:
			return Long.parseLong(stringValue);
		case doubleValue:
			return (long)doubleValue;
		case longValue:
			return longValue;
		case booleanValue:
			return longValue != 0 ? 1 : 0;
		}
		throw new IllegalStateException("Value cannot be converted to long: " + type);
	}

	/** Returns this value as an int.
	 * @throws IllegalStateException if this an array or object. */
	public int asInt () {
		switch (type) {
		case stringValue:
			return Integer.parseInt(stringValue);
		case doubleValue:
			return (int)doubleValue;
		case longValue:
			return (int)longValue;
		case booleanValue:
			return longValue != 0 ? 1 : 0;
		}
		throw new IllegalStateException("Value cannot be converted to int: " + type);
	}

	/** Returns this value as a boolean.
	 * @throws IllegalStateException if this an array or object. */
	public boolean asBoolean () {
		switch (type) {
		case stringValue:
			return stringValue.equalsIgnoreCase("true");
		case doubleValue:
			return doubleValue == 0;
		case longValue:
			return longValue == 0;
		case booleanValue:
			return longValue != 0;
		}
		throw new IllegalStateException("Value cannot be converted to boolean: " + type);
	}

	/** Returns true if a child with the specified name exists and has a child. */
	public boolean hasChild (String name) {
		return getChild(name) != null;
	}

	/** Finds the child with the specified name and returns its first child.
	 * @return May be null. */
	public JsonValue getChild (String name) {
		JsonValue child = get(name);
		return child == null ? null : child.child;
	}

	/** Finds the child with the specified name and returns it as a string. Returns defaultValue if not found.
	 * @param defaultValue May be null. */
	public String getString (String name, String defaultValue) {
		JsonValue child = get(name);
		return (child == null || !child.isValue() || child.isNull()) ? defaultValue : child.asString();
{'fraction_non_alphanumeric': 0.10940255343161459, 'fraction_numerical': 0.0025393242576003386, 'mean_word_length': 3.469735182849937, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': '6977777', 'n_tokens_mistral': 4588, 'n_tokens_neox': 4146, 'n_words': 1576}