@Override public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) { this.facesContext = facesContext; Class<?> type = ValueExpressionHelper.getValueType(facesContext, uiComponent, Lists.<Class<?>> newArrayList(DateTime.class, LocalDate.class, LocalTime.class)); Preconditions.checkArgument(type != null, "DateTimeConverter is not attached to a component bound to either a" + " DateTime, LocalDate, or LocalTime."); if (value instanceof LocalDate) { LocalDate localDate = (LocalDate) value; return getAsStringValue(facesContext, uiComponent, localDate.toDateTimeAtStartOfDay(getTimeZone())); } if (value instanceof LocalTime) { LocalTime localTime = (LocalTime) value; return getAsStringValue(facesContext, uiComponent, localTime.toDateTimeToday(getTimeZone())); } if (value instanceof ReadablePartial) { ReadablePartial readablePartial = (ReadablePartial) value; return getAsStringValue(facesContext, uiComponent, readablePartial.toDateTime(new DateTime())); } this.facesContext = null; return getAsStringValue(facesContext, uiComponent, value); }
/** * Calculates the number of whole units between the two specified partial datetimes. * <p> * The two partials must contain the same fields, for example you can specify * two <code>LocalDate</code> objects. * * @param start the start partial date, validated to not be null * @param end the end partial date, validated to not be null * @param zeroInstance the zero instance constant, must not be null * @return the period * @throws IllegalArgumentException if the partials are null or invalid */ protected static int between(ReadablePartial start, ReadablePartial end, ReadablePeriod zeroInstance) { if (start == null || end == null) { throw new IllegalArgumentException("ReadablePartial objects must not be null"); } if (start.size() != end.size()) { throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields"); } for (int i = 0, isize = start.size(); i < isize; i++) { if (start.getFieldType(i) != end.getFieldType(i)) { throw new IllegalArgumentException("ReadablePartial objects must have the same set of fields"); } } if (DateTimeUtils.isContiguous(start) == false) { throw new IllegalArgumentException("ReadablePartial objects must be contiguous"); } Chronology chrono = DateTimeUtils.getChronology(start.getChronology()).withUTC(); int[] values = chrono.get(zeroInstance, chrono.set(start, 0L), chrono.set(end, 0L)); return values[0]; }
/** * Compares this ReadablePartial with another returning true if the chronology, * field types and values are equal. * * @param partial an object to check against * @return true if fields and values are equal */ public boolean equals(Object partial) { if (this == partial) { return true; } if (partial instanceof ReadablePartial == false) { return false; } ReadablePartial other = (ReadablePartial) partial; if (size() != other.size()) { return false; } for (int i = 0, isize = size(); i < isize; i++) { if (getValue(i) != other.getValue(i) || getFieldType(i) != other.getFieldType(i)) { return false; } } return FieldUtils.equals(getChronology(), other.getChronology()); }
/** * Compares this partial with another returning an integer * indicating the order. * <p> * The fields are compared in order, from largest to smallest. * The first field that is non-equal is used to determine the result. * <p> * The specified object must be a partial instance whose field types * match those of this partial. * <p> * NOTE: Prior to v2.0, the {@code Comparable} interface was only implemented * in this class and not in the {@code ReadablePartial} interface. * * @param other an object to check against * @return negative if this is less, zero if equal, positive if greater * @throws ClassCastException if the partial is the wrong class * or if it has field types that don't match * @throws NullPointerException if the partial is null * @since 1.1 */ public int compareTo(ReadablePartial other) { if (this == other) { return 0; } if (size() != other.size()) { throw new ClassCastException("ReadablePartial objects must have matching field types"); } for (int i = 0, isize = size(); i < isize; i++) { if (getFieldType(i) != other.getFieldType(i)) { throw new ClassCastException("ReadablePartial objects must have matching field types"); } } // fields are ordered largest first for (int i = 0, isize = size(); i < isize; i++) { if (getValue(i) > other.getValue(i)) { return 1; } if (getValue(i) < other.getValue(i)) { return -1; } } return 0; }
public PointInTime parsePointInTime(String text) { if (getParser() == null) { throw new UnsupportedOperationException("Parsing not supported"); } text = truncateTimeZone(text); Chronology chrono = selectChronology(); PointInTimeParserBucket bucket = new PointInTimeParserBucket(0, chrono, getLocale()); int newPos = getParser().parseInto(bucket, text, 0); if (newPos >= 0) { if (newPos >= text.length()) { ReadablePartial partial = bucket.toPartial(); return new PointInTime(partial); } } else { newPos = ~newPos; } throw new IllegalArgumentException(createErrorMessage(text, newPos)); }
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { DateTimePrinter[] elements = iPrinters; if (elements == null) { throw new UnsupportedOperationException(); } if (locale == null) { // Guard against default locale changing concurrently. locale = Locale.getDefault(); } int len = elements.length; for (int i=0; i<len; i++) { elements[i].printTo(buf, partial, locale); } }
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException { DateTimePrinter[] elements = iPrinters; if (elements == null) { throw new UnsupportedOperationException(); } if (locale == null) { // Guard against default locale changing concurrently. locale = Locale.getDefault(); } int len = elements.length; for (int i=0; i<len; i++) { elements[i].printTo(out, partial, locale); } }
@Override public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { if (hasMilliSecondPrecision) { buf.append(String.valueOf(getDateTimeMillis(partial))); } else { buf.append(String.valueOf(getDateTimeMillis(partial) / 1000)); } }
@Override public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException { if (hasMilliSecondPrecision) { out.append(String.valueOf(getDateTimeMillis(partial))); } else { out.append(String.valueOf(getDateTimeMillis(partial) / 1000)); } }
private long getDateTimeMillis(ReadablePartial partial) { int year = partial.get(DateTimeFieldType.year()); int monthOfYear = partial.get(DateTimeFieldType.monthOfYear()); int dayOfMonth = partial.get(DateTimeFieldType.dayOfMonth()); int hourOfDay = partial.get(DateTimeFieldType.hourOfDay()); int minuteOfHour = partial.get(DateTimeFieldType.minuteOfHour()); int secondOfMinute = partial.get(DateTimeFieldType.secondOfMinute()); int millisOfSecond = partial.get(DateTimeFieldType.millisOfSecond()); return partial.getChronology().getDateTimeMillis(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, millisOfSecond); }
@Override public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) { DateTimeFormatter formatter = getFormatter(annotation, fieldType); if (ReadablePartial.class.isAssignableFrom(fieldType)) { return new ReadablePartialPrinter(formatter); } else if (ReadableInstant.class.isAssignableFrom(fieldType) || Calendar.class.isAssignableFrom(fieldType)) { // assumes Calendar->ReadableInstant converter is registered return new ReadableInstantPrinter(formatter); } else { // assumes Date->Long converter is registered return new MillisecondInstantPrinter(formatter); } }
@Override public int getMaximumValue(ReadablePartial partial) { if (partial.isSupported(DateTimeFieldType.monthOfYear())) { int month = partial.get(DateTimeFieldType.monthOfYear()); return this.daysInMonth[month - 1]; // Months are 1-based } return this.getMaximumValue(); }
@Override public int getMaximumValue(ReadablePartial partial, int[] values) { int size = partial.size(); for (int i = 0; i < size; i++) { if (partial.getFieldType(i) == DateTimeFieldType.monthOfYear()) { int month = values[i]; return this.daysInMonth[month - 1]; } } return this.getMaximumValue(); }
/** * See {@link android.text.format.DateUtils#isToday} for full docs. * * @return true if the supplied when is today else false */ public static boolean isToday(ReadablePartial time) { if (!time.isSupported(DateTimeFieldType.dayOfMonth()) || !time.isSupported(DateTimeFieldType.monthOfYear()) || !time.isSupported(DateTimeFieldType.year())) { throw new IllegalArgumentException("isToday() must be passed a ReadablePartial that supports day of " + "month, month of year and year."); } LocalDate localDate = time instanceof LocalDate ? (LocalDate) time : new LocalDate(time); return LocalDate.now().compareTo(localDate) == 0; }
/** * Return string describing the time until/elapsed time since 'time' formatted like * "[relative time/date], [time]". * * See {@link android.text.format.DateUtils#getRelativeDateTimeString} for full docs. * * @throws IllegalArgumentException if using a ReadablePartial without a time component * @see #getRelativeDateTimeString(Context, ReadableInstant, ReadablePeriod, int) */ public static CharSequence getRelativeDateTimeString(Context context, ReadablePartial time, ReadablePeriod transitionResolution, int flags) { if (!time.isSupported(DateTimeFieldType.hourOfDay()) || !time.isSupported(DateTimeFieldType.minuteOfHour())) { throw new IllegalArgumentException("getRelativeDateTimeString() must be passed a ReadablePartial that " + "supports time, otherwise it makes no sense"); } return getRelativeDateTimeString(context, time.toDateTime(DateTime.now()), transitionResolution, flags); }
protected int getValue(ReadablePartial partial) { try { int value = partial.get(type); if (value < 0) { value = -value; } return value; } catch (RuntimeException e) { return -1; } }
@Test public void testToPartialForYearMonthDayHour() { ReadablePartial p = new Partial().with(DateTimeFieldType.year(), 1975).with(DateTimeFieldType.monthOfYear(), 7) .with(DateTimeFieldType.dayOfMonth(), 23).with(DateTimeFieldType.hourOfDay(), 15); ReadablePartial t = new PointInTime(1975, 7, 23, 15); Assert.assertTrue(p.equals(t)); }
/** * Compare this field to the same field on another partial instant. * <p> * The comparison is based on the value of the same field type, irrespective * of any difference in chronology. Thus, if this property represents the * hourOfDay field, then the hourOfDay field of the other partial will be queried * whether in the same chronology or not. * * @param partial the partial to compare to * @return negative value if this is less, 0 if equal, or positive value if greater * @throws IllegalArgumentException if the instant is null * @throws IllegalArgumentException if the field of this property cannot be queried * on the specified instant */ public int compareTo(ReadablePartial partial) { if (partial == null) { throw new IllegalArgumentException("The instant must not be null"); } int thisValue = get(); int otherValue = partial.get(getFieldType()); if (thisValue < otherValue) { return -1; } else if (thisValue > otherValue) { return 1; } else { return 0; } }
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { for (DateTimeFieldType type : contingentOnFields) { if (partial.isSupported(type)) { buf.append(literal); return; } } }
/** * Compare this field to the same field on another partial instant. * <p> * The comparison is based on the value of the same field type, irrespective * of any difference in chronology. Thus, if this property represents the * hourOfDay field, then the hourOfDay field of the other partial will be queried * whether in the same chronology or not. * * @param partial the partial to compare to * @return negative value if this is less, 0 if equal, or positive value if greater * @throws IllegalArgumentException if the partial is null * @throws IllegalArgumentException if the partial doesn't support this field */ public int compareTo(ReadablePartial partial) { if (partial == null) { throw new IllegalArgumentException("The partial must not be null"); } int thisValue = get(); int otherValue = partial.get(getFieldType()); if (thisValue < otherValue) { return -1; } else if (thisValue > otherValue) { return 1; } else { return 0; } }
@Override public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { int value = getValue(partial); if (value < 0) { if (fillChar != null) { buf.append(fillChar); buf.append(fillChar); } } else { super.printTo(buf, partial, locale); } }
@Test public void testToPartialForYearMonthDayHourMinuteSecond() { ReadablePartial p = new Partial().with(DateTimeFieldType.year(), 1975).with(DateTimeFieldType.monthOfYear(), 7) .with(DateTimeFieldType.dayOfMonth(), 23).with(DateTimeFieldType.hourOfDay(), 15) .with(DateTimeFieldType.minuteOfHour(), 23).with(DateTimeFieldType.secondOfMinute(), 42); ReadablePartial t = new PointInTime(1975, 7, 23, 15, 23, 42); Assert.assertTrue(p.equals(t)); }
@Override public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException { int value = getValue(partial); if (value < 0) { if (fillChar != null) { out.write(fillChar); out.write(fillChar); } } else { super.printTo(out, partial, locale); } }
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { if (partial.isSupported(iFieldType)) { try { FormatUtils.appendUnpaddedInteger(buf, partial.get(iFieldType)); } catch (RuntimeException e) { buf.append('\ufffd'); } } else { buf.append('\ufffd'); } }
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException { if (partial.isSupported(iFieldType)) { try { FormatUtils.writeUnpaddedInteger(out, partial.get(iFieldType)); } catch (RuntimeException e) { out.write('\ufffd'); } } else { out.write('\ufffd'); } }
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { if (partial.isSupported(iFieldType)) { try { FormatUtils.appendPaddedInteger(buf, partial.get(iFieldType), iMinPrintedDigits); } catch (RuntimeException e) { appendUnknownString(buf, iMinPrintedDigits); } } else { appendUnknownString(buf, iMinPrintedDigits); } }
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException { if (partial.isSupported(iFieldType)) { try { FormatUtils.writePaddedInteger(out, partial.get(iFieldType), iMinPrintedDigits); } catch (RuntimeException e) { printUnknownString(out, iMinPrintedDigits); } } else { printUnknownString(out, iMinPrintedDigits); } }
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { int year = getTwoDigitYear(partial); if (year < 0) { buf.append('\ufffd'); buf.append('\ufffd'); } else { FormatUtils.appendPaddedInteger(buf, year, 2); } }
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException { int year = getTwoDigitYear(partial); if (year < 0) { out.write('\ufffd'); out.write('\ufffd'); } else { FormatUtils.writePaddedInteger(out, year, 2); } }
/** * Gets the values of a partial from an instant. * * @param partial the partial instant to use * @param instant the instant to query * @return the values of the partial extracted from the instant */ public int[] get(ReadablePartial partial, long instant) { int size = partial.size(); int[] values = new int[size]; for (int i = 0; i < size; i++) { values[i] = partial.getFieldType(i).getField(this).get(instant); } return values; }
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { try { buf.append(print(partial, locale)); } catch (RuntimeException e) { buf.append('\ufffd'); } }
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException { try { out.write(print(partial, locale)); } catch (RuntimeException e) { out.write('\ufffd'); } }
@Test public void testToPartialForYear() { ReadablePartial p = new Partial().with(DateTimeFieldType.year(), 1975); ReadablePartial t = new PointInTime(1975); Assert.assertEquals(1, t.size()); Assert.assertTrue(t.isSupported(DateTimeFieldType.year())); Assert.assertEquals(1975, t.get(DateTimeFieldType.year())); Assert.assertTrue(p.equals(t)); }
public void printTo(StringBuffer buf, ReadablePartial partial, Locale locale) { // removed check whether field is supported, as input field is typically // secondOfDay which is unsupported by TimeOfDay long millis = partial.getChronology().set(partial, 0L); try { printTo(buf, null, millis, partial.getChronology()); } catch (IOException e) { // Not gonna happen. } }
public void printTo(Writer out, ReadablePartial partial, Locale locale) throws IOException { for (DateTimeFieldType type : contingentOnFields) { if (partial.isSupported(type)) { out.write(literal); return; } } }