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
index 86d7a1b..ebe5175 100755 (executable)
@@ -1,22 +1,51 @@
 /*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
  */
-
 package eu.svjatoslav.commons.string.tokenizer;
 
+import java.util.regex.Matcher;
+
 public class TokenizerMatch {
 
     public final String token;
+
+    /**
+     * {@link Terminator} that matched current token
+     */
     public final Terminator terminator;
 
-    public TokenizerMatch(final String token, final Terminator terminator) {
+    public final Matcher matcher;
+
+    private Tokenizer tokenizer;
+
+    public TokenizerMatch(final String token, final Terminator terminator, Matcher matcher, Tokenizer tokenizer) {
         this.token = token;
         this.terminator = terminator;
+        this.matcher = matcher;
+        this.tokenizer = tokenizer;
+    }
+
+    public boolean isGroup(String group){
+        if (terminator == null){
+            return group == null;
+        }
+
+        if (terminator.group == null){
+            return group == null;
+        }
+
+        return terminator.group.equals(group);
+    }
+
+    public String[] getRegExpGroups(){
+        String[] result = new String[matcher.groupCount()];
+
+        for (int i = 0; i< result.length; i++){
+            result[i] = matcher.group(i+1);
+        }
+
+        return result;
     }
 
     @Override
@@ -26,4 +55,8 @@ public class TokenizerMatch {
                 ", terminator=" + terminator +
                 '}';
     }
+
+    public Tokenizer getTokenizer() {
+        return tokenizer;
+    }
 }