/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.text.format;

import java.io.Serializable;
import java.util.regex.Matcher;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public final class AnsiMatch implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private final AnsiCharSequence source;
    
    private final int[] groups;
    
    AnsiMatch(AnsiCharSequence source, int start, int end) {
        this.source = source;
        this.groups = new int[]{start, end};
    }
    
    AnsiMatch(AnsiCharSequence source, Matcher hit) {
        this.source = source;
        this.groups = new int[2*hit.groupCount()+2];
        for(int i=0, n=hit.groupCount(); i<=n; i++) {
            this.groups[2*i+0] = hit.start(i);
            this.groups[2*i+1] = hit.end(i);
        }
    }
    
    public AnsiCharSequence value() {
        return source.subSequence(begin(), end());
    }
    
    public AnsiCharSequence group(int index) {
        return source.subSequence(begin(index), end(index));
    }
    
    public int groups() {
        return groups.length / 2 - 1;
    }
    
    public int begin(int index) {
        return groups[2*index];
    }
    
    public int end(int index) {
        return groups[2*index+1];
    }
    
    public int begin() {
        return groups[0];
    }
    
    public int end() {
        return groups[1];
    }
    
}
