<commit_before>package org.http4k.routing

import org.http4k.core.Body
import org.http4k.core.Filter
import org.http4k.core.Headers
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Uri
import org.http4k.core.UriTemplate
import org.http4k.routing.RouterMatch.*
import org.http4k.websocket.WsConsumer
import java.io.InputStream

/**
 * Provides matching of a Request to an HttpHandler which can service it.
 */
fun interface Router {
    /**
     * Attempt to supply an HttpHandler which can service the passed request.
     */
    fun match(request: Request): RouterMatch
}

sealed class RouterMatch(private val priority: Int) : Comparable<RouterMatch> {
    data class MatchingHandler(private val httpHandler: HttpHandler) : RouterMatch(0), HttpHandler {
        override fun invoke(request: Request): Response = httpHandler(request)
    }

    object MatchedWithoutHandler : RouterMatch(1)
    object MethodNotMatched : RouterMatch(2)
    object Unmatched : RouterMatch(3)

    override fun compareTo(other: RouterMatch): Int = priority.compareTo(other.priority)
}

fun RouterMatch.and(other: RouterMatch): RouterMatch = when (this) {
    is MatchingHandler -> this
    is MatchedWithoutHandler -> other
    is MethodNotMatched, Unmatched -> Unmatched
}

/**
 * Composite HttpHandler which can potentially service many different URL patterns. Should
 * return a 404 Response if it cannot service a particular Request.
 *
 * Note that generally there should be no reason for the API user to implement this interface over and above the
 * implementations that already exist. The interface is public only because we have not found a way to hide it from
 * the API user in an API-consistent manner.
 */
interface RoutingHttpHandler : Router, HttpHandler {
    /**
     * Returns a RoutingHttpHandler which applies the passed Filter to all received requests before servicing them.
     * To follow the trend of immutability, this will generally be a new instance.
     */
    fun withFilter(new: Filter): RoutingHttpHandler

    /**
     * Returns a RoutingHttpHandler which prepends the passed base path to the logic determining the match()
     * To follow the trend of immutability, this will generally be a new instance.
     */
    fun withBasePath(new: String): RoutingHttpHandler
}
<commit_msg>Better 'and'<commit_after>package org.http4k.routing

import org.http4k.core.Body
import org.http4k.core.Filter
import org.http4k.core.Headers
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Uri
import org.http4k.core.UriTemplate
import org.http4k.routing.RouterMatch.MatchedWithoutHandler
import org.http4k.routing.RouterMatch.MatchingHandler
import org.http4k.routing.RouterMatch.MethodNotMatched
import org.http4k.routing.RouterMatch.Unmatched
import org.http4k.websocket.WsConsumer
import java.io.InputStream

/**
 * Provides matching of a Request to an HttpHandler which can service it.
 */
fun interface Router {
    /**
     * Attempt to supply an HttpHandler which can service the passed request.
     */
    fun match(request: Request): RouterMatch
}

sealed class RouterMatch(private val priority: Int) : Comparable<RouterMatch> {
    data class MatchingHandler(private val httpHandler: HttpHandler) : RouterMatch(0), HttpHandler {
        override fun invoke(request: Request): Response = httpHandler(request)
    }

    object MatchedWithoutHandler : RouterMatch(1)
    object MethodNotMatched : RouterMatch(2)
    object Unmatched : RouterMatch(3)

    override fun compareTo(other: RouterMatch): Int = priority.compareTo(other.priority)
}

fun RouterMatch.and(other: RouterMatch): RouterMatch = when (this) {
    is MatchedWithoutHandler -> other
    is MatchingHandler, MethodNotMatched, Unmatched -> this
}

/**
 * Composite HttpHandler which can potentially service many different URL patterns. Should
 * return a 404 Response if it cannot service a particular Request.
 *
 * Note that generally there should be no reason for the API user to implement this interface over and above the
 * implementations that already exist. The interface is public only because we have not found a way to hide it from
 * the API user in an API-consistent manner.
 */
interface RoutingHttpHandler : Router, HttpHandler {
    /**
     * Returns a RoutingHttpHandler which applies the passed Filter to all received requests before servicing them.
     * To follow the trend of immutability, this will generally be a new instance.
     */
    fun withFilter(new: Filter): RoutingHttpHandler

    /**
     * Returns a RoutingHttpHandler which prepends the passed base path to the logic determining the match()
     * To follow the trend of immutability, this will generally be a new instance.
     */
    fun withBasePath(new: String): RoutingHttpHandler
}

{'fraction_non_alphanumeric': 0.0710393541876892, 'fraction_numerical': 0.008274470232088799, 'mean_word_length': 4.119834710743802, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3952805', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1343, 'n_words': 497}