<commit_before>/**
 * Copyright (c) 2015 Lemur Consulting Ltd.
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 uk.co.flax.biosolr;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;

/**
 * POJO representing an entry in the hierarchical facet tree.
 *
 * <p>Implements Comparable, so that entries in the tree may be ordered by their
 * value count.</p>
 */
public class TreeFacetField implements Comparable<TreeFacetField>, Serializable {

	private static final long serialVersionUID = 5709339278691781478L;

	private static final String LABEL_KEY = "label";
	private static final String VALUE_KEY = "value";
	private static final String COUNT_KEY = "count";
	private static final String TOTAL_KEY = "total";
	private static final String HIERARCHY_KEY = "hierarchy";

	private final String label;
	private final String value;
	private final long count;
	private final long childCount;
	private final Set<TreeFacetField> hierarchy;

	/**
	 * Construct a new TreeFacetField node.
	 * @param label the label for the node (optional).
	 * @param value the facet value.
	 * @param count the actual facet count for this node.
	 * @param childCount the total count for facets which are children of this
	 * node.
	 * @param hierarchy the set of nodes which comprise the children of this node.
	 */
	public TreeFacetField(String label, String value, long count, long childCount, Set<TreeFacetField> hierarchy) {
		this.label = label;
		this.value = value;
		this.count = count;
		this.childCount = childCount;
		this.hierarchy = hierarchy;
	}

	public String getValue() {
		return value;
	}

	public long getCount() {
		return count;
	}

	public long getChildCount() {
		return childCount;
	}

	public long getTotal() {
		return count + childCount;
	}

	public Set<TreeFacetField> getHierarchy() {
		return hierarchy;
	}

	public boolean hasChildren() {
		return hierarchy != null && hierarchy.size() > 0;
	}

	@Override
	public int compareTo(TreeFacetField o) {
		int ret = 0;

		if (o == null) {
			ret = 1;
		} else {
			ret = (int) (getTotal() - o.getTotal());
			if (ret == 0) {
				// If the totals are the same, compare the count as well.
				ret = (int) (count - o.count);
				if (ret == 0) {
					// If the counts are also the same, compare the ID as well, to double-check
					// whether they're actually the same entry
					ret = getValue().compareTo(o.getValue());
				}<commit_msg>Add method to recalculate child node.<commit_after>/**
 * Copyright (c) 2015 Lemur Consulting Ltd.
 * <p/>
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 uk.co.flax.biosolr;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;

/**
 * POJO representing an entry in the hierarchical facet tree.
 *
 * <p>Implements Comparable, so that entries in the tree may be ordered by their
 * value count.</p>
 */
public class TreeFacetField implements Comparable<TreeFacetField>, Serializable {

	private static final long serialVersionUID = 5709339278691781478L;

	private static final String LABEL_KEY = "label";
	private static final String VALUE_KEY = "value";
	private static final String COUNT_KEY = "count";
	private static final String TOTAL_KEY = "total";
	private static final String HIERARCHY_KEY = "hierarchy";

	private final String label;
	private final String value;
	private final long count;
	private long childCount;
	private final Set<TreeFacetField> hierarchy;

	/**
	 * Construct a new TreeFacetField node.
	 * @param label the label for the node (optional).
	 * @param value the facet value.
	 * @param count the actual facet count for this node.
	 * @param childCount the total count for facets which are children of this
	 * node.
	 * @param hierarchy the set of nodes which comprise the children of this node.
	 */
	public TreeFacetField(String label, String value, long count, long childCount, Set<TreeFacetField> hierarchy) {
		this.label = label;
		this.value = value;
		this.count = count;
		this.childCount = childCount;
		this.hierarchy = hierarchy;
	}

	public String getValue() {
		return value;
	}

	public long getCount() {
		return count;
	}

	public long getChildCount() {
		return childCount;
	}

	public long getTotal() {
		return count + childCount;
	}

	public Set<TreeFacetField> getHierarchy() {
		return hierarchy;
	}

	public boolean hasChildren() {
		return hierarchy != null && hierarchy.size() > 0;
	}
	
	public void recalculateChildCount() {
		long cCount = 0;
		
		if (hasChildren()) {
			for (TreeFacetField childNode : hierarchy) {
				childNode.recalculateChildCount();
				cCount += childNode.getChildCount();
			}
		}
		
		this.childCount = cCount;
	}

	@Override
	public int compareTo(TreeFacetField o) {
		int ret = 0;

		if (o == null) {
			ret = 1;
		} else {
			ret = (int) (getTotal() - o.getTotal());
			if (ret == 0) {
				// If the totals are the same, compare the count as well.
				ret = (int) (count - o.count);
				if (ret == 0) {
					// If the counts are also the same, compare the ID as well, to double-check
					// whether they're actually the same entry
					ret = getValue().compareTo(o.getValue());
				}
{'fraction_non_alphanumeric': 0.09679398680779261, 'fraction_numerical': 0.009970854425525387, 'mean_word_length': 3.6974063400576367, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '7062923', 'n_tokens_mistral': 2123, 'n_tokens_neox': 1900, 'n_words': 739}