KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > kawa > lang > ListRepeatPat


1 package kawa.lang;
2 import gnu.mapping.*;
3 import gnu.lists.*;
4 import java.io.*;
5 import gnu.text.Printable;
6
7 public class ListRepeatPat extends Pattern implements Printable, Externalizable
8 {
9   Pattern element_pattern;
10
11   public ListRepeatPat ()
12   {
13   }
14
15   public ListRepeatPat (Pattern element_pattern)
16   {
17     this.element_pattern = element_pattern;
18   }
19
20   public static ListRepeatPat make (Pattern element_pattern)
21   {
22     return new ListRepeatPat (element_pattern);
23   }
24
25   public void print (Consumer out)
26   {
27     out.write("#<list-repeat-pattern ");
28     element_pattern.print(out);
29     out.write('>');
30   }
31
32   public boolean match (Object JavaDoc obj, Object JavaDoc[] vars, int start_vars)
33   {
34     /* DEBUGGING
35       System.err.print ("(match ");
36       print (System.err);
37       System.err.print (" on ");
38       System.err.print(obj);
39       System.err.print (")\n");
40       */

41     int length = LList.listLength(obj, false);
42     if (length < 0)
43       return false;
44
45     int var_count = element_pattern.varCount ();
46     for (int i = var_count; --i >= 0; )
47       vars[start_vars + i] = new Object JavaDoc [length];
48     Object JavaDoc[] element_vars = new Object JavaDoc [var_count];
49     for (int j = 0; j < length; j++)
50       {
51     Pair pair = (Pair) obj;
52     /* DEBUGGING
53        System.err.print ("(sub-match ["+j+"] ");
54        System.err.print(element_pattern);
55        System.err.print (" on ");
56        System.err.print(pair.car);
57        */

58
59     if (! element_pattern.match (pair.car, element_vars, 0))
60       return false;
61     for (int i = 0; i < var_count; i++)
62       ((Object JavaDoc[]) vars[start_vars + i]) [j] = element_vars[i];
63     obj = pair.cdr;
64       }
65     return true;
66   }
67
68   public int varCount () { return element_pattern.varCount (); }
69
70   /**
71    * @serialData Write the element_pattern (using writeObject).
72    */

73   public void writeExternal(ObjectOutput out) throws IOException
74   {
75     out.writeObject(element_pattern);
76   }
77
78   public void readExternal(ObjectInput in)
79     throws IOException, ClassNotFoundException JavaDoc
80   {
81     element_pattern = (Pattern) in.readObject();
82   }
83 }
84
Popular Tags