Copyright test
[jalview.git] / src / com / stevesoft / pat / PartialBuffer.java
1 /*******************************************************************************
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $(date) The Jalview Authors
4  *
5  * This file is part of Jalview.
6  *  
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *   
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  *******************************************************************************/
21 //
22 // This software is now distributed according to
23 // the Lesser Gnu Public License.  Please see
24 // http://www.gnu.org/copyleft/lesser.txt for
25 // the details.
26 //    -- Happy Computing!
27 //
28 package com.stevesoft.pat;
29
30 /**
31  * This class allows you to match on a partial string. If the allowOverRun flag
32  * is true, then the length() method returns a number 1 larger than is actually
33  * contained by the class.
34  * <p>
35  * If one attempts to access the last character as follows:
36  * 
37  * <pre>
38  *  StringBuffer sb = ...;
39  *  ...
40  *  PartialBuffer pb = new PartialBuffer(sb);
41  *  char c = pb.charAt(pb.length()-1);
42  * </pre>
43  * 
44  * then two things happen. First, a zero is returned into the variable c.
45  * Second, the overRun flag is set to "true." Accessing data beyond the end of
46  * the buffer is considered an "overRun" of the data.
47  * <p>
48  * This can be helpful in determining whether more characters are required for a
49  * match to occur, as the pseudo-code below illustrates.
50  * 
51  * <pre>
52  *  int i = ...;
53  *  Regex r = new Regex(&quot;some pattern&quot;);
54  *  pb.allowOverRun = true;
55  *  pb.overRun = true;
56  *  boolean result = r.matchAt(pb,i);
57  *  if(pb.overRun) {
58  *  // The result of the match is not relevant, regardless
59  *  // of whether result is true or false.  We need to
60  *  // append more data to the buffer and try again.
61  *  ....
62  *  sb.append(more data);
63  *  }
64  * </pre>
65  */
66 class PartialBuffer implements StringLike
67 {
68   int off;
69
70   public boolean allowOverRun = true;
71
72   public boolean overRun = false;
73
74   StringBuffer sb;
75
76   PartialBuffer(StringBuffer sb)
77   {
78     this.sb = sb;
79   }
80
81   public char charAt(int n)
82   {
83     n += off;
84     if (n == sb.length())
85     {
86       overRun = true;
87       return 0;
88     }
89     return sb.charAt(n);
90   }
91
92   public int length()
93   {
94     return allowOverRun ? sb.length() + 1 : sb.length();
95   }
96
97   public int indexOf(char c)
98   {
99     for (int i = 0; i < sb.length(); i++)
100     {
101       if (sb.charAt(i) == c)
102       {
103         return i;
104       }
105     }
106     return -1;
107   }
108
109   public Object unwrap()
110   {
111     return sb;
112   }
113
114   public String substring(int i1, int i2)
115   {
116     StringBuffer sb = new StringBuffer(i2 - i1);
117     for (int i = i1; i < i2; i++)
118     {
119       sb.append(charAt(i));
120     }
121     return sb.toString();
122   }
123
124   /** Just returns null. */
125   public BasicStringBufferLike newStringBufferLike()
126   {
127     return null;
128   }
129 }