KickJava   Java API By Example, From Geeks To Geeks.

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


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

21 package org.gjt.sp.util;
22
23 import java.io.Serializable JavaDoc;
24 import javax.swing.text.Segment JavaDoc;
25
26 /**
27  * Class that lets java.util.regex search within a javax.swing.text.Segment.
28  *
29  * @author Marcelo Vanzin
30  */

31 public class SegmentCharSequence implements CharSequence JavaDoc, Serializable JavaDoc
32 {
33
34     public SegmentCharSequence(Segment JavaDoc seg)
35     {
36         this(seg, false);
37     }
38
39     public SegmentCharSequence(Segment JavaDoc seg, boolean reverse)
40     {
41         this(seg, 0, seg.count);
42         this.reverse = reverse;
43     }
44
45     public SegmentCharSequence(Segment JavaDoc seg, int off, int len)
46     {
47         this.offset = off;
48         this.length = len;
49         this.seg = seg;
50     }
51
52     public char charAt(int index)
53     {
54         if (reverse)
55             index = length - index - 1;
56         return seg.array[seg.offset + offset + index];
57     }
58
59     public int length()
60     {
61         return length;
62     }
63
64     public CharSequence JavaDoc subSequence(int start, int end)
65     {
66         if (reverse)
67             throw new IllegalStateException JavaDoc("reverse sub-sequences are not supported");
68         return new SegmentCharSequence(seg, offset + start, end - start);
69     }
70
71     public String JavaDoc toString()
72     {
73         return new String JavaDoc(seg.array, offset+seg.offset, length);
74     }
75
76     private boolean reverse;
77     private int offset;
78     private int length;
79     private Segment JavaDoc seg;
80
81 }
82
83
Popular Tags