1 package eu.simuline.m2latex.core;
2
3 import com.florianingerl.util.regex.MatchResult;
4
5
6 /**
7 * Describes a match in a file.
8 * The first question is whether the file is readable
9 * which is represented by {@link #isFileReadable()}.
10 * If it is readable, then it can be asked whether it matches a regular expression
11 * by {@link #doesExprMatch()}.
12 * Of course, if the file is not readable one cannot ask for a match.
13 * If it matches, then, one can query the match result via {@link #getMatchResult()}.
14 * Of course without a match, it cannot be asked for a match result.
15 * <p>
16 * To create a file match accordingly, there are static methods:
17 * {@link #unreadable()} returns an instance representing an unreadable file,
18 * whereas all other creator methods represent readable files.
19 * {@link #noMatch()} represents a file readable but without a match.
20 * Finally, {@link #fileMatch(MatchResult)} represents a match
21 * given by the match result.
22 * <p>
23 * This class is both the base class for all kins of file matches
24 * and also represents the case with an unreadable file as returned by {@link #unreadable()}.
25 */
26 public class FileMatch {
27
28 static class FileMatchReadableNoMatch extends FileMatch {
29
30 private FileMatchReadableNoMatch() {}
31
32 boolean isFileReadable() {
33 return true;
34 }
35
36 boolean doesExprMatch() {
37 return false;
38 }
39 } // FileMatchReadable
40
41 static class FileMatchWithMatcher extends FileMatch {
42
43 private final MatchResult matchRes;
44
45 FileMatchWithMatcher(MatchResult matchRes) {
46 this.matchRes = matchRes;
47 }
48
49 boolean isFileReadable() {
50 return true;
51 }
52
53 boolean doesExprMatch() {
54 return true;
55 }
56
57 MatchResult getMatchResult() {
58 return this.matchRes;
59 }
60 } // class FileMatchWithMatcher
61
62 private final static FileMatch UNREADABLE = new FileMatch();
63
64 private final static FileMatch NO_MATCH = new FileMatchReadableNoMatch();
65
66 private FileMatch() {}
67
68 /**
69 * Returns a file match with an unreadable file.
70 * This is the singleton {@link #UNREADABLE}.
71 */
72 static FileMatch unreadable() {
73 return UNREADABLE;
74 }
75
76 /**
77 * Returns a file match with readable file but without match.
78 */
79 static FileMatch noMatch() {
80 return NO_MATCH;
81 }
82
83 /**
84 * Returns a file match with the given match result.
85 * In particular, the file is readable and matches.
86 *
87 * @param matchRes
88 * The match result of an existing file.
89 */
90 static FileMatch fileMatch(MatchResult matchRes) {
91 return new FileMatchWithMatcher(matchRes);
92 }
93
94 // to be overwritten
95 /**
96 * Returns whether the underlying file is readable.
97 *
98 * @return
99 * whether the underlying file is readable.
100 */
101 boolean isFileReadable() {
102 return false;
103 }
104
105 // to be overwritten
106 /**
107 * Returns whether the pattern matches, provided the file is readable.
108 *
109 * @return
110 * whether the pattern matches.
111 * @throws IllegalStateException
112 * if the file is unreadable.
113 */
114 boolean doesExprMatch() {
115 throw new IllegalStateException("Unreadable cannot be asked for match. ");
116 }
117
118 // to be overwritten
119 /**
120 * Returns a match result if there is one.
121 *
122 * @return
123 * the match result if the file is readable and matches the underlying regular expression.
124 * @throws IllegalStateException
125 * if
126 * <ul>
127 * <li>the file is unreadable or
128 * <li>the pattern does not match
129 * </ul>
130 * @throws IllegalArgumentException
131 * If no IllegalStateException but there is no group matched.
132 */
133 MatchResult getMatchResult() {
134 throw new IllegalStateException("No group matched. ");
135 }
136
137 }