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 java.util.Date;
  7. /**
  8. * This class implements comparisons for Dates
  9. *
  10. * @author Bill Shannon
  11. * @author John Mani
  12. */
  13. public abstract class DateTerm extends ComparisonTerm {
  14. /**
  15. * The date.
  16. *
  17. * @serial
  18. */
  19. protected Date date;
  20. /**
  21. * Constructor.
  22. * @param comparison the comparison type
  23. * @date The Date to be compared against
  24. */
  25. protected DateTerm(int comparison, Date date) {
  26. this.comparison = comparison;
  27. this.date = date;
  28. }
  29. /**
  30. * Return the Date to compare with.
  31. */
  32. public Date getDate() {
  33. return new Date(date.getTime());
  34. }
  35. /**
  36. * Return the type of comparison.
  37. */
  38. public int getComparison() {
  39. return comparison;
  40. }
  41. /**
  42. * The date comparison method.
  43. *
  44. * @param d the date in the constructor is compared with this date
  45. * @return true if the dates match, otherwise false
  46. */
  47. protected boolean match(Date d) {
  48. switch (comparison) {
  49. case LE:
  50. return d.before(date) || d.equals(date);
  51. case LT:
  52. return d.before(date);
  53. case EQ:
  54. return d.equals(date);
  55. case NE:
  56. return !d.equals(date);
  57. case GT:
  58. return d.after(date);
  59. case GE:
  60. return d.after(date) || d.equals(date);
  61. default:
  62. return false;
  63. }
  64. }
  65. /**
  66. * Equality comparison.
  67. */
  68. public boolean equals(Object obj) {
  69. if (!(obj instanceof DateTerm))
  70. return false;
  71. DateTerm dt = (DateTerm)obj;
  72. return dt.date.equals(this.date) && super.equals(obj);
  73. }
  74. /**
  75. * Compute a hashCode for this object.
  76. */
  77. public int hashCode() {
  78. return date.hashCode() + super.hashCode();
  79. }
  80. }