<commit_before>package org.apache.lucene.search.trie;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import java.io.IOException;
import java.util.Date;

import org.apache.lucene.search.Filter;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.util.OpenBitSet;

/**
 * Implementation of a Lucene {@link Filter} that implements trie-based range filtering.
 * This filter depends on a specific structure of terms in the index that can only be created
 * by {@link TrieUtils} methods.
 * For more information, how the algorithm works, see the package description {@link org.apache.lucene.search.trie}.
 */
public final class TrieRangeFilter extends Filter {

  /**
   * Universal constructor (expert use only): Uses already trie-converted min/max values.
   * You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
   * With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
   * bound should be included or excluded from the range.
   */
  public TrieRangeFilter(final String field, String min, String max,
    final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
  ) {
    if (min==null && max==null) throw new IllegalArgumentException("The min and max values cannot be both null.");
    this.trieVariant=variant;
    this.field=field.intern();
    // just for toString()
    this.minUnconverted=min;
    this.maxUnconverted=max;
    this.minInclusive=minInclusive;
    this.maxInclusive=maxInclusive;
    // encode bounds
    this.min=(min==null) ? trieVariant.TRIE_CODED_NUMERIC_MIN : (
      minInclusive ? min : variant.incrementTrieCoded(min)
    );
    this.max=(max==null) ? trieVariant.TRIE_CODED_NUMERIC_MAX : (
      maxInclusive ? max : variant.decrementTrieCoded(max)
    );
  }

  /**
   * Generates a trie filter using the supplied field with range bounds in numeric form (double).
   * You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
   * With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
   * bound should be included or excluded from the range.
   */
  public TrieRangeFilter(final String field, final Double min, final Double max,
    final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
  ) {
    this(
      field,
      (min==null) ? null : variant.doubleToTrieCoded(min.doubleValue()),
      (max==null) ? null : variant.doubleToTrieCoded(max.doubleValue()),
      minInclusive,
      maxInclusive,
      variant
    );<commit_msg>Add some extra check for validity of c'tor parameters in TrieRangeFilter<commit_after>package org.apache.lucene.search.trie;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import java.io.IOException;
import java.util.Date;

import org.apache.lucene.search.Filter;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.util.OpenBitSet;

/**
 * Implementation of a Lucene {@link Filter} that implements trie-based range filtering.
 * This filter depends on a specific structure of terms in the index that can only be created
 * by {@link TrieUtils} methods.
 * For more information, how the algorithm works, see the package description {@link org.apache.lucene.search.trie}.
 */
public final class TrieRangeFilter extends Filter {

  /**
   * Universal constructor (expert use only): Uses already trie-converted min/max values.
   * You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
   * With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
   * bound should be included or excluded from the range.
   */
  public TrieRangeFilter(final String field, String min, String max,
    final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
  ) {
    if (min==null && max==null) throw new IllegalArgumentException("The min and max values cannot be both null.");
    this.trieVariant=variant;
    this.field=field.intern();
    // just for toString()
    this.minUnconverted=min;
    this.maxUnconverted=max;
    this.minInclusive=minInclusive;
    this.maxInclusive=maxInclusive;
    // encode bounds
    this.min=(min==null) ? trieVariant.TRIE_CODED_NUMERIC_MIN : (
      minInclusive ? min : variant.incrementTrieCoded(min)
    );
    this.max=(max==null) ? trieVariant.TRIE_CODED_NUMERIC_MAX : (
      maxInclusive ? max : variant.decrementTrieCoded(max)
    );
    // check encoded values
    if (
      this.min.length() != trieVariant.TRIE_CODED_LENGTH ||
      this.max.length() != trieVariant.TRIE_CODED_LENGTH
    ) throw new NumberFormatException("Invalid trie encoded numerical value representation (incompatible length).");
  }

  /**
   * Generates a trie filter using the supplied field with range bounds in numeric form (double).
   * You can set <code>min</code> or <code>max</code> (but not both) to <code>null</code> to leave one bound open.
   * With <code>minInclusive</code> and <code>maxInclusive</code> can be choosen, if the corresponding
   * bound should be included or excluded from the range.
   */
  public TrieRangeFilter(final String field, final Double min, final Double max,
    final boolean minInclusive, final boolean maxInclusive, final TrieUtils variant
  ) {
    this(
      field,
      (min==null) ? null : variant.doubleToTrieCoded(min.doubleValue()),
      (max==null) ? null : variant.doubleToTrieCoded(max.doubleValue()),
      minInclusive,
      maxInclusive,
      variant
    );
{'fraction_non_alphanumeric': 0.09432185145317545, 'fraction_numerical': 0.001076426264800861, 'mean_word_length': 3.948735019973369, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '5631023', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2110, 'n_words': 750}