<commit_before>package org.scalacoin.util

import org.scalacoin.protocol.{VarIntImpl, VarInt}
import org.slf4j.LoggerFactory

/**
 * Created by chris on 2/8/16.
 */
trait NumberUtil {


  private def logger = LoggerFactory.getLogger(this.getClass())

  def toLong(hex : String) : Long = toLong(ScalacoinUtil.decodeHex(hex))

  def toLong(bytes : List[Byte]) : Long = {
    logger.debug("bytes: " + bytes)
    val reversedBytes = bytes.reverse
    if (bytes.size == 1 && bytes.head == -128) {
      //the case for negative zero
      0
    } else if (isPositive(bytes)) {
      if (firstByteAllZeros(reversedBytes) && reversedBytes.size > 1) {
        parseLong(reversedBytes.slice(1,reversedBytes.size))
      } else parseLong(reversedBytes)
    } else {
      //remove the sign bit
      val removedSignBit : List[Byte] = changeSignBitToPositive(reversedBytes)
      if (firstByteAllZeros(removedSignBit)) -parseLong(removedSignBit.slice(1,removedSignBit.size))
      else -parseLong(removedSignBit)
    }
  }


  def longToHex(long : Long) : String = {
    if (long > -1) {
      val bytes = toByteList(long)
      ScalacoinUtil.encodeHex(bytes)
    } else {
      val bytes = toByteList(long.abs)
      //add sign bit
      val negativeNumberBytes : List[Byte] = changeSignBitToNegative(bytes)
      val hex = ScalacoinUtil.encodeHex(negativeNumberBytes.reverse)
      hex
    }
  }

  /**
   * Determines if a given hex string is a positive number
   * @param hex
   * @return
   */
  def isPositive(hex : String) : Boolean = isPositive(ScalacoinUtil.decodeHex(hex))

  /**
   * Determines if a byte array is a positive or negative number
   * @param bytes<commit_msg>Adding some scaladocs for util functions<commit_after>package org.scalacoin.util

import org.scalacoin.protocol.{VarIntImpl, VarInt}
import org.slf4j.LoggerFactory

/**
 * Created by chris on 2/8/16.
 */
trait NumberUtil {


  private def logger = LoggerFactory.getLogger(this.getClass())


  /**
   * Takes a hex number and converts it into a signed number
   * used in the bitcoin numbering system
   * @param hex
   * @return
   */
  def toLong(hex : String) : Long = toLong(ScalacoinUtil.decodeHex(hex))

  /**
   * Takes a list of bytes and converts it in to signed number inside of bitcoins
   * numbering system
   * @param bytes
   * @return
   */
  def toLong(bytes : List[Byte]) : Long = {
    logger.debug("bytes: " + bytes)
    val reversedBytes = bytes.reverse
    if (bytes.size == 1 && bytes.head == -128) {
      //the case for negative zero
      0
    } else if (isPositive(bytes)) {
      if (firstByteAllZeros(reversedBytes) && reversedBytes.size > 1) {
        parseLong(reversedBytes.slice(1,reversedBytes.size))
      } else parseLong(reversedBytes)
    } else {
      //remove the sign bit
      val removedSignBit : List[Byte] = changeSignBitToPositive(reversedBytes)
      if (firstByteAllZeros(removedSignBit)) -parseLong(removedSignBit.slice(1,removedSignBit.size))
      else -parseLong(removedSignBit)
    }
  }


  /**
   * Converts a long number to a signed number hex representation
   * @param long
   * @return
   */
  def longToHex(long : Long) : String = {
    if (long > -1) {
      val bytes = toByteList(long)
      ScalacoinUtil.encodeHex(bytes)
    } else {
      val bytes = toByteList(long.abs)
      //add sign bit
      val negativeNumberBytes : List[Byte] = changeSignBitToNegative(bytes)
      val hex = ScalacoinUtil.encodeHex(negativeNumberBytes.reverse)
      hex
    }
  }

  /**
   * Determines if a given hex string is a positive number
   * @param hex
   * @return
   */
  def isPositive(hex : String) : Boolean = isPositive(ScalacoinUtil.decodeHex(hex))

  /**
   * Determines if a byte array is a positive or negative number
   * @param bytes
{'fraction_non_alphanumeric': 0.10766374966852293, 'fraction_numerical': 0.007425086184036065, 'mean_word_length': 3.017039403620873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6882893', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1187, 'n_words': 305}