Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / src / com / stevesoft / pat / Bracket.java
1 //
2 // This software is now distributed according to
3 // the Lesser Gnu Public License.  Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
5 // the details.
6 //    -- Happy Computing!
7 //
8 package com.stevesoft.pat;
9
10 import java.util.Hashtable;
11 import java.util.Vector;
12
13 /**
14  * The Bracket is a form of the Or class, implements the pattern element [ ].
15  */
16 class Bracket extends Or
17 {
18   boolean neg;
19
20   Bracket(boolean n)
21   {
22     neg = n;
23   }
24
25   String leftForm()
26   {
27     if (neg)
28     {
29       return "[^";
30     }
31     else
32     {
33       return "[";
34     }
35   }
36
37   String rightForm()
38   {
39     return "]";
40   }
41
42   String sepForm()
43   {
44     return "";
45   }
46
47   public int matchInternal(int pos, Pthings pt)
48   {
49     if (pos >= pt.src.length())
50     {
51       return -1;
52     }
53     int r = super.matchInternal(pos, pt);
54     if ((neg && r < 0) || (!neg && r >= 0))
55     {
56       return nextMatch(pos + 1, pt);
57     }
58     return -1;
59   }
60
61   public patInt minChars()
62   {
63     return new patInt(1);
64   }
65
66   public patInt maxChars()
67   {
68     return new patInt(1);
69   }
70
71   public Or addOr(Pattern p)
72   {
73     pv = null;
74     v.addElement(p);
75     p.setParent(null);
76     return this;
77   }
78
79   public Pattern clone1(Hashtable h)
80   {
81     Bracket b = new Bracket(neg);
82     b.v = new Vector();
83     for (int i = 0; i < v.size(); i++)
84     {
85       b.v.addElement(((Pattern) v.elementAt(i)).clone1(h));
86     }
87     return b;
88   }
89 };