KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > util > CharIndexedSegment


1 /*
2  * CharIndexedSegment.java
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1998 Wes Biggs
7  * Copyright (C) 2000, 2001 Slava Pestov
8  *
9  * This library is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Library General Public License as published
11  * by the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */

23 package org.gjt.sp.util;
24
25 //{{{ Imports
26
import java.io.Serializable JavaDoc;
27 import javax.swing.text.Segment JavaDoc;
28 import gnu.regexp.*;
29 //}}}
30

31 /**
32  * Lets <code>gnu.regexp</code> search within <code>Segment</code> objects.
33  *
34  * @deprecated Use {@link SegmentCharSequence} and the java.util.regex
35  * package instead.
36  */

37 public class CharIndexedSegment implements CharIndexed, Serializable JavaDoc
38 {
39     //{{{ CharIndexedSegment constructor
40
/**
41      * Creates a new <code>CharIndexedSegment</code>.
42      * @since jEdit 4.1pre3
43      */

44     public CharIndexedSegment(Segment JavaDoc seg, int index)
45     {
46         this.seg = seg;
47         m_index = index;
48     } //}}}
49

50     //{{{ CharIndexedSegment constructor
51
/**
52      * Creates a new <code>CharIndexedSegment</code>.
53      * @since jEdit 4.1pre1
54      */

55     public CharIndexedSegment(Segment JavaDoc seg, boolean reverse)
56     {
57         this.seg = seg;
58         m_index = (reverse ? seg.count - 1 : 0);
59         this.reverse = reverse;
60     } //}}}
61

62     //{{{ charAt() method
63
public char charAt(int index)
64     {
65         if(reverse)
66             index = -index;
67
68         return ((m_index + index) < seg.count && m_index + index >= 0)
69             ? seg.array[seg.offset + m_index + index]
70             : CharIndexed.OUT_OF_BOUNDS;
71     } //}}}
72

73     //{{{ isValid() method
74
public boolean isValid()
75     {
76         return (m_index >=0 && m_index < seg.count);
77     } //}}}
78

79     //{{{ reset() method
80
public void reset()
81     {
82         m_index = (reverse ? seg.count - 1 : 0);
83     } //}}}
84

85     //{{{ move() method
86
public boolean move(int index)
87     {
88         if(reverse)
89             index = -index;
90
91         return ((m_index += index) < seg.count);
92     } //}}}
93

94     //{{{ Private members
95
private Segment JavaDoc seg;
96     private int m_index;
97     private boolean reverse;
98     //}}}
99
}
100
Popular Tags