<commit_before>package com.github.sbouclier.jaads;

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

/**
 * A node can have child nodes. A node without children is called 'leaf'. A node
 * without parent node is called 'root'
 * 
 * @author <NAME>
 *
 * @param <T>
 *            type of node value
 */
public class Node<T> {
	public static short MAX_DEGREE = Short.MAX_VALUE;

	protected final int maxDegree;
	protected T value;
	protected List<Node<T>> children = new ArrayList<Node<T>>();

	// ----------------
	// - CONSTRUCTORS -
	// ----------------

	public Node(T value, int maxDegree) {
		this.value = value;
		this.maxDegree = maxDegree;
	}

	public Node(T value) {
		this(value, MAX_DEGREE);
	}

	// -----------
	// - METHODS -
	// -----------

	public void addChild(Node<T> node) {
		synchronized (this.children) {
			if (this.children.size() == maxDegree) {
				throw new NodeOutOfBoundException(maxDegree);
			} else {
				this.children.add(node);
			}
		}
	}

	public Node<T> getChild(int index) {
		return children.get(index);
	}

	public int getDegree() {
		if (children.isEmpty()) {
			return 0;
		} else {
			return (int) children.stream().filter(n -> n != null).count();
		}
	}

	public boolean isLeaf() {
		if (children.isEmpty()) {
			return true;
		} else {
			return !(children.stream().filter(n -> n != null).count() > 0);
		}
	}

	// -------------
	// - TO STRING -
	// -------------<commit_msg>optimize getDegree and isLeaf<commit_after>package com.github.sbouclier.jaads;

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

/**
 * A node can have child nodes. A node without children is called 'leaf'. A node
 * without parent node is called 'root'
 * 
 * @author <NAME>
 *
 * @param <T>
 *            type of node value
 */
public class Node<T> {
	public static short MAX_DEGREE = Short.MAX_VALUE;

	protected final int maxDegree;
	protected T value;
	protected List<Node<T>> children = new ArrayList<Node<T>>();

	// ----------------
	// - CONSTRUCTORS -
	// ----------------

	public Node(T value, int maxDegree) {
		this.value = value;
		this.maxDegree = maxDegree;
	}

	public Node(T value) {
		this(value, MAX_DEGREE);
	}

	// -----------
	// - METHODS -
	// -----------

	public void addChild(Node<T> node) {
		synchronized (this.children) {
			if (this.children.size() == maxDegree) {
				throw new NodeOutOfBoundException(maxDegree);
			} else {
				this.children.add(node);
			}
		}
	}

	public Node<T> getChild(int index) {
		return children.get(index);
	}

	public int getDegree() {
		return (int) children.stream().filter(n -> n != null).count();
	}

	public boolean isLeaf() {
		return !(children.stream().filter(n -> n != null).count() > 0);
	}

	// -------------
	// - TO STRING -
	// -------------
{'fraction_non_alphanumeric': 0.1989071038251366, 'fraction_numerical': 0.001092896174863388, 'mean_word_length': 2.340632603406326, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 27, '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': '5622784', 'n_tokens_mistral': 1019, 'n_tokens_neox': 924, 'n_words': 231}