All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fireflysource.net.http.server.impl.matcher.AbstractPatternMatcher.kt Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.net.http.server.impl.matcher

import com.fireflysource.common.string.Pattern
import com.fireflysource.net.http.server.Matcher
import com.fireflysource.net.http.server.Router
import java.util.*


abstract class AbstractPatternMatcher : Matcher {

    companion object {
        const val paramName = "param"
    }

    protected val patternMap: MutableMap> = HashMap>()

    class PatternRule(val rule: String) {

        val pattern: Pattern = Pattern.compile(rule, "*")

        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (javaClass != other?.javaClass) return false

            other as PatternRule
            return rule == other.rule
        }

        override fun hashCode(): Int {
            return rule.hashCode()
        }
    }

    override fun add(rule: String, router: Router) {
        patternMap.computeIfAbsent(PatternRule(rule)) { TreeSet() }.add(router)
    }

    override fun match(value: String): Matcher.MatchResult? {
        if (patternMap.isEmpty()) return null

        val routers = TreeSet()
        val parameters = HashMap>()

        patternMap.forEach { (rule, routerSet) ->
            val strings: Array? = rule.pattern.match(value)
            if (strings != null) {
                routers.addAll(routerSet)
                if (strings.isNotEmpty()) {
                    val param: MutableMap = HashMap()
                    for (i in strings.indices) {
                        param["$paramName$i"] = strings[i]
                    }
                    routerSet.forEach { router -> parameters[router] = param }
                }
            }
        }
        return if (routers.isEmpty()) null else Matcher.MatchResult(routers, parameters, matchType)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy