Remove redundancy in Eclipse
[jalview.git] / src / com / stevesoft / pat / PartialBuffer.java
1 //\r
2 // This software is now distributed according to\r
3 // the Lesser Gnu Public License.  Please see\r
4 // http://www.gnu.org/copyleft/lesser.txt for\r
5 // the details.\r
6 //    -- Happy Computing!\r
7 //\r
8 package com.stevesoft.pat;\r
9 \r
10 \r
11 /** This class allows you to match on a partial string.\r
12     If the allowOverRun flag is true, then the\r
13     length() method returns a number 1 larger than\r
14     is actually contained by the class.\r
15     <p>\r
16     If one attempts to access the last character as\r
17     follows:\r
18     <pre>\r
19     StringBuffer sb = ...;\r
20     ...\r
21     PartialBuffer pb = new PartialBuffer(sb);\r
22     char c = pb.charAt(pb.length()-1);\r
23     </pre>\r
24     then two things happen.  First, a zero is returned\r
25     into the variable c.  Second, the overRun flag is\r
26     set to "true."  Accessing data beyond the end of\r
27     the buffer is considered an "overRun" of the data.\r
28     <p>\r
29     This can be helpful in determining whether more\r
30     characters are required for a match to occur, as\r
31     the pseudo-code below illustrates.\r
32     <pre>\r
33     int i = ...;\r
34     Regex r = new Regex("some pattern");\r
35     pb.allowOverRun = true;\r
36     pb.overRun = true;\r
37     boolean result = r.matchAt(pb,i);\r
38     if(pb.overRun) {\r
39       // The result of the match is not relevant, regardless\r
40       // of whether result is true or false.  We need to\r
41       // append more data to the buffer and try again.\r
42       ....\r
43       sb.append(more data);\r
44     }\r
45     </pre>\r
46     */\r
47 class PartialBuffer implements StringLike {\r
48   int off;\r
49   public boolean allowOverRun = true;\r
50   public boolean overRun = false;\r
51   StringBuffer sb;\r
52   PartialBuffer(StringBuffer sb) {\r
53     this.sb = sb;\r
54   }\r
55   public char charAt(int n) {\r
56     n += off;\r
57     if(n == sb.length()) {\r
58       overRun = true;\r
59       return 0;\r
60     }\r
61     return sb.charAt(n);\r
62   }\r
63   public int length() {\r
64     return allowOverRun ? sb.length()+1 : sb.length();\r
65   }\r
66   public int indexOf(char c) {\r
67     for(int i=0;i<sb.length();i++)\r
68       if(sb.charAt(i)==c)\r
69         return i;\r
70     return -1;\r
71   }\r
72   public Object unwrap() { return sb; }\r
73   public String substring(int i1,int i2) {\r
74     StringBuffer sb = new StringBuffer(i2-i1);\r
75     for(int i=i1;i<i2;i++)\r
76       sb.append(charAt(i));\r
77     return sb.toString();\r
78   }\r
79   /** Just returns null. */\r
80   public BasicStringBufferLike newStringBufferLike() {\r
81     return null;\r
82   }\r
83 }\r