public Date calculateLeastMatchOnOrBefore(Date that) { if (equals(UNSPECIFIED)) // Performance improvement return MINIMUM_DATE; boolean matched = matches(that); GregorianCalendar gc = that.calculateGC(); if (year != UNSPECIFIED_YEAR && year < that.year) // Performance improvement gc.add(Calendar.YEAR, year - that.year + 1); Date date = new Date(gc); while (true) { if (date.sameAs(MINIMUM_DATE)) return matched ? date : null; gc.add(Calendar.DATE, -1); date.resetTo(gc); boolean b = matches(date); if (b && !matched) matched = true; else if (matched && !b) break; if (year != UNSPECIFIED_YEAR && year > date.year) // Performance improvement return null; } gc.add(Calendar.DATE, 1); date.resetTo(gc); return date; }
public Date calculateGreatestMatchOnOrBefore(Date that) { if (equals(UNSPECIFIED)) // Performance improvement return null; GregorianCalendar gc = that.calculateGC(); if (year != UNSPECIFIED_YEAR && year < that.year) // Performance improvement gc.add(Calendar.YEAR, year - that.year + 1); if (!that.sameAs(MAXIMUM_DATE)) { // Start a day ahead gc.add(Calendar.DATE, 1); } Date date = new Date(gc); boolean matched = matches(date); while (true) { gc.add(Calendar.DATE, -1); date.resetTo(gc); boolean b = matches(date); if (!b && matched) matched = false; else if (!matched && b) break; if (date.sameAs(MINIMUM_DATE)) return null; if (year != UNSPECIFIED_YEAR && year > date.year) // Performance improvement return null; } return date; }
private boolean matchDay(Date that) { if (day == UNSPECIFIED_DAY) return true; if (day == LAST_DAY_OF_MONTH) { GregorianCalendar gc = that.calculateGC(); int lastDay = gc.getActualMaximum(Calendar.DATE); return lastDay == that.day; } return day == that.day; }
public Date calculateGreatestMatchOnOrAfter(Date that) { if (equals(UNSPECIFIED)) return MAXIMUM_DATE; boolean matched = matches(that); GregorianCalendar gc = that.calculateGC(); Date date = new Date(gc); while (true) { gc.add(Calendar.DATE, 1); date.resetTo(gc); boolean b = matches(date); if (b && !matched) matched = true; else if (matched && !b) break; if (date.sameAs(MAXIMUM_DATE)) return date; } gc.add(Calendar.DATE, -1); date.resetTo(gc); return date; }
public Date calculateLeastMatchOnOrAfter(Date that) { GregorianCalendar gc = that.calculateGC(); if (!that.sameAs(MINIMUM_DATE)) { // Start a day behind gc.add(Calendar.DATE, -1); } Date date = new Date(gc); boolean matched = matches(date); while (true) { gc.add(Calendar.DATE, 1); date.resetTo(gc); boolean b = matches(date); if (!b && matched) matched = false; else if (!matched && b) break; if (date.sameAs(MAXIMUM_DATE)) return null; } return date; }
public boolean matches(Date date) { if (this == DayOfWeek.UNSPECIFIED) return true; DayOfWeek thatDow = date.getDayOfWeek(); // Tolerate the day of week of the given date being unspecified. if (thatDow.isSpecific()) return this == thatDow; // Otherwise we need to calculate the given date's day of week. return equals(forDate(date)); }
@Override public int compareTo(Date that) { if (!isSpecific()) throw new BACnetRuntimeException( "Comparisons can only be made between specific dates: " + this); if (!that.isSpecific()) throw new BACnetRuntimeException( "Comparisons can only be made between specific dates: " + that); if (year == that.year) { if (month == that.month) return day - that.day; return month.ordinal() - that.month.ordinal(); } return year - that.year; }
/** * Matches this presumably wildcard date with a (that) necessarily specifically defined date to * determine if (true) the given date is one of this' defined dates or (false) not. * * @param that the specific date with which to compare. * @return */ @Override public boolean matches(Date that) { if (!that.isSpecific()) throw new BACnetRuntimeException("Dates for matching must be completely specified: " + that); if (!matchYear(that.year)) return false; if (!month.matches(that.month)) return false; if (!matchDay(that)) return false; if (!dayOfWeek.matches(that)) return false; return true; }
public static DayOfWeek forDate(Date date) { GregorianCalendar gc = date.calculateGC(); int calendarDow = gc.get(Calendar.DAY_OF_WEEK); return forCalendarDow(calendarDow); }