<commit_before>/* 
 * Copyright 2014 <NAME> (http://www.igormaznitsa.com).
 *
 * 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 com.igormaznitsa.jbbp.utils;

import com.igormaznitsa.jbbp.io.*;
import com.igormaznitsa.jbbp.model.JBBPAbstractField;
import java.io.*;
import java.lang.reflect.AccessibleObject;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;

/**
 * Misc auxiliary methods to be used in the framework.
 *
 * @since 1.0
 */
public enum JBBPUtils {

  ;

  /**
   * Inside auxiliary class to make makeAccessible as a privileged action.
   * @since 1.1
   */
  private static final class PrivilegedProcessor implements PrivilegedAction<AccessibleObject> {

    private AccessibleObject theObject;

    public void setAccessibleObject(final AccessibleObject obj) {
      this.theObject = obj;
    }

    public AccessibleObject run() {
      final AccessibleObject objectToProcess = this.theObject;
      this.theObject = null;
      if (objectToProcess != null) {
        objectToProcess.setAccessible(true);
      }
      return objectToProcess;
    }
  }

  /**
   * Thread local storage for privileged processors.
   *
   * @since 1.1
   */
  private static final ThreadLocal<PrivilegedProcessor> privelegedProcessors = new ThreadLocal<PrivilegedProcessor>() {
    @Override
    protected PrivilegedProcessor initialValue() {
      return new PrivilegedProcessor();
    }
  };

  /**
   * Make accessible an accessible object, AccessController.doPrivileged will be
   * called.
   *
   * @param obj an object to make accessible, it can be null.
   * @since 1.1
   * @see AccessController#doPrivileged(java.security.PrivilegedAction) 
   */
  public static void makeAccessible(final AccessibleObject obj) {
    if (obj != null) {
      final PrivilegedProcessor processor = privelegedProcessors.get();
      processor.setAccessibleObject(obj);
      AccessController.doPrivileged(processor);
    }
  }

  /**
   * Check that a string is a number.
   *
   * @param num a string to be checked, it can be null
   * @return true if the string represents a number, false if it is not number
   * or it is null
   */
  public static boolean isNumber(final String num) {<commit_msg>replaced ThreadLocal by Queue in JBBPUtils<commit_after>/* 
 * Copyright 2014 <NAME> (http://www.igormaznitsa.com).
 *
 * 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 com.igormaznitsa.jbbp.utils;

import com.igormaznitsa.jbbp.io.*;
import com.igormaznitsa.jbbp.model.JBBPAbstractField;
import java.io.*;
import java.lang.reflect.AccessibleObject;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * Misc auxiliary methods to be used in the framework.
 *
 * @since 1.0
 */
public enum JBBPUtils {

  ;

  /**
   * Inside auxiliary class to make makeAccessible as a privileged action.
   * @since 1.1
   */
  private static final class PrivilegedProcessor implements PrivilegedAction<AccessibleObject> {

    private AccessibleObject theObject;

    public void setAccessibleObject(final AccessibleObject obj) {
      this.theObject = obj;
    }

    public AccessibleObject run() {
      final AccessibleObject objectToProcess = this.theObject;
      this.theObject = null;
      if (objectToProcess != null) {
        objectToProcess.setAccessible(true);
      }
      return objectToProcess;
    }
  }

  /**
   * Inside auxiliary queue for privileged processors to avoid mass creation of processors.
   * @since 1.1
   */
  private static final Queue<PrivilegedProcessor> processorsQueue = new ArrayBlockingQueue<PrivilegedProcessor>(32);
  
  /**
   * Make accessible an accessible object, AccessController.doPrivileged will be
   * called.
   *
   * @param obj an object to make accessible, it can be null.
   * @since 1.1
   * @see AccessController#doPrivileged(java.security.PrivilegedAction) 
   */
  public static void makeAccessible(final AccessibleObject obj) {
    if (obj != null) {
      PrivilegedProcessor processor = processorsQueue.poll();
      if (processor==null){
        processor = new PrivilegedProcessor();
      }
      processor.setAccessibleObject(obj);
      AccessController.doPrivileged(processor);
      processorsQueue.offer(processor);
    }
  }

  /**
   * Check that a string is a number.
   *
   * @param num a string to be checked, it can be null
   * @return true if the string represents a number, false if it is not number
   * or it is null
   */
  public static boolean isNumber(final String num) {
{'fraction_non_alphanumeric': 0.08595835557928456, 'fraction_numerical': 0.00605089873642997, 'mean_word_length': 3.425196850393701, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1886339', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1578, 'n_words': 531}