<commit_before>package concrete;

import java.lang.reflect.Field
import scala.xml.NodeSeq
import java.util.Properties
import scala.collection.JavaConversions
import java.security.InvalidParameterException
import scala.collection.mutable.HashMap

/**
 * This class is intended to hold CSP4J's various parameters.
 *
 * @author vion
 *
 */
final object ParameterManager {

  private val parameters: HashMap[String, (Any, Field)] = new HashMap()
  private val pending: HashMap[String, Any] = new HashMap()
  private val pendingParse: HashMap[String, String] = new HashMap()

  def register(o: AnyRef) {
    require(!o.isInstanceOf[Class[_]])
    for (f <- o.getClass.getDeclaredFields) {
      val param = f.getAnnotation(classOf[concrete.Parameter]);
      if (param != null) {
        val name = param.value
        parameters += name -> (o, f)
        f.setAccessible(true);
        for (value <- pending.get(name)) {
          f.set(o, value)
          pending -= name
        }
        for (s <- pendingParse.get(name)) {
          f.set(o, parse(f, s))
          pendingParse -= name
        }
      }
    }
  }

  /**
   * Updates some parameter, overriding default or previous value.
   *
   * @param name
   * @param value
   */
  def update(name: String, value: Any) {
    parameters.get(name) match {
      case None => pending += name -> value
      case Some((o, f)) => f.set(o, value)
    }
  }

  private def parse(field: Field, value: String): Any = {
    val fType = field.getType

    if (fType.isAssignableFrom(classOf[Int])) {
      value.toInt
    } else if (fType.isAssignableFrom(classOf[Double])) {
      value.toDouble
    } else if (fType.isAssignableFrom(classOf[String])) {
      value.toString
    } else if (fType.isAssignableFrom(classOf[Class[_]])) {
      Class.forName(value)
    } else {
      throw new IllegalArgumentException(s"Cannot parse $field of type $fType")
    }

  }

  /**
   * Parses some parameter as a String, and converts it automatically using
   * given registered type.<commit_msg>Added accounting for boolean parameters<commit_after>package concrete;

import java.lang.reflect.Field
import scala.xml.NodeSeq
import java.util.Properties
import scala.collection.JavaConversions
import java.security.InvalidParameterException
import scala.collection.mutable.HashMap

/**
 * This class is intended to hold CSP4J's various parameters.
 *
 * @author vion
 *
 */
final object ParameterManager {

  private val parameters: HashMap[String, (Any, Field)] = new HashMap()
  private val pending: HashMap[String, Any] = new HashMap()
  private val pendingParse: HashMap[String, String] = new HashMap()

  def register(o: AnyRef) {
    require(!o.isInstanceOf[Class[_]])
    for (f <- o.getClass.getDeclaredFields) {
      val param = f.getAnnotation(classOf[concrete.Parameter]);
      if (param != null) {
        val name = param.value
        parameters += name -> (o, f)
        f.setAccessible(true);
        for (value <- pending.get(name)) {
          f.set(o, value)
          pending -= name
        }
        for (s <- pendingParse.get(name)) {
          f.set(o, parse(f, s))
          pendingParse -= name
        }
      }
    }
  }

  /**
   * Updates some parameter, overriding default or previous value.
   *
   * @param name
   * @param value
   */
  def update(name: String, value: Any) {
    parameters.get(name) match {
      case None => pending += name -> value
      case Some((o, f)) => f.set(o, value)
    }
  }

  private def parse(field: Field, value: String): Any = {
    val fType = field.getType

    if (fType.isAssignableFrom(classOf[Int])) {
      value.toInt
    } else if (fType.isAssignableFrom(classOf[Boolean])) {
      value.toBoolean
    } else if (fType.isAssignableFrom(classOf[Double])) {
      value.toDouble
    } else if (fType.isAssignableFrom(classOf[String])) {
      value.toString
    } else if (fType.isAssignableFrom(classOf[Class[_]])) {
      Class.forName(value)
    } else {
      throw new IllegalArgumentException(s"Cannot parse $field of type $fType")
    }

  }

  /**
   * Parses some parameter as a String, and converts it automatically using
   * given registered type.
{'fraction_non_alphanumeric': 0.12125569134914929, 'fraction_numerical': 0.0004792715073088905, 'mean_word_length': 2.8188472095150963, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1553572', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1257, 'n_words': 333}