1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail.search;
  6. import javax.mail.*;
  7. /**
  8. * This class implements comparisons for Message Flags.
  9. *
  10. * @author Bill Shannon
  11. * @author John Mani
  12. */
  13. public final class FlagTerm extends SearchTerm {
  14. /**
  15. * Indicates whether to test for the presence or
  16. * absence of the specified Flag. If <code>true</code>,
  17. * then test whether all the specified flags are present, else
  18. * test whether all the specified flags are absent.
  19. *
  20. * @serial
  21. */
  22. protected boolean set;
  23. /**
  24. * Flags object containing the flags to test.
  25. *
  26. * @serial
  27. */
  28. protected Flags flags;
  29. /**
  30. * Constructor.
  31. *
  32. * @param flags Flags object containing the flags to check for
  33. * @param set the flag setting to check for
  34. */
  35. public FlagTerm(Flags flags, boolean set) {
  36. this.flags = flags;
  37. this.set = set;
  38. }
  39. /**
  40. * Return the Flags to test.
  41. */
  42. public Flags getFlags() {
  43. return (Flags)flags.clone();
  44. }
  45. /**
  46. * Return true if testing whether the flags are set.
  47. */
  48. public boolean getTestSet() {
  49. return set;
  50. }
  51. /**
  52. * The comparison method.
  53. *
  54. * @param msg The flag comparison is applied to this Message
  55. * @return true if the comparson succeeds, otherwise false.
  56. */
  57. public boolean match(Message msg) {
  58. try {
  59. Flags f = msg.getFlags();
  60. if (set) { // This is easy
  61. if (f.contains(flags))
  62. return true;
  63. else
  64. return false;
  65. }
  66. // Return true if ALL flags in the passed in Flags
  67. // object are NOT set in this Message.
  68. // Got to do this the hard way ...
  69. Flags.Flag[] sf = flags.getSystemFlags();
  70. // Check each flag in the passed in Flags object
  71. for (int i = 0; i < sf.length; i++) {
  72. if (f.contains(sf[i]))
  73. // this flag IS set in this Message, get out.
  74. return false;
  75. }
  76. String[] s = flags.getUserFlags();
  77. // Check each flag in the passed in Flags object
  78. for (int i = 0; i < s.length; i++) {
  79. if (f.contains(s[i]))
  80. // this flag IS set in this Message, get out.
  81. return false;
  82. }
  83. return true;
  84. } catch (Exception e) {
  85. return false;
  86. }
  87. }
  88. /**
  89. * Equality comparison.
  90. */
  91. public boolean equals(Object obj) {
  92. if (!(obj instanceof FlagTerm))
  93. return false;
  94. FlagTerm ft = (FlagTerm)obj;
  95. return ft.set == this.set && ft.flags.equals(this.flags);
  96. }
  97. /**
  98. * Compute a hashCode for this object.
  99. */
  100. public int hashCode() {
  101. return set ? flags.hashCode() : ~flags.hashCode();
  102. }
  103. }