@Override public String toString(final Object obj) { return obj == null ? null : ((Chronology)obj).getId(); } }
final String id = chronology.getId(); if (str.startsWith(id + ' ')) { final Matcher matcher = CHRONO_DATE_PATTERN.matcher(str.subSequence(id.length(), str.length()));
@Override public String toString(final Object obj) { return obj == null ? null : ((Chronology)obj).getId(); } }
/** * Outputs this chronology as a {@code String}, using the ID. * * @return a string representation of this chronology, not null */ @Override public String toString() { return getId(); }
@Override public String toString(final Object obj) { return obj == null ? null : ((Chronology)obj).getId(); } }
@Override public String toString(final Object obj) { return obj == null ? null : ((Chronology)obj).getId(); } }
/** * Compares this chronology to another chronology. * <p> * The comparison order first by the chronology ID string, then by any * additional information specific to the subclass. * It is "consistent with equals", as defined by {@link Comparable}. * <p> * The default implementation compares the chronology ID. * Subclasses must compare any additional state that they store. * * @param other the other chronology to compare to, not null * @return the comparator value, negative if less, positive if greater */ @Override public int compareTo(Chronology other) { return getId().compareTo(other.getId()); }
void writeExternal(DataOutput out) throws IOException { out.writeUTF(getId()); }
/** * A hash code for this chronology. * <p> * The default implementation is based on the ID and class. * Subclasses should add any additional state that they store. * * @return a suitable hash code */ @Override public int hashCode() { return getClass().hashCode() ^ getId().hashCode(); }
private static void register(Chronology chrono) { CHRONOS_BY_ID.putIfAbsent(chrono.getId(), chrono); String type = chrono.getCalendarType(); if (type != null) { CHRONOS_BY_TYPE.putIfAbsent(type, chrono); } }
@Override public boolean print(DateTimePrintContext context, StringBuilder buf) { Chronology chrono = context.getValue(TemporalQueries.chronology()); if (chrono == null) { return false; } if (textStyle == null) { buf.append(chrono.getId()); } else { ResourceBundle bundle = ResourceBundle.getBundle( "java.bp.format.ChronologyText", context.getLocale(), DateTimeFormatterBuilder.class.getClassLoader()); try { String text = bundle.getString(chrono.getId()); buf.append(text); } catch (MissingResourceException ex) { buf.append(chrono.getId()); } } return true; }
/** * Casts the {@code Temporal} to {@code ChronoLocalDate} with the same chronology. * * @param temporal a date-time to cast, not null * @return the date-time checked and cast to {@code ChronoLocalDate}, not null * @throws ClassCastException if the date-time cannot be cast to ChronoLocalDate * or the chronology is not equal this Chrono */ <D extends ChronoLocalDate> D ensureChronoLocalDate(Temporal temporal) { @SuppressWarnings("unchecked") D other = (D) temporal; if (this.equals(other.getChronology()) == false) { throw new ClassCastException("Chrono mismatch, expected: " + getId() + ", actual: " + other.getChronology().getId()); } return other; }
/** * Casts the {@code Temporal} to {@code ChronoLocalDateTime} with the same chronology. * * @param temporal a date-time to cast, not null * @return the date-time checked and cast to {@code ChronoLocalDateTime}, not null * @throws ClassCastException if the date-time cannot be cast to ChronoLocalDateTimeImpl * or the chronology is not equal this Chrono */ <D extends ChronoLocalDate> ChronoLocalDateTimeImpl<D> ensureChronoLocalDateTime(Temporal temporal) { @SuppressWarnings("unchecked") ChronoLocalDateTimeImpl<D> other = (ChronoLocalDateTimeImpl<D>) temporal; if (this.equals(other.toLocalDate().getChronology()) == false) { throw new ClassCastException("Chrono mismatch, required: " + getId() + ", supplied: " + other.toLocalDate().getChronology().getId()); } return other; }
/** * Casts the {@code Temporal} to {@code ChronoZonedDateTimeImpl} with the same chronology. * * @param temporal a date-time to cast, not null * @return the date-time checked and cast to {@code ChronoZonedDateTimeImpl}, not null * @throws ClassCastException if the date-time cannot be cast to ChronoZonedDateTimeImpl * or the chronology is not equal this Chrono */ <D extends ChronoLocalDate> ChronoZonedDateTimeImpl<D> ensureChronoZonedDateTime(Temporal temporal) { @SuppressWarnings("unchecked") ChronoZonedDateTimeImpl<D> other = (ChronoZonedDateTimeImpl<D>) temporal; if (this.equals(other.toLocalDate().getChronology()) == false) { throw new ClassCastException("Chrono mismatch, required: " + getId() + ", supplied: " + other.toLocalDate().getChronology().getId()); } return other; }
@Override public int parse(DateTimeParseContext context, CharSequence text, int position) { // simple looping parser to find the chronology if (position < 0 || position > text.length()) { throw new IndexOutOfBoundsException(); } Set<Chronology> chronos = Chronology.getAvailableChronologies(); Chronology bestMatch = null; int matchLen = -1; for (Chronology chrono : chronos) { String id = chrono.getId(); int idLen = id.length(); if (idLen > matchLen && context.subSequenceEquals(text, position, id, 0, idLen)) { bestMatch = chrono; matchLen = idLen; } } if (bestMatch == null) { return ~position; } context.setParsed(bestMatch); return position + matchLen; } }
private static void init() { if (CHRONOS_BY_ID.isEmpty()) { register(IsoChronology.INSTANCE); register(ThaiBuddhistChronology.INSTANCE); register(MinguoChronology.INSTANCE); register(JapaneseChronology.INSTANCE); register(HijrahChronology.INSTANCE); CHRONOS_BY_ID.putIfAbsent("Hijrah", HijrahChronology.INSTANCE); CHRONOS_BY_TYPE.putIfAbsent("islamic", HijrahChronology.INSTANCE); ServiceLoader<Chronology> loader = ServiceLoader.load(Chronology.class, Chronology.class.getClassLoader()); for (Chronology chrono : loader) { CHRONOS_BY_ID.putIfAbsent(chrono.getId(), chrono); String type = chrono.getCalendarType(); if (type != null) { CHRONOS_BY_TYPE.putIfAbsent(type, chrono); } } } }
/** * A hash code for this date. * * @return a suitable hash code based only on the Chronology and the date */ @Override // override for performance public int hashCode() { return getChronology().getId().hashCode() ^ ((getProlepticYear() & 0xFFFFF800) ^ ((getProlepticYear() << 11) + (getMonth() << 6) + (getDayOfMonth()))); }
@Override public Temporal addTo(Temporal temporal) { Jdk8Methods.requireNonNull(temporal, "temporal"); Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); if (temporalChrono != null && chronology.equals(temporalChrono) == false) { throw new DateTimeException("Invalid chronology, required: " + chronology.getId() + ", but was: " + temporalChrono.getId()); } if (years != 0) { temporal = temporal.plus(years, YEARS); } if (months != 0) { temporal = temporal.plus(months, MONTHS); } if (days != 0) { temporal = temporal.plus(days, DAYS); } return temporal; }
@Override public Temporal subtractFrom(Temporal temporal) { Jdk8Methods.requireNonNull(temporal, "temporal"); Chronology temporalChrono = temporal.query(TemporalQueries.chronology()); if (temporalChrono != null && chronology.equals(temporalChrono) == false) { throw new DateTimeException("Invalid chronology, required: " + chronology.getId() + ", but was: " + temporalChrono.getId()); } if (years != 0) { temporal = temporal.minus(years, YEARS); } if (months != 0) { temporal = temporal.minus(months, MONTHS); } if (days != 0) { temporal = temporal.minus(days, DAYS); } return temporal; }
@SuppressWarnings("unchecked") @Override public ChronoDateImpl<D> plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case DAYS: return plusDays(amountToAdd); case WEEKS: return plusDays(Jdk8Methods.safeMultiply(amountToAdd, 7)); case MONTHS: return plusMonths(amountToAdd); case YEARS: return plusYears(amountToAdd); case DECADES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 10)); case CENTURIES: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 100)); case MILLENNIA: return plusYears(Jdk8Methods.safeMultiply(amountToAdd, 1000)); // case ERAS: throw new DateTimeException("Unable to add era, standard calendar system only has one era"); // case FOREVER: return (period == 0 ? this : (period > 0 ? LocalDate.MAX_DATE : LocalDate.MIN_DATE)); } throw new DateTimeException(unit + " not valid for chronology " + getChronology().getId()); } return (ChronoDateImpl<D>) getChronology().ensureChronoLocalDate(unit.addTo(this, amountToAdd)); }