Skip to content

Commit 8c72822

Browse files
committed
✨ commit yaml/properties file utils && net related utils && string regex ops
1 parent 557e5a7 commit 8c72822

14 files changed

Lines changed: 569 additions & 4 deletions

File tree

pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>tech.sqlclub</groupId>
88
<artifactId>common-utils</artifactId>
9-
<version>1.0.0</version>
9+
<version>1.0.1</version>
1010

1111
<name>common utils</name>
1212
<url>https://github.com/bebee4java/common-utils</url>
@@ -94,6 +94,18 @@
9494
<version>2.9.9</version>
9595
</dependency>
9696

97+
<dependency>
98+
<groupId>org.yaml</groupId>
99+
<artifactId>snakeyaml</artifactId>
100+
<version>1.25</version>
101+
</dependency>
102+
103+
<dependency>
104+
<groupId>org.apache.commons</groupId>
105+
<artifactId>commons-lang3</artifactId>
106+
<version>3.4</version>
107+
</dependency>
108+
97109
</dependencies>
98110

99111

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tech.sqlclub.common.context
2+
3+
import java.io.{File, FileInputStream, InputStream}
4+
import java.util.Properties
5+
6+
import tech.sqlclub.common.utils.{FileUtils, ParamMapUtils}
7+
8+
object PropertiesContext {
9+
val resourcePath = this.getClass.getClassLoader.getResource("").getPath
10+
lazy val properties = new Properties()
11+
12+
import scala.collection.JavaConversions._
13+
private lazy val paramMap = {
14+
getAllPropertyFile.map(loadPropertyFile _ )
15+
properties.toMap[String, AnyRef]
16+
}
17+
18+
def getAllPropertyFile = FileUtils.lsfile(resourcePath,".*\\.properties")
19+
20+
def loadPropertyFile(file:File)
21+
(implicit inputStream:InputStream=new FileInputStream(file)) = {
22+
try {
23+
properties.load(inputStream)
24+
} finally {
25+
inputStream.close()
26+
}
27+
}
28+
29+
30+
implicit class PropertiesContext_impl( _this_ : PropertiesContext.type ) extends ParamMapUtils(_this_.paramMap)
31+
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tech.sqlclub.common.context
2+
3+
import java.io.{File, FileInputStream, InputStream}
4+
import java.util
5+
6+
import org.yaml.snakeyaml.Yaml
7+
import tech.sqlclub.common.utils.{FileUtils, ParamMapUtils}
8+
9+
10+
object YamlContext {
11+
val resourcePath = this.getClass.getClassLoader.getResource("").getPath
12+
lazy val yaml = new Yaml()
13+
14+
import scala.collection.JavaConversions._
15+
private lazy val paramMap = getAllYamlFile.flatMap(loadYamlFile[util.Map[String,Object]](_)).toMap
16+
17+
def getAllYamlFile = FileUtils.lsfile(resourcePath,".*\\.(yaml|yml)")
18+
19+
def loadYamlFile[A](file:File)
20+
(implicit inputStream:InputStream=new FileInputStream(file)):A = {
21+
try {
22+
yaml.load[A](inputStream)
23+
} finally {
24+
inputStream.close()
25+
}
26+
}
27+
28+
implicit class YamlContext_impl( _this_ : YamlContext.type ) extends ParamMapUtils(_this_.paramMap)
29+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package tech.sqlclub.common.net
2+
3+
import java.io.IOException
4+
import java.net._
5+
import java.util.concurrent.atomic.AtomicInteger
6+
7+
object NetUtils {
8+
9+
def getLocalServerIp: String = try {
10+
java.net.InetAddress.getLocalHost.getHostAddress
11+
} catch {
12+
case e: UnknownHostException =>
13+
e.printStackTrace()
14+
null
15+
}
16+
17+
18+
def getLocalServerIpv4List: List[String] = {
19+
try {
20+
import scala.collection.JavaConversions._
21+
NetworkInterface.getNetworkInterfaces.toList.filter(_.isUp)
22+
.flatMap(_.getInetAddresses)
23+
.filter {
24+
iaddr =>
25+
iaddr.isInstanceOf[Inet4Address] && !iaddr.isLoopbackAddress
26+
}.map(_.getHostAddress)
27+
} catch {
28+
case e: Exception =>
29+
e.printStackTrace()
30+
List.empty[String]
31+
}
32+
}
33+
34+
def portAvailableAndReturn(hostname: String, port_min_number: Int, port_max_number: Int):Int = {
35+
var bindSuccess: Boolean = false
36+
var ss: Socket = null
37+
var ss1: Socket = null
38+
val start: AtomicInteger = new AtomicInteger(port_min_number)
39+
40+
while (!bindSuccess && start.get() <= port_max_number) {
41+
try {
42+
ss = new Socket()
43+
ss.bind(new InetSocketAddress(hostname, start.get()))
44+
ss.close()
45+
if (hostname != "0.0.0.0") {
46+
ss1 = new Socket()
47+
ss1.bind(new InetSocketAddress("0.0.0.0", start.get()))
48+
ss1.close()
49+
}
50+
bindSuccess = true
51+
} catch {
52+
case e:IOException =>
53+
bindSuccess = false
54+
start.set(start.get() + 1)
55+
} finally {
56+
socketClose(ss)
57+
socketClose(ss1)
58+
}
59+
}
60+
if (bindSuccess) start.get() else -1
61+
}
62+
63+
def socketClose(s:Socket)= {
64+
if (s != null && !s.isClosed){
65+
try {
66+
s.close()
67+
} catch {
68+
case e:IOException =>
69+
e.printStackTrace()
70+
}
71+
}
72+
}
73+
74+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package tech.sqlclub.common.regex
2+
3+
4+
object RegexOp {
5+
implicit class RegexString(s:String) {
6+
/**
7+
* 判断字符串是否匹配正则
8+
* @param pattern 正则表达式
9+
* @return Boolean
10+
*/
11+
def matching(pattern:String):Boolean = (pattern.r findFirstIn s).isDefined
12+
13+
14+
/**
15+
* 正则匹配按组获取值
16+
* @param pattern 正则表达式
17+
* @param group 第几组
18+
* @return Iterator[String]
19+
*/
20+
def matchGroup(pattern:String, group:Int):Iterator[String] = {
21+
val matchs = pattern.r findAllMatchIn s
22+
matchs.map(_.group(group))
23+
}
24+
25+
/**
26+
* 正则匹配所有值
27+
* @param pattern 正则表达式
28+
* @return List[String]
29+
*/
30+
def matchAll(pattern:String):List[String] = (pattern.r findAllIn s).toList
31+
32+
/**
33+
* 正则匹配第一个值
34+
* @param pattern 正则表达式
35+
* @return Option[String]
36+
*/
37+
def matchFirst(pattern:String):Option[String] = pattern.r findFirstIn s
38+
39+
40+
/**
41+
* 判断字符串是否是数值格式
42+
* @return Boolean
43+
*/
44+
def isMumeric:Boolean = s matching "^(\\+|\\-)?(0+\\.[\\d]+|[1-9]+[\\d]*\\.[\\d]+|[\\d]+)$"
45+
46+
47+
/**
48+
* 提取字符串中所有的数值
49+
* @return List[String]
50+
*/
51+
def extractMumeric:List[String] = s matchAll "(\\+|\\-)?(0+\\.[\\d]+|[1-9]+[\\d]*\\.[\\d]+|[\\d]+)"
52+
53+
/**
54+
* 判断字符串是否是手机号格式
55+
* @param len 字符串长度
56+
* @return Boolean
57+
*/
58+
def isMobileNumber(implicit len:Int = s.length):Boolean = {
59+
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}$"
60+
// 截取后11位
61+
val number = s.slice(len-11, len)
62+
number matching mobileNumberRegex
63+
}
64+
65+
/**
66+
* 提取字符串所有手机号
67+
* @return List[String]
68+
*/
69+
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}"
70+
71+
}
72+
73+
}
74+
75+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package tech.sqlclub.common.utils
2+
3+
import java.io.{File, FileInputStream, InputStream}
4+
import java.util.Properties
5+
6+
import org.apache.commons.lang3.StringUtils
7+
import org.yaml.snakeyaml.Yaml
8+
import tech.sqlclub.common.regex.RegexOp._
9+
import scala.collection.JavaConversions._
10+
11+
/**
12+
*
13+
* Created by songgr on 2020/01/03.
14+
*/
15+
object FileUtils {
16+
17+
/**
18+
* 获取目录文件列表
19+
* @param path 目录
20+
* @param pattern 匹配正则
21+
* @param recursive 是否递归子目录
22+
* @return Array[File]
23+
*/
24+
def lsfile(path:String, pattern:String = ".*", recursive:Boolean=false):Array[File] = {
25+
26+
val file = new File(path)
27+
28+
if (file.isDirectory){
29+
30+
val files = file.listFiles().filter(_.isFile).filter(_.getName matching pattern )
31+
32+
if (recursive) {
33+
val fs = file.listFiles().filter(_.isDirectory).flatMap{
34+
f =>
35+
lsfile(f.getAbsolutePath, pattern, recursive)
36+
}
37+
return files ++ fs
38+
}
39+
return files
40+
}
41+
Array.empty[File]
42+
}
43+
44+
45+
/**
46+
* 创建目录
47+
* @param path 目录
48+
* @return Boolean
49+
*/
50+
def mkdirs(path:String): Boolean = {
51+
if (StringUtils.isNotBlank(path)) {
52+
val file = new File(path)
53+
if (!file.isDirectory){
54+
if (!file.exists) file.mkdirs()
55+
}
56+
}
57+
false
58+
}
59+
60+
def readYamlFile(path:String):Map[String,Object] = readYamlFile(new File(path))
61+
62+
def readYamlFile(file:File)
63+
(implicit inputStream:InputStream=new FileInputStream(file)):Map[String,Object] = {
64+
try {
65+
val yaml = new Yaml()
66+
yaml.load[java.util.Map[String,Object]](inputStream).toMap
67+
} finally {
68+
inputStream.close()
69+
}
70+
}
71+
72+
73+
def readPropertiesFile(path:String):Map[String,Object] = readPropertiesFile(new File(path))
74+
75+
def readPropertiesFile(file:File)
76+
(implicit inputStream:InputStream=new FileInputStream(file)):Map[String,Object] = {
77+
try {
78+
val properties = new Properties()
79+
properties.load(inputStream)
80+
properties.toMap[String,Object]
81+
} finally {
82+
inputStream.close()
83+
84+
}
85+
}
86+
87+
}

src/main/java/tech/sqlclub/common/utils/JacksonUtil.scala renamed to src/main/java/tech/sqlclub/common/utils/JacksonUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.util.control.NonFatal
44
import com.fasterxml.jackson.databind.ObjectMapper
55
import com.fasterxml.jackson.module.scala.DefaultScalaModule
66

7-
object JacksonUtil {
7+
object JacksonUtils {
88
private val _mapper = new ObjectMapper()
99
_mapper.registerModule(DefaultScalaModule)
1010

src/main/java/tech/sqlclub/common/utils/NumberUtils.scala

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package tech.sqlclub.common.utils
22

3+
import java.lang._
4+
35
object NumberUtils {
46

57

6-
def getIntValue( obj:Object, defaultValue:Int) = {
8+
def getIntValue( obj:Object, defaultValue:Integer):Integer = {
79
if (obj == null) {
810
defaultValue
911
} else {
@@ -16,4 +18,41 @@ object NumberUtils {
1618

1719
}
1820

21+
def getLongValue( obj:Object, defaultValue:Long):Long = {
22+
if (obj == null) {
23+
defaultValue
24+
} else {
25+
try {
26+
Long.parseLong(obj.toString)
27+
} catch {
28+
case e:Exception => defaultValue
29+
}
30+
}
31+
}
32+
33+
def getDoubleValue( obj:Object, defaultValue:Double):Double = {
34+
if (obj == null) {
35+
defaultValue
36+
} else {
37+
try {
38+
Double.parseDouble(obj.toString)
39+
} catch {
40+
case e:Exception => defaultValue
41+
}
42+
}
43+
44+
}
45+
46+
def getFloatValue( obj:Object, defaultValue:Float):Float = {
47+
if (obj == null) {
48+
defaultValue
49+
} else {
50+
try {
51+
Float.parseFloat(obj.toString)
52+
} catch {
53+
case e:Exception => defaultValue
54+
}
55+
}
56+
}
57+
1958
}

0 commit comments

Comments
 (0)