KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > SubSequence


1 // Copyright (c) 2001, 2002, 2003 Per M.A. Bothner and Brainfood Inc.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.lists;
5
6 /** A sequence consisting of a sub-range of the elements of a base sequence.
7  * The start and end positions are positions triples (on the same sequence).
8  */

9
10 public class SubSequence
11 extends AbstractSequence implements Sequence
12 {
13   /** Normally the Sequence this a sub-sequence of.
14    * Actually the sequence that provides context for the
15    * start and end position pairs. */

16   AbstractSequence base;
17
18   /** Integer part of start position. */
19   int ipos0;
20
21   /** Integer part of end position. */
22   int ipos1;
23
24   public SubSequence()
25   {
26   }
27
28   public SubSequence(AbstractSequence base, int startPos, int endPos)
29   {
30     this.base = base;
31     this.ipos0 = startPos;
32     this.ipos1 = endPos;
33   }
34
35   public SubSequence(AbstractSequence base)
36   {
37     this.base = base;
38   }
39
40   public Object JavaDoc get (int index)
41   {
42     if (index < 0 || index >=size())
43       throw new IndexOutOfBoundsException JavaDoc();
44     int start = base.nextIndex(ipos0);
45     return base.get(start + index);
46   }
47
48   public int size()
49   {
50     return base.getIndexDifference(ipos1, ipos0);
51   }
52
53   public void removePosRange(int istart, int iend)
54   {
55     base.removePosRange(istart == 0 ? ipos0 : istart == -1 ? ipos1 : istart,
56             iend == -1 ? ipos1 : iend == 0 ? ipos0 : iend);
57   }
58
59   protected boolean isAfterPos(int ipos)
60   { return base.isAfterPos(ipos); }
61
62   public int createPos(int offset, boolean isAfter)
63   {
64     return base.createRelativePos(ipos0, offset, isAfter);
65   }
66
67   public int createRelativePos(int pos, int offset, boolean isAfter)
68   {
69     return base.createRelativePos(pos, offset, isAfter);
70   }
71
72   protected int getIndexDifference(int ipos1, int ipos0)
73   {
74     return base.getIndexDifference(ipos1, ipos0);
75   }
76
77   public void releasePos(int ipos)
78   {
79     base.releasePos(ipos);
80   }
81
82   protected int nextIndex (int ipos)
83   {
84     return getIndexDifference(ipos, ipos0);
85   }
86
87   public int compare (int ipos1, int ipos2)
88   {
89     return base.compare(ipos1, ipos2);
90   }
91
92   public Object JavaDoc getPosNext(int ipos)
93   {
94     if (base.compare(ipos, ipos1) >= 0)
95       return eofValue;
96     return base.getPosNext(ipos);
97   }
98
99   public int getNextKind(int ipos)
100   {
101     if (base.compare(ipos, ipos1) >= 0)
102       return EOF_VALUE;
103     return base.getNextKind(ipos);
104   }
105
106   public Object JavaDoc getPosPrevious(int ipos)
107   {
108     if (base.compare(ipos, ipos0) <= 0)
109       return eofValue;
110     return base.getPosPrevious(ipos);
111   }
112
113   public void clear()
114   {
115     removePosRange(ipos0, ipos1);
116   }
117
118   public void finalize()
119   {
120     base.releasePos(ipos0);
121     base.releasePos(ipos1);
122   }
123 }
124
Popular Tags