improved javadoc
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / WildCardMatcher.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.string;
11
12 public class WildCardMatcher {
13
14         /**
15          * Allow only for * in wildcards
16          */
17         private static boolean checkWildCardEnd(final String wildcardExpression,
18                         int wildCardPosition) {
19                 for (; wildCardPosition < wildcardExpression.length(); wildCardPosition++) {
20                         final char wildCardChar = wildcardExpression
21                                         .charAt(wildCardPosition);
22                         if (wildCardChar != '*')
23                                 return false;
24                 }
25
26                 return true;
27         }
28
29         /**
30          * <pre>
31          * Test input string against wildcard expression.
32          * * -- corresponds to any amount of characters.
33          * ? -- corresponds to any single character.
34          * </pre>
35          *
36          * @param inputString
37          *            input string
38          * @param wildcardExpression
39          *            wildcard expression
40          * @return <code>true</code> if input string matches input pattern
41          */
42         public static boolean match(final String inputString,
43                         final String wildcardExpression) {
44
45                 if (inputString == null)
46                         return false;
47
48                 if (wildcardExpression == null)
49                         return false;
50
51                 int i;
52
53                 for (i = 0; i < inputString.length(); i++) {
54                         if (i >= wildcardExpression.length())
55                                 return false;
56                         final char wildCardChar = wildcardExpression.charAt(i);
57                         if (wildCardChar == '*')
58                                 return matchPiece(inputString, i, wildcardExpression, i + 1);
59                         if (wildCardChar != '?')
60                                 if (inputString.charAt(i) != wildCardChar)
61                                         return false;
62                 }
63
64                 return checkWildCardEnd(wildcardExpression, i);
65         }
66
67         private static boolean matchPiece(final String inputString,
68                         final int inputStringIndex, final String wildcardExpression,
69                         final int wildCardExpressionIndex) {
70
71                 int wildCardPosition = wildCardExpressionIndex;
72
73                 for (int i = inputStringIndex; i < inputString.length(); i++) {
74
75                         wildCardPosition = wildCardExpressionIndex;
76
77                         subMatchAttempt: {
78
79                                 for (int j = i; j < inputString.length(); j++) {
80                                         if (wildCardPosition >= wildcardExpression.length())
81                                                 break subMatchAttempt;
82                                         final char wildCardChar = wildcardExpression
83                                                         .charAt(wildCardPosition);
84
85                                         if (wildCardChar == '*')
86                                                 return matchPiece(inputString, j, wildcardExpression,
87                                                                 wildCardPosition + 1);
88
89                                         if (wildCardChar != '?')
90                                                 if (inputString.charAt(j) != wildCardChar)
91                                                         break subMatchAttempt;
92
93                                         wildCardPosition++;
94                                 }
95
96                                 return checkWildCardEnd(wildcardExpression, wildCardPosition);
97                         }
98
99                 }
100
101                 return checkWildCardEnd(wildcardExpression, wildCardPosition);
102         }
103
104 }