EqualPat.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package kawa.lang;
  2. import gnu.mapping.*;
  3. import java.io.*;
  4. import gnu.kawa.format.ReportFormat;
  5. import gnu.lists.Consumer;
  6. import gnu.kawa.format.Printable;
  7. /**
  8. * A pattern that requires an exact match (using equal?).
  9. */
  10. public class EqualPat extends Pattern implements Printable, Externalizable
  11. {
  12. Object value;
  13. public EqualPat () { }
  14. public EqualPat (Object obj) { value = obj; }
  15. static public EqualPat make (Object obj) { return new EqualPat (obj); }
  16. public boolean match (Object obj, Object[] vars, int start_vars)
  17. {
  18. // We should be using Translator's matches routine, but the current
  19. // Translator isn't available, so here is a special-purpose kludge.
  20. if (value instanceof String && obj instanceof Symbol)
  21. obj = ((Symbol) obj).getName();
  22. return value.equals (obj);
  23. }
  24. public int varCount () { return 0; }
  25. public void print (Consumer out)
  26. {
  27. out.write("#<equals: ");
  28. ReportFormat.print(value, out);
  29. out.write('>');
  30. }
  31. /**
  32. * @serialData Write the value (using writeObject).
  33. */
  34. public void writeExternal(ObjectOutput out) throws IOException
  35. {
  36. out.writeObject(value);
  37. }
  38. public void readExternal(ObjectInput in)
  39. throws IOException, ClassNotFoundException
  40. {
  41. value = in.readObject();
  42. }
  43. }