Helper function to split string into groups based on regexp. Possibility to retrieve...
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / string / tokenizer / TokenizerMatch.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.commons.string.tokenizer;
6
7 import java.util.regex.Matcher;
8
9 public class TokenizerMatch {
10
11     public final String token;
12
13     /**
14      * {@link Terminator} that matched current token
15      */
16     public final Terminator terminator;
17
18     public final Matcher matcher;
19
20     private Tokenizer tokenizer;
21
22     public TokenizerMatch(final String token, final Terminator terminator, Matcher matcher, Tokenizer tokenizer) {
23         this.token = token;
24         this.terminator = terminator;
25         this.matcher = matcher;
26         this.tokenizer = tokenizer;
27     }
28
29     public boolean isGroup(String group){
30         if (terminator == null){
31             return group == null;
32         }
33
34         if (terminator.group == null){
35             return group == null;
36         }
37
38         return terminator.group.equals(group);
39     }
40
41     public String[] getRegExpGroups(){
42         String[] result = new String[matcher.groupCount()];
43
44         for (int i = 0; i< result.length; i++){
45             result[i] = matcher.group(i+1);
46         }
47
48         return result;
49     }
50
51     @Override
52     public String toString() {
53         return "TokenizerMatch{" +
54                 "token='" + token + '\'' +
55                 ", terminator=" + terminator +
56                 '}';
57     }
58
59     public Tokenizer getTokenizer() {
60         return tokenizer;
61     }
62 }