Skip to content

Commit

Permalink
✨ commit yaml/properties file utils && net related utils && string re…
Browse files Browse the repository at this point in the history
…gex ops
  • Loading branch information
bebee4java committed Jan 5, 2020
1 parent 557e5a7 commit 8c72822
Show file tree
Hide file tree
Showing 14 changed files with 569 additions and 4 deletions.
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tech.sqlclub</groupId>
<artifactId>common-utils</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>

<name>common utils</name>
<url>https://github.com/bebee4java/common-utils</url>
Expand Down Expand Up @@ -94,6 +94,18 @@
<version>2.9.9</version>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.25</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

</dependencies>


Expand Down
32 changes: 32 additions & 0 deletions src/main/java/tech/sqlclub/common/context/PropertiesContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tech.sqlclub.common.context

import java.io.{File, FileInputStream, InputStream}
import java.util.Properties

import tech.sqlclub.common.utils.{FileUtils, ParamMapUtils}

object PropertiesContext {
val resourcePath = this.getClass.getClassLoader.getResource("").getPath
lazy val properties = new Properties()

import scala.collection.JavaConversions._
private lazy val paramMap = {
getAllPropertyFile.map(loadPropertyFile _ )
properties.toMap[String, AnyRef]
}

def getAllPropertyFile = FileUtils.lsfile(resourcePath,".*\\.properties")

def loadPropertyFile(file:File)
(implicit inputStream:InputStream=new FileInputStream(file)) = {
try {
properties.load(inputStream)
} finally {
inputStream.close()
}
}


implicit class PropertiesContext_impl( _this_ : PropertiesContext.type ) extends ParamMapUtils(_this_.paramMap)

}
29 changes: 29 additions & 0 deletions src/main/java/tech/sqlclub/common/context/YamlContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tech.sqlclub.common.context

import java.io.{File, FileInputStream, InputStream}
import java.util

import org.yaml.snakeyaml.Yaml
import tech.sqlclub.common.utils.{FileUtils, ParamMapUtils}


object YamlContext {
val resourcePath = this.getClass.getClassLoader.getResource("").getPath
lazy val yaml = new Yaml()

import scala.collection.JavaConversions._
private lazy val paramMap = getAllYamlFile.flatMap(loadYamlFile[util.Map[String,Object]](_)).toMap

def getAllYamlFile = FileUtils.lsfile(resourcePath,".*\\.(yaml|yml)")

def loadYamlFile[A](file:File)
(implicit inputStream:InputStream=new FileInputStream(file)):A = {
try {
yaml.load[A](inputStream)
} finally {
inputStream.close()
}
}

implicit class YamlContext_impl( _this_ : YamlContext.type ) extends ParamMapUtils(_this_.paramMap)
}
74 changes: 74 additions & 0 deletions src/main/java/tech/sqlclub/common/net/NetUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package tech.sqlclub.common.net

import java.io.IOException
import java.net._
import java.util.concurrent.atomic.AtomicInteger

object NetUtils {

def getLocalServerIp: String = try {
java.net.InetAddress.getLocalHost.getHostAddress
} catch {
case e: UnknownHostException =>
e.printStackTrace()
null
}


def getLocalServerIpv4List: List[String] = {
try {
import scala.collection.JavaConversions._
NetworkInterface.getNetworkInterfaces.toList.filter(_.isUp)
.flatMap(_.getInetAddresses)
.filter {
iaddr =>
iaddr.isInstanceOf[Inet4Address] && !iaddr.isLoopbackAddress
}.map(_.getHostAddress)
} catch {
case e: Exception =>
e.printStackTrace()
List.empty[String]
}
}

def portAvailableAndReturn(hostname: String, port_min_number: Int, port_max_number: Int):Int = {
var bindSuccess: Boolean = false
var ss: Socket = null
var ss1: Socket = null
val start: AtomicInteger = new AtomicInteger(port_min_number)

while (!bindSuccess && start.get() <= port_max_number) {
try {
ss = new Socket()
ss.bind(new InetSocketAddress(hostname, start.get()))
ss.close()
if (hostname != "0.0.0.0") {
ss1 = new Socket()
ss1.bind(new InetSocketAddress("0.0.0.0", start.get()))
ss1.close()
}
bindSuccess = true
} catch {
case e:IOException =>
bindSuccess = false
start.set(start.get() + 1)
} finally {
socketClose(ss)
socketClose(ss1)
}
}
if (bindSuccess) start.get() else -1
}

def socketClose(s:Socket)= {
if (s != null && !s.isClosed){
try {
s.close()
} catch {
case e:IOException =>
e.printStackTrace()
}
}
}

}
75 changes: 75 additions & 0 deletions src/main/java/tech/sqlclub/common/regex/RegexOp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package tech.sqlclub.common.regex


object RegexOp {
implicit class RegexString(s:String) {
/**
* 判断字符串是否匹配正则
* @param pattern 正则表达式
* @return Boolean
*/
def matching(pattern:String):Boolean = (pattern.r findFirstIn s).isDefined


/**
* 正则匹配按组获取值
* @param pattern 正则表达式
* @param group 第几组
* @return Iterator[String]
*/
def matchGroup(pattern:String, group:Int):Iterator[String] = {
val matchs = pattern.r findAllMatchIn s
matchs.map(_.group(group))
}

/**
* 正则匹配所有值
* @param pattern 正则表达式
* @return List[String]
*/
def matchAll(pattern:String):List[String] = (pattern.r findAllIn s).toList

/**
* 正则匹配第一个值
* @param pattern 正则表达式
* @return Option[String]
*/
def matchFirst(pattern:String):Option[String] = pattern.r findFirstIn s


/**
* 判断字符串是否是数值格式
* @return Boolean
*/
def isMumeric:Boolean = s matching "^(\\+|\\-)?(0+\\.[\\d]+|[1-9]+[\\d]*\\.[\\d]+|[\\d]+)$"


/**
* 提取字符串中所有的数值
* @return List[String]
*/
def extractMumeric:List[String] = s matchAll "(\\+|\\-)?(0+\\.[\\d]+|[1-9]+[\\d]*\\.[\\d]+|[\\d]+)"

/**
* 判断字符串是否是手机号格式
* @param len 字符串长度
* @return Boolean
*/
def isMobileNumber(implicit len:Int = s.length):Boolean = {
val mobileNumberRegex = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))[0-9]{8}$"
// 截取后11位
val number = s.slice(len-11, len)
number matching mobileNumberRegex
}

/**
* 提取字符串所有手机号
* @return List[String]
*/
def extractMobileNumber:List[String] = s matchAll "((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))[0-9]{8}"

}

}


87 changes: 87 additions & 0 deletions src/main/java/tech/sqlclub/common/utils/FileUtils.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package tech.sqlclub.common.utils

import java.io.{File, FileInputStream, InputStream}
import java.util.Properties

import org.apache.commons.lang3.StringUtils
import org.yaml.snakeyaml.Yaml
import tech.sqlclub.common.regex.RegexOp._
import scala.collection.JavaConversions._

/**
*
* Created by songgr on 2020/01/03.
*/
object FileUtils {

/**
* 获取目录文件列表
* @param path 目录
* @param pattern 匹配正则
* @param recursive 是否递归子目录
* @return Array[File]
*/
def lsfile(path:String, pattern:String = ".*", recursive:Boolean=false):Array[File] = {

val file = new File(path)

if (file.isDirectory){

val files = file.listFiles().filter(_.isFile).filter(_.getName matching pattern )

if (recursive) {
val fs = file.listFiles().filter(_.isDirectory).flatMap{
f =>
lsfile(f.getAbsolutePath, pattern, recursive)
}
return files ++ fs
}
return files
}
Array.empty[File]
}


/**
* 创建目录
* @param path 目录
* @return Boolean
*/
def mkdirs(path:String): Boolean = {
if (StringUtils.isNotBlank(path)) {
val file = new File(path)
if (!file.isDirectory){
if (!file.exists) file.mkdirs()
}
}
false
}

def readYamlFile(path:String):Map[String,Object] = readYamlFile(new File(path))

def readYamlFile(file:File)
(implicit inputStream:InputStream=new FileInputStream(file)):Map[String,Object] = {
try {
val yaml = new Yaml()
yaml.load[java.util.Map[String,Object]](inputStream).toMap
} finally {
inputStream.close()
}
}


def readPropertiesFile(path:String):Map[String,Object] = readPropertiesFile(new File(path))

def readPropertiesFile(file:File)
(implicit inputStream:InputStream=new FileInputStream(file)):Map[String,Object] = {
try {
val properties = new Properties()
properties.load(inputStream)
properties.toMap[String,Object]
} finally {
inputStream.close()

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.util.control.NonFatal
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule

object JacksonUtil {
object JacksonUtils {
private val _mapper = new ObjectMapper()
_mapper.registerModule(DefaultScalaModule)

Expand Down
41 changes: 40 additions & 1 deletion src/main/java/tech/sqlclub/common/utils/NumberUtils.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tech.sqlclub.common.utils

import java.lang._

object NumberUtils {


def getIntValue( obj:Object, defaultValue:Int) = {
def getIntValue( obj:Object, defaultValue:Integer):Integer = {
if (obj == null) {
defaultValue
} else {
Expand All @@ -16,4 +18,41 @@ object NumberUtils {

}

def getLongValue( obj:Object, defaultValue:Long):Long = {
if (obj == null) {
defaultValue
} else {
try {
Long.parseLong(obj.toString)
} catch {
case e:Exception => defaultValue
}
}
}

def getDoubleValue( obj:Object, defaultValue:Double):Double = {
if (obj == null) {
defaultValue
} else {
try {
Double.parseDouble(obj.toString)
} catch {
case e:Exception => defaultValue
}
}

}

def getFloatValue( obj:Object, defaultValue:Float):Float = {
if (obj == null) {
defaultValue
} else {
try {
Float.parseFloat(obj.toString)
} catch {
case e:Exception => defaultValue
}
}
}

}
Loading

0 comments on commit 8c72822

Please sign in to comment.