1 // $Id: Interval.java,v 1.2 2004/02/07 11:24:11 powerpete Exp $
2 // [JMP, 03.02.2004] Created this file.
3 package org.jface.stepmt.core;
4
5 import org.apache.commons.lang.StringUtils;
6
7 /***
8 * Represents an interval inside a {@link String}.
9 *
10 * @see org.jface.stepmt.core.Pattern
11 *
12 * @author <a href="mailto:powerpete@users.sf.net">M. Petersen</a>
13 * @version $Id: Interval.java,v 1.2 2004/02/07 11:24:11 powerpete Exp $
14 */
15 public class Interval
16 {
17 private int start;
18 private int end;
19
20 /***
21 * Creates a new {@link Interval} at the given start index with the given
22 * length.
23 *
24 * @param start The start index.
25 * @param length The length of the {@link Interval}.
26 */
27 public Interval(final int start, final int length)
28 {
29 this.start = start;
30 this.end = start + length;
31 }
32
33 /***
34 * Returns the text before this {@link Interval}.
35 *
36 * @param text The text used to cut out the text before this
37 * {@link Interval}.
38 *
39 * @return The text before this {@link Interval}.
40 */
41 public String textBefore(final String text)
42 {
43 return StringUtils.substring(text, 0, start);
44 }
45
46 /***
47 * Returns the text after this {@link Interval}.
48 *
49 * @param text The text used to cut out the text after this
50 * {@link Interval}.
51 *
52 * @return The text after this {@link Interval}.
53 */
54 public String textAfter(final String text)
55 {
56 return StringUtils.substring(text, end);
57 }
58
59 /***
60 * Merges this {@link Interval} with the given {@link Interval}. The result
61 * will be an {@link Interval} spanning both {@link Interval}s.
62 *
63 * @param interval The other {@link Interval}.
64 *
65 * @return The merged {@link Interval}.
66 */
67 public Interval merge(final Interval interval)
68 {
69 if (interval == null)
70 {
71 return null;
72 }
73 final int min = Math.min(start, interval.start);
74 final int max = Math.max(end, interval.end);
75 return new Interval(min, max - min);
76 }
77
78 /***
79 * Returns the index after this {@link Interval}.
80 *
81 * @return The index after this {@link Interval}.
82 */
83 public int indexAfter()
84 {
85 return end;
86 }
87
88 /***
89 * Returns <tt>true</tt>, if this {@link Interval} is befor the given
90 * {@link Interval}.
91 *
92 * @param interval The other {@link Interval}.
93 *
94 * @return <tt>true</tt> if this {@link Interval} is before the other,
95 * otherwise <tt>false</tt>.
96 */
97 boolean isBefore(final Interval interval)
98 {
99 return start < interval.start;
100 }
101 }
This page was automatically generated by Maven