Skip to content

Commit

Permalink
Merge pull request #15 from simplecloudapp/feat/numerical-operation-m…
Browse files Browse the repository at this point in the history
…atcher

feat: added NumericOperationMatcher and GreaterThanOperation
  • Loading branch information
niklasnieberler authored Jan 9, 2025
2 parents 23faff9 + 1a9ee8f commit e7b12c9
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
package app.simplecloud.plugin.api.shared.matcher

import app.simplecloud.plugin.api.shared.matcher.operation.*
import app.simplecloud.plugin.api.shared.matcher.operation.NumericOperationMatcher
import app.simplecloud.plugin.api.shared.matcher.operation.OperationMatcher
import app.simplecloud.plugin.api.shared.matcher.operation.StringOperationMatcher
import app.simplecloud.plugin.api.shared.matcher.operation.impl.*

enum class OperationType(
private val matcher: OperationMatcher
private val matcher: OperationMatcher<*, *>
) {

REGEX(RegexOperationMatcher),
PATTERN(PatternOperationMatcher),
EQUALS(EqualsOperationMatcher),
CONTAINS(ContainsOperationMatcher),
STARTS_WITH(StartsWithOperationMatcher),
ENDS_WITH(EndsWithOperationMatcher);
ENDS_WITH(EndsWithOperationMatcher),
GREATER_THAN(GreaterThanOperationMatcher);

fun matches(name: String, value: String, negate: Boolean): Boolean {
val matches = this.matcher.matches(name, value)
if (negate) {
return matches.not()
fun matches(key: Any, value: Any, negate: Boolean): Boolean {
val matches = when (matcher) {
is StringOperationMatcher -> {
if (key is String && value is String) {
matcher.matches(key, value)
} else false
}

is NumericOperationMatcher -> {
if (key is Int && value is Int) {
matcher.matches(key, value)
} else false
}

else -> false
}
return matches

return if (negate) matches.not() else matches
}

fun anyMatches(names: List<String>, value: String, negate: Boolean): Boolean {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package app.simplecloud.plugin.api.shared.matcher.operation

interface NumericOperationMatcher : OperationMatcher<Int, Int>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.simplecloud.plugin.api.shared.matcher.operation

interface OperationMatcher {
interface OperationMatcher<K, V> {

fun matches(name: String, value: String): Boolean
fun matches(key: K, value: V): Boolean

}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package app.simplecloud.plugin.api.shared.matcher.operation

interface StringOperationMatcher : OperationMatcher<String, String>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.simplecloud.plugin.api.shared.matcher.operation.impl

import app.simplecloud.plugin.api.shared.matcher.operation.StringOperationMatcher
import org.spongepowered.configurate.objectmapping.ConfigSerializable

@ConfigSerializable
object ContainsOperationMatcher : StringOperationMatcher {

override fun matches(key: String, value: String): Boolean {
return key.contains(value, ignoreCase = true)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.simplecloud.plugin.api.shared.matcher.operation.impl

import app.simplecloud.plugin.api.shared.matcher.operation.StringOperationMatcher
import org.spongepowered.configurate.objectmapping.ConfigSerializable

@ConfigSerializable
object EndsWithOperationMatcher : StringOperationMatcher {

override fun matches(key: String, value: String): Boolean {
return key.endsWith(value, ignoreCase = true)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.simplecloud.plugin.api.shared.matcher.operation.impl

import app.simplecloud.plugin.api.shared.matcher.operation.StringOperationMatcher
import org.spongepowered.configurate.objectmapping.ConfigSerializable

@ConfigSerializable
object EqualsOperationMatcher : StringOperationMatcher {

override fun matches(key: String, value: String): Boolean {
return key.equals(value, ignoreCase = true)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package app.simplecloud.plugin.api.shared.matcher.operation.impl

import app.simplecloud.plugin.api.shared.matcher.operation.NumericOperationMatcher

object GreaterThanOperationMatcher : NumericOperationMatcher {

override fun matches(key: Int, value: Int): Boolean {
return key > value
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package app.simplecloud.plugin.api.shared.matcher.operation.impl

import app.simplecloud.plugin.api.shared.matcher.operation.StringOperationMatcher
import org.spongepowered.configurate.objectmapping.ConfigSerializable
import java.util.regex.Pattern

@ConfigSerializable
object PatternOperationMatcher : StringOperationMatcher {

override fun matches(key: String, value: String): Boolean {
return Pattern.matches(key, value)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.simplecloud.plugin.api.shared.matcher.operation.impl

import app.simplecloud.plugin.api.shared.matcher.operation.StringOperationMatcher
import org.spongepowered.configurate.objectmapping.ConfigSerializable

@ConfigSerializable
object RegexOperationMatcher : StringOperationMatcher {

override fun matches(key: String, value: String): Boolean {
return Regex(value).matches(key)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package app.simplecloud.plugin.api.shared.matcher.operation.impl

import app.simplecloud.plugin.api.shared.matcher.operation.StringOperationMatcher
import org.spongepowered.configurate.objectmapping.ConfigSerializable

@ConfigSerializable
object StartsWithOperationMatcher : StringOperationMatcher {

override fun matches(key: String, value: String): Boolean {
return key.startsWith(value, ignoreCase = true)
}

}

0 comments on commit e7b12c9

Please sign in to comment.