package com.airlock.iam.common.application.configuration.authorization;

import com.google.common.base.Splitter;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.regex.Pattern;

public class MethodAndPathMatcher {
   private static final Splitter LIST_SPLITTER = Splitter.on(',').omitEmptyStrings().trimResults();

   public boolean methodMatches(String methodPattern, String method) {
      for (String m : LIST_SPLITTER.split(methodPattern)) {
         if ("*".equals(m) || method.equals(m)) {
            return true;
         }
      }

      return false;
   }

   public boolean pathMatches(Pattern pathPattern, String path) {
      return pathPattern.matcher(path).matches();
   }

   @SuppressFBWarnings(justification = "generated code")
   MethodAndPathMatcher() {
   }

   @SuppressFBWarnings(justification = "generated code")
   public static MethodAndPathMatcher.MethodAndPathMatcherBuilder builder() {
      return new MethodAndPathMatcher.MethodAndPathMatcherBuilder();
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class MethodAndPathMatcherBuilder {
      @SuppressFBWarnings(justification = "generated code")
      MethodAndPathMatcherBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public MethodAndPathMatcher build() {
         return new MethodAndPathMatcher();
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "MethodAndPathMatcher.MethodAndPathMatcherBuilder()";
      }
   }
}
