2222
2323import java .net .MalformedURLException ;
2424import java .net .URL ;
25+ import java .util .ArrayList ;
26+ import java .util .List ;
2527import java .util .Objects ;
2628import java .util .function .Predicate ;
2729import java .util .regex .Pattern ;
3032import static com .microsoft .playwright .impl .Utils .toJsRegexFlags ;
3133
3234class UrlMatcher {
33- final Object rawSource ;
34- private final Predicate <String > predicate ;
35-
36- private static Predicate <String > toPredicate (Pattern pattern ) {
37- return s -> pattern .matcher (s ).find ();
38- }
39-
40- static UrlMatcher any () {
41- return new UrlMatcher ((Object ) null , null );
42- }
35+ private final URL baseURL ;
36+ public final String glob ;
37+ public final Pattern pattern ;
38+ public final Predicate <String > predicate ;
4339
4440 static UrlMatcher forOneOf (URL baseUrl , Object object ) {
4541 if (object == null ) {
46- return UrlMatcher . any ( );
42+ return new UrlMatcher ( null , null , null , null );
4743 }
4844 if (object instanceof String ) {
4945 return new UrlMatcher (baseUrl , (String ) object );
@@ -68,50 +64,76 @@ static String resolveUrl(URL baseUrl, String spec) {
6864 }
6965 }
7066
71- UrlMatcher (URL base , String url ) {
72- this (url , toPredicate ( Pattern . compile ( globToRegex ( resolveUrl ( base , url )))). or ( s -> url == null || url . equals ( s )) );
67+ UrlMatcher (URL baseURL , String glob ) {
68+ this (baseURL , glob , null , null );
7369 }
7470
7571 UrlMatcher (Pattern pattern ) {
76- this (pattern , toPredicate ( pattern ) );
72+ this (null , null , pattern , null );
7773 }
74+
7875 UrlMatcher (Predicate <String > predicate ) {
79- this (predicate , predicate );
76+ this (null , null , null , predicate );
8077 }
8178
82- private UrlMatcher (Object rawSource , Predicate <String > predicate ) {
83- this .rawSource = rawSource ;
79+ private UrlMatcher (URL baseURL , String glob , Pattern pattern , Predicate <String > predicate ) {
80+ this .baseURL = baseURL ;
81+ this .glob = glob ;
82+ this .pattern = pattern ;
8483 this .predicate = predicate ;
8584 }
8685
8786 boolean test (String value ) {
88- return predicate == null || predicate .test (value );
87+ return testImpl (baseURL , pattern , predicate , glob , value );
88+ }
89+
90+ private static boolean testImpl (URL baseURL , Pattern pattern , Predicate <String > predicate , String glob , String value ) {
91+ if (pattern != null ) {
92+ return pattern .matcher (value ).find ();
93+ }
94+ if (predicate != null ) {
95+ return predicate .test (value );
96+ }
97+ if (glob != null ) {
98+ return Pattern .compile (globToRegex (resolveUrl (baseURL , glob ))).matcher (value ).find ();
99+ }
100+ return true ;
89101 }
90102
91103 @ Override
92104 public boolean equals (Object o ) {
93105 if (this == o ) return true ;
94106 if (o == null || getClass () != o .getClass ()) return false ;
95107 UrlMatcher that = (UrlMatcher ) o ;
96- if (rawSource instanceof Pattern && that .rawSource instanceof Pattern ) {
97- Pattern a = (Pattern ) rawSource ;
98- Pattern b = (Pattern ) that .rawSource ;
99- return a .pattern ().equals (b .pattern ()) && a .flags () == b .flags ();
108+ if (pattern != null && !pattern .pattern ().equals (that .pattern .pattern ()) && pattern .flags () == that .pattern .flags ()) {
109+ return false ;
110+ }
111+ if (predicate != null && !predicate .equals (that .predicate )) {
112+ return false ;
100113 }
101- return Objects .equals (rawSource , that .rawSource );
114+ if (glob != null && !glob .equals (that .glob )) {
115+ return false ;
116+ }
117+ return true ;
102118 }
103119
104120 @ Override
105121 public int hashCode () {
106- return Objects .hash (rawSource );
122+ if (pattern != null ) {
123+ return pattern .hashCode ();
124+ }
125+ if (predicate != null ) {
126+ return predicate .hashCode ();
127+ }
128+ return glob .hashCode ();
107129 }
108130
109131 @ Override
110132 public String toString () {
111- if (rawSource = = null )
112- return "<any>" ;
113- if (rawSource instanceof Predicate )
114- return "matching predicate" ;
115- return rawSource . toString ( );
133+ if (pattern ! = null )
134+ return String . format ( "<regex pattern= \" %s \" flags= \" %s \" >" , pattern . pattern (), toJsRegexFlags ( pattern )) ;
135+ if (this . predicate != null )
136+ return "< predicate> " ;
137+ return String . format ( "<glob pattern= \" %s \" >" , glob );
116138 }
117139}
0 commit comments