Skip to content

Commit

Permalink
chore: remove deprecated utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent committed Sep 3, 2023
1 parent 6fa8dfa commit 4b1f8c2
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ object RegexUtils {
/**
* find the [Pattern] from cache or compile the regular expression and cache it
*/
private fun getPattern(regex: String?, flags: Int = 0): Pattern? {
if (regex == null) return null
private fun getPattern(regex: String, flags: Int = 0): Pattern {
val regexWithFlag = RegexWithFlag(regex, flags)
var pattern: Pattern? = tryGet(regexWithFlag)
if (null == pattern) {
pattern = Pattern.compile(regex, flags)
cachePattern(regexWithFlag, pattern)
}
return pattern
return pattern!!
}

/**
Expand Down Expand Up @@ -57,7 +56,7 @@ object RegexUtils {
return null
}
val pattern = getPattern(regex, Pattern.DOTALL)
return get(pattern!!, content, groupIndex)
return get(pattern, content, groupIndex)
}

private fun get(pattern: Pattern, content: String, groupIndex: Int): String? {
Expand All @@ -72,13 +71,13 @@ object RegexUtils {
* otherwise null if not matched
*/
fun getAllGroups(pattern: String?, content: String?): List<String>? {
if (pattern == null || content == null) {
return null
}
return getAllGroups(getPattern(pattern), content)
}

private fun getAllGroups(pattern: Pattern?, content: String?): List<String>? {
if (null == content || null == pattern) {
return null
}
private fun getAllGroups(pattern: Pattern, content: String): List<String> {

val result = ArrayList<String>()
val matcher = pattern.matcher(content)
Expand All @@ -96,18 +95,18 @@ object RegexUtils {
return null
}
val pattern = getPattern(regex, Pattern.DOTALL)
return extract(pattern!!, content, template)
return extract(pattern, content, template)
}

private fun extract(pattern: Pattern, content: String, template: String): String {
return if (template.contains("$")) {
extract(pattern, content) { template }
} else {
extract(pattern, content) { matcher ->
extract(GROUP_VAR, template) {
matcher.group(it.group(0).toInt())
matcher.group(it.group(1).toInt())
}
}
} else {
extract(pattern, content) { template }
}
}

Expand All @@ -126,7 +125,7 @@ object RegexUtils {
* pattern with the given replacement string.
*/
fun delFirst(pattern: String, content: String): String {
return delFirst(getPattern(pattern)!!, content)
return delFirst(getPattern(pattern), content)
}

private fun delFirst(pattern: Pattern, content: String): String {
Expand All @@ -143,13 +142,13 @@ object RegexUtils {
* pattern
*/
fun delAll(regex: String, content: String): String {
if (StringUtils.isAnyBlank(regex, content)) {
if (StringUtils.isAnyEmpty(regex, content)) {
return content
}

// RegEx pattern = RegEx.compile(regex, RegEx.DOTALL);
val pattern = getPattern(regex, Pattern.DOTALL)
return delAll(pattern!!, content)
return delAll(pattern, content)
}

/**
Expand All @@ -158,14 +157,11 @@ object RegexUtils {
* pattern
*/
private fun delAll(pattern: Pattern, content: String): String {
return if (content.isBlank()) {
content
} else pattern.matcher(content).replaceAll("")

return pattern.matcher(content).replaceAll("")
}

fun delBefore(regex: String, content: String): String? {
val pattern = getPattern(regex, Pattern.DOTALL)!!
val pattern = getPattern(regex, Pattern.DOTALL)
val matcher = pattern.matcher(content)
return if (matcher.find()) {
StringUtils.substring(content, matcher.end() - 1, content.length)
Expand All @@ -192,19 +188,11 @@ object RegexUtils {
}

private fun <T : MutableCollection<String>> findAll(
pattern: Pattern?,
content: String?,
pattern: Pattern,
content: String,
group: Int,
collection: T?,
): T? {
if (null == pattern || null == content) {
return null
}

if (null == collection) {
throw NullPointerException("Null collection param provided!")
}

collection: T,
): T {
val matcher = pattern.matcher(content)
while (matcher.find()) {
collection.add(matcher.group(group))
Expand All @@ -217,7 +205,7 @@ object RegexUtils {
return 0
}
val pattern = getPattern(regex, Pattern.DOTALL)
return count(pattern!!, content)
return count(pattern, content)
}

private fun count(pattern: Pattern, content: String): Int {
Expand All @@ -235,15 +223,15 @@ object RegexUtils {
return false
}
val pattern = getPattern(regex, Pattern.DOTALL)
return pattern!!.matcher(content).find()
return pattern.matcher(content).find()
}

fun isMatch(regex: String?, content: String?): Boolean {
if (content == null || regex.isNullOrBlank()) {
return false
}
val pattern = getPattern(regex, Pattern.DOTALL)
return pattern!!.matcher(content).matches()
return pattern.matcher(content).matches()
}

fun replaceAll(content: String, regex: String, replacementTemplate: String): String? {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,95 @@ package com.itangcent.idea.plugin.utils
*/
interface Storage {

/**
* Get the value of the specified name in the default group
*/
fun get(name: String?): Any?

/**
* Get the value of the specified name in the specified group
*/
fun get(group: String?, name: String?): Any?

/**
* Set the value of the specified name in the default group
*/
fun set(name: String?, value: Any?)

/**
* Set the value of the specified name in the specified group
*/
fun set(group: String?, name: String?, value: Any?)

/**
* Pop the value of the specified name in the default group
*/
fun pop(name: String?): Any?

/**
* Pop the value of the specified name in the specified group
*/
fun pop(group: String?, name: String?): Any?

/**
* Peek the value of the specified name in the default group
*/
fun peek(name: String?): Any?

/**
* Peek the value of the specified name in the specified group
*/
fun peek(group: String?, name: String?): Any?

/**
* Push the value of the specified name in the default group
*/
fun push(name: String?, value: Any?)

/**
* Push the value of the specified name in the specified group
*/
fun push(group: String?, name: String?, value: Any?)

/**
* Remove the value of the specified name in the default group
*/
fun remove(name: String)

/**
* Remove the value of the specified name in the specified group
*/
fun remove(group: String?, name: String)

/**
* Get all keys in the default group
*/
fun keys(): Array<Any?>

/**
* Get all keys in the specified group
*/
fun keys(group: String?): Array<Any?>

/**
* Clear all data in the default group
*/
fun clear()

/**
* Clear all data in the specified group
*/
fun clear(group: String?)

companion object {
/**
* The default group
*/
const val DEFAULT_GROUP = "__default_local_group"

/**
* The null key
*/
const val NULL = "__null"
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4b1f8c2

Please sign in to comment.