aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/main/java/com/yahoo/jdisc/application/BindingMatch.java
blob: 2ba9758ef74331f38d98b3b8507226cd13310074 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;

import java.net.URI;
import java.util.Objects;

/**
 * <p>This class holds the result of a {@link BindingSet#match(URI)} operation. It contains methods to inspect the
 * groups captured during matching, where a <em>group</em> is defined as a sequence of characters matches by a wildcard
 * in the {@link UriPattern}, and to retrieve the matched target.</p>
 *
 * @param <T> The class of the target.
 */
public class BindingMatch<T> {

    private final UriPattern.Match match;
    private final T target;
    private final UriPattern matched;

    /**
     * <p>Constructs a new instance of this class.</p>
     *
     * @param match  The match information for this instance.
     * @param target The target of this match.
     * @param matched The matched URI pattern
     * @throws NullPointerException If any argument is null.
     */
    public BindingMatch(UriPattern.Match match, T target, UriPattern matched) {
        Objects.requireNonNull(match, "match");
        Objects.requireNonNull(target, "target");
        this.match = match;
        this.target = target;
        this.matched = matched;
    }

    /**
     * <p>Returns the number of captured groups of this match. Any non-negative integer smaller than the value returned
     * by this method is a valid group index for this match.</p>
     *
     * @return The number of captured groups.
     */
    public int groupCount() {
        return match.groupCount();
    }

    /**
     * <p>Returns the input subsequence captured by the given group by this match. Groups are indexed from left to
     * right, starting at zero. Note that some groups may match an empty string, in which case this method returns the
     * empty string. This method never returns null.</p>
     *
     * @param idx The index of the group to return.
     * @return The (possibly empty) substring captured by the group during matching, never <code>null</code>.
     * @throws IndexOutOfBoundsException If there is no group in the match with the given index.
     */
    public String group(int idx) {
        return match.group(idx);
    }

    /**
     * <p>Returns the matched target.</p>
     *
     * @return The matched target.
     */
    public T target() {
        return target;
    }

    /**
     * <p>Returns the URI pattern that was matched.</p>
     *
     * @return The matched pattern.
     */
    public UriPattern matched() {
        return matched;
    }

}