PairWithPosition.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package gnu.lists;
  2. import java.io.*;
  3. import gnu.text.SourceLocator;
  4. import gnu.text.SourceMapper;
  5. /** A <code>Pair</code> with the file name and position it was read from.
  6. * Note the position (start/end range) is actually that of the car part. */
  7. public class PairWithPosition extends ImmutablePair
  8. implements gnu.text.SourceLocator
  9. {
  10. String filename; // Future: union(String,SourceMapper)
  11. long position; // See SourceMapper#simpleEncode
  12. public final void setFile (String filename)
  13. {
  14. this.filename = filename;
  15. }
  16. public final void setLine(int lineno, int colno) {
  17. position = SourceMapper.simpleEncode(lineno, colno);
  18. }
  19. public final void setEndLine(int endline, int endcolumn) {
  20. position = SourceMapper.simpleEncode(getStartLine(), getStartColumn(),
  21. endline, endcolumn);
  22. }
  23. public final void setLine (int lineno)
  24. {
  25. setLine (lineno, 0);
  26. }
  27. public final String getFileName ()
  28. {
  29. return filename;
  30. }
  31. public String getPublicId ()
  32. {
  33. return null;
  34. }
  35. public String getSystemId ()
  36. {
  37. return filename;
  38. }
  39. public final int getLineNumber() {
  40. return SourceMapper.simpleStartLine(position);
  41. }
  42. public final int getColumnNumber() {
  43. return SourceMapper.simpleStartColumn(position);
  44. }
  45. public final int getStartLine() {
  46. return SourceMapper.simpleStartLine(position);
  47. }
  48. public final int getStartColumn() {
  49. return SourceMapper.simpleStartColumn(position);
  50. }
  51. public final int getEndLine() {
  52. return SourceMapper.simpleEndLine(position);
  53. }
  54. public final int getEndColumn() {
  55. return SourceMapper.simpleEndColumn(position);
  56. }
  57. public boolean isStableSourceLocation() { return true; }
  58. /** Only for serialization. */
  59. public PairWithPosition ()
  60. {
  61. }
  62. public PairWithPosition (SourceLocator where,
  63. Object car, Object cdr)
  64. {
  65. super (car, cdr);
  66. filename = where.getFileName();
  67. this.position = SourceMapper.simpleEncode(where.getStartLine(), where.getStartColumn(),
  68. where.getEndLine(), where.getEndColumn());
  69. }
  70. public PairWithPosition (Object car, Object cdr)
  71. {
  72. super (car, cdr);
  73. }
  74. public static PairWithPosition make(Object car, Object cdr,
  75. String filename, int line, int column)
  76. {
  77. PairWithPosition pair = new PairWithPosition(car, cdr);
  78. pair.filename = filename;
  79. pair.setLine(line, column);
  80. return pair;
  81. }
  82. public static PairWithPosition make(Object car, Object cdr,
  83. String filename, long position)
  84. {
  85. PairWithPosition pair = new PairWithPosition(car, cdr);
  86. pair.filename = filename;
  87. pair.position = position;
  88. return pair;
  89. }
  90. /** Should only be used when initializing a PairWithPosition instance. */
  91. public void init(Object car, Object cdr,
  92. String filename, long position) {
  93. this.car = car;
  94. this.cdr = cdr;
  95. this.filename = filename;
  96. this.position = position;
  97. }
  98. /**
  99. * @serialData Write the car followed by the cdr,
  100. * followed by filename (as an {@code Object}, so it can be shared),
  101. * followed by position (see SourceMapper#simpleEncode}.
  102. */
  103. public void writeExternal(ObjectOutput out) throws IOException
  104. {
  105. out.writeObject(car);
  106. out.writeObject(cdr);
  107. out.writeObject(filename);
  108. out.writeLong(position);
  109. }
  110. public void readExternal(ObjectInput in)
  111. throws IOException, ClassNotFoundException {
  112. Object car = in.readObject();
  113. Object cdr = in.readObject();
  114. String filename = (String) in.readObject();
  115. long position = in.readLong();
  116. init(car, cdr, filename, position);
  117. }
  118. }