Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4027176
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: Java

2012-12-05 14:40:31

AgiString.scala
  1. import java.io._
  2. import java.nio.channels._
  3. import java.nio._
  4. import java.net.{ URLEncoder, URLDecoder }
  5. import com.spreada.utils.chinese.ZHConverter
  6. import scala.collection.mutable.HashMap

  7. case class AgiString(value : String) {

  8.   private val base64encoder = new sun.misc.BASE64Encoder
  9.   private val base64decoder = new sun.misc.BASE64Decoder

  10.   private val desRegister = HashMap[String, Des]()
  11.   private val systemEncode = System.getProperty("file.encoding")

  12.   private def trueIndexOf(index : Int) : Int = {
  13.     val length = value.length
  14.     if (index < 0)
  15.       index + length
  16.     else if (index > length)
  17.       index - length
  18.     else
  19.       index
  20.   }

  21.   // 默认的urlencode按照UTF8
  22.   def encode : String = URLEncoder.encode(value, "UTF8")
  23.   def encode(enc : String) : String = URLEncoder.encode(value, enc)
  24.   def decode : String = URLDecoder.decode(value, "UTF8")
  25.   def decode(enc : String) : String = URLDecoder.decode(value, enc)
  26.   // 根据系统默认的编码进行encode,用于gui开发使用
  27.   def encodeSys : String = URLEncoder.encode(value, systemEncode)
  28.   def decodeSys : String = URLDecoder.decode(value, systemEncode)

  29.   def base64encode : String = base64encoder.encode(value.getBytes)
  30.   def base64decode : String = new String(base64decoder.decodeBuffer(value))

  31.   // 简繁转换
  32.   // 基于ZhConverter:
  33.   def toTraditional : String = ZHConverter.convert(value, ZHConverter.TRADITIONAL)
  34.   def toSimplified : String = ZHConverter.convert(value, ZHConverter.SIMPLIFIED)

  35.   // 获取DesUtil实例
  36.   private def getDesUtil(key : String) : Des = {
  37.     if (!desRegister.contains(key))
  38.       desRegister(key) = new Des(key)
  39.     desRegister(key)
  40.   }

  41.   // Des加密
  42.   def desEncrypt(key : String) : String = {
  43.     new String(getDesUtil(key).DesEncrypt(value.getBytes, 1))
  44.   }
  45.   def desEncryptBase64(key : String) : String = {
  46.     base64encoder.encode(getDesUtil(key).DesEncrypt(value.getBytes, 1))
  47.   }

  48.   // Des解密
  49.   def desDecrypt(key : String) : String = new String(getDesUtil(key).DesEncrypt(new String(value.getBytes, "utf8").getBytes, 0))
  50.   def desDecryptBase64(key : String) : String = new String(getDesUtil(key).DesEncrypt(base64decoder.decodeBuffer(value), 0))

  51.   // 驼峰命名
  52.   def toCamelCase : String = toCamelCase(true)
  53.   def toCamelCase(isFirstUpper : Boolean = true) : String = {
  54.     val b = new StringBuilder
  55.     var toUpperPos = -1
  56.     for (i <- 0.to(value.length - 1)) {
  57.       if (!value(i).isLetterOrDigit)
  58.         toUpperPos = i + 1
  59.       else
  60.         b.append(if (toUpperPos == i) value(i).toUpperCase else value(i))
  61.     }
  62.     val first = b.charAt(0)
  63.     if (isFirstUpper)
  64.       if (!first.isUpperCase)
  65.         b.setCharAt(0, first.toUpperCase)
  66.       else if (!first.isLowerCase)
  67.         b.setCharAt(0, first.toLowerCase)
  68.     b.mkString
  69.   }

  70.   def toUnCamcelCase(implicit sp : String = "_") : String = {
  71.     val b = new StringBuilder
  72.     var pos = -1
  73.     value.foreach { char =>
  74.       pos += 1
  75.       if (char.isLetterOrDigit) {
  76.         if (char.isUpperCase)
  77.           (if (pos == 0) b else b.append(sp)).append(char.toLowerCase)
  78.         else
  79.           b.append(char)
  80.       }
  81.     }
  82.     b.mkString
  83.   }

  84.   // 指定位置大写
  85.   def toUpperCase(index : Int) : String = {
  86.     val trueIndex = trueIndexOf(index)
  87.     val char = value.charAt(trueIndex)
  88.     if (!char.isUpper) {
  89.       value.updated(trueIndex, char.toUpperCase).mkString
  90.     } else
  91.       value
  92.   }
  93.   // 指定位置小写
  94.   def toLowerCase(index : Int) : String = {
  95.     val trueIndex = trueIndexOf(index)
  96.     val char = value.charAt(trueIndex)
  97.     if (!char.isLower) {
  98.       value.updated(trueIndex, char.toLowerCase).mkString
  99.     } else
  100.       value
  101.   }

  102.   def dump(file : File) : Boolean = {
  103.     try {
  104.       val opChannel = new FileOutputStream(file).getChannel
  105.       opChannel.write(ByteBuffer.wrap(value.getBytes))
  106.       opChannel.close
  107.       true
  108.     } catch {
  109.       case _ =>
  110.         System.err.println("Can't dump I/O error!")
  111.         false
  112.     }
  113.   }
  114.   def dump(name : String) : Boolean = dump(new File(name))

  115.   lazy private val md5handle = java.security.MessageDigest.getInstance("MD5")
  116.   private val hexDigits = Array[Char]('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')

  117.   def md5 : String = {
  118.     val encrypt = md5handle.digest(value.getBytes)
  119.     val b = new StringBuilder(32)
  120.     for (i <- 0.to(15)) {
  121.       b.append(hexDigits(encrypt(i) >>> 4 & 0xf)).append(hexDigits(encrypt(i) & 0xf))
  122.     }
  123.     b.mkString
  124.   }

  125.   def jsonDecode : JsValue = JsValue.fromString(value)

  126.   def toEventName : String = {
  127.     val name = value.toLowerCase()
  128.     if (name.indexOf("on") == 0)
  129.       name.replace("on", "")
  130.     else
  131.       name
  132.   }
  133. }

Base.scala
  1. import com.agiers.util._
  2. import org.apache.log4j.Logger

  3. trait Base {
  4.   // String extends
  5.   implicit def AgiStringExtend(value: String) = AgiString(value)

  6.   def log = Logger.getLogger(getClass)

  7.   def currentTime(isMillis: Boolean): Long = {
  8.     if (isMillis)
  9.       System.currentTimeMillis
  10.     else
  11.       currentTime
  12.   }

  13.   def currentTime(): Long = {
  14.     System.currentTimeMillis
  15.   }

  16.   def random(max: Int): Long = {
  17.     (Math.random * (max + 1)).toLong
  18. // (randomSeed.nextInt >>> 1) & max
  19.   }

  20.   def random(min: Int, max: Int): Long = {
  21.     min + random(max - min)
  22.   }
  23. }
阅读(21011) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~