KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > PairWithPosition


1 package gnu.lists;
2 import java.io.*;
3 import gnu.text.SourceLocator;
4
5 /** A <code>Pair</code> with the file name and position it was read from. */
6
7 public class PairWithPosition extends Pair
8   implements gnu.text.SourceLocator
9 {
10   String JavaDoc filename;
11   /** An encoding of lineNumber+(columnNumber<<20).
12    * Note if columnNumber is unspecified (0), then position is lineNumber. */

13   int position;
14
15   public final void setFile (String JavaDoc filename)
16   {
17     this.filename = filename;
18   }
19
20   public final void setLine (int lineno, int colno)
21   {
22     if (lineno < 0)
23       lineno = 0;
24     if (colno < 0)
25       colno = 0;
26     position = (lineno << 12) + colno;
27   }
28
29   public final void setLine (int lineno)
30   {
31     setLine (lineno, 0);
32   }
33
34   public final String JavaDoc getFileName ()
35   {
36     return filename;
37   }
38
39   public String JavaDoc getPublicId ()
40   {
41     return null;
42   }
43
44   public String JavaDoc getSystemId ()
45   {
46     return filename;
47   }
48
49   /** Get the line number of (the start of) this Expression.
50     * The "first" line is line 1; unknown is -1. */

51   public final int getLineNumber()
52   {
53     int line = position >> 12;
54     return line == 0 ? -1 : line;
55   }
56
57   public final int getColumnNumber()
58   {
59     int column = position & ((1 << 12) - 1);
60     return column == 0 ? -1 : column;
61   }
62
63   public boolean isStableSourceLocation() { return true; }
64
65   /** Only for serialization. */
66   public PairWithPosition ()
67   {
68   }
69
70   public PairWithPosition (SourceLocator where,
71                            Object JavaDoc car, Object JavaDoc cdr)
72   {
73     super (car, cdr);
74     filename = where.getFileName();
75     setLine(where.getLineNumber(), where.getColumnNumber());
76   }
77
78   public PairWithPosition (Object JavaDoc car, Object JavaDoc cdr)
79   {
80     super (car, cdr);
81   }
82
83   public static PairWithPosition make(Object JavaDoc car, Object JavaDoc cdr,
84                       String JavaDoc filename, int line, int column)
85   {
86     PairWithPosition pair = new PairWithPosition(car, cdr);
87     pair.filename = filename;
88     pair.setLine(line, column);
89     return pair;
90   }
91
92   public static PairWithPosition make(Object JavaDoc car, Object JavaDoc cdr,
93                       String JavaDoc filename, int position)
94   {
95     PairWithPosition pair = new PairWithPosition(car, cdr);
96     pair.filename = filename;
97     pair.position = position;
98     return pair;
99   }
100
101   /**
102    * @serialData Write the car followed by the cdr,
103    * followed by filename (as an Object, so it can be shared),
104    * followed by position (line|(column<<20)).
105    */

106   public void writeExternal(ObjectOutput out) throws IOException
107   {
108     out.writeObject(car);
109     out.writeObject(cdr);
110     out.writeObject(filename);
111     out.writeInt(position);
112   }
113
114   public void readExternal(ObjectInput in)
115     throws IOException, ClassNotFoundException JavaDoc
116   {
117     car = in.readObject();
118     cdr = in.readObject();
119     filename = (String JavaDoc) in.readObject();
120     position = in.readInt();
121   }
122 }
123
Popular Tags