def checkMatch()

in common/src/main/scala/com/gu/multimedia/storagetier/framework/RoutingKeyMatcher.scala [14:38]


  def checkMatch(keySpec:String, routingKeyParts:Seq[String]): Boolean = {
    val keySpecParts = keySpec.split("\\.")

    def matchPiece(keySpecPiece:Option[String], remainingKeySpec:Seq[String], routingKeyPiece:Option[String], remainingRoutingKey:Seq[String]):Boolean = {
      if(keySpecPiece.isEmpty && routingKeyPiece.isEmpty) { //we got to the end of the iteration and both match
        true
      } else if(keySpecPiece.contains("#")) {
        true
      } else if(keySpecPiece.contains("*") && remainingRoutingKey.nonEmpty) {
        matchPiece(remainingKeySpec.headOption, remainingKeySpec.tail, remainingRoutingKey.headOption, remainingRoutingKey.tail)
      } else if(keySpecPiece==routingKeyPiece) {
        if(remainingKeySpec.isEmpty && remainingRoutingKey.isEmpty) { //both end on the next piece - we have 100% match
          true
        } else if(remainingKeySpec.isEmpty || remainingRoutingKey.isEmpty) {  //one ends but the other doesn't - then there is a mismatch
          false
        } else {  //keep going on to the next piece
          matchPiece(remainingKeySpec.headOption, remainingKeySpec.tail, remainingRoutingKey.headOption, remainingRoutingKey.tail)
        }
      } else {  //if they don't match literally or on wildcard (or one is empty) then we have a mismatch
        false
      }
    }

    matchPiece(keySpecParts.headOption, keySpecParts.tail, routingKeyParts.headOption, routingKeyParts.tail)
  }