src/share/classes/java/time/format/DateTimePrintContext.java

Print this page




  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time.format;
  63 
  64 import static java.time.temporal.ChronoField.EPOCH_DAY;
  65 import static java.time.temporal.ChronoField.INSTANT_SECONDS;
  66 
  67 import java.time.DateTimeException;
  68 import java.time.Instant;
  69 import java.time.ZoneId;
  70 import java.time.temporal.Chrono;
  71 import java.time.temporal.ChronoField;
  72 import java.time.temporal.ChronoLocalDate;
  73 import java.time.temporal.Queries;
  74 import java.time.temporal.TemporalAccessor;
  75 import java.time.temporal.TemporalField;
  76 import java.time.temporal.TemporalQuery;
  77 import java.time.temporal.ValueRange;
  78 import java.util.Locale;
  79 import java.util.Objects;
  80 
  81 /**
  82  * Context object used during date and time printing.
  83  * <p>
  84  * This class provides a single wrapper to items used in the print.
  85  *
  86  * <h3>Specification for implementors</h3>
  87  * This class is a mutable context intended for use from a single thread.
  88  * Usage of the class is thread-safe within standard printing as the framework creates
  89  * a new instance of the class for each print and printing is single-threaded.
  90  *
  91  * @since 1.8
  92  */
  93 final class DateTimePrintContext {
  94 
  95     /**
  96      * The temporal being output.
  97      */
  98     private TemporalAccessor temporal;
  99     /**
 100      * The formatter, not null.
 101      */
 102     private DateTimeFormatter formatter;
 103     /**
 104      * Whether the current formatter is optional.
 105      */
 106     private int optional;
 107 
 108     /**
 109      * Creates a new instance of the context.
 110      *
 111      * @param temporal  the temporal object being output, not null
 112      * @param formatter  the formatter controlling the print, not null
 113      */
 114     DateTimePrintContext(TemporalAccessor temporal, DateTimeFormatter formatter) {
 115         super();
 116         this.temporal = adjust(temporal, formatter);
 117         this.formatter = formatter;
 118     }
 119 
 120     private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) {
 121         // normal case first
 122         Chrono<?> overrideChrono = formatter.getChrono();
 123         ZoneId overrideZone = formatter.getZone();
 124         if (overrideChrono == null && overrideZone == null) {
 125             return temporal;
 126         }
 127 
 128         // ensure minimal change
 129         Chrono<?> temporalChrono = Chrono.from(temporal);  // default to ISO, handles Instant
 130         ZoneId temporalZone = temporal.query(Queries.zone());  // zone then offset, handles OffsetDateTime
 131         if (temporal.isSupported(EPOCH_DAY) == false || Objects.equals(overrideChrono, temporalChrono)) {
 132             overrideChrono = null;
 133         }
 134         if (temporal.isSupported(INSTANT_SECONDS) == false || Objects.equals(overrideZone, temporalZone)) {
 135             overrideZone = null;
 136         }
 137         if (overrideChrono == null && overrideZone == null) {
 138             return temporal;
 139         }
 140 
 141         // make adjustment
 142         if (overrideChrono != null && overrideZone != null) {
 143             return overrideChrono.zonedDateTime(Instant.from(temporal), overrideZone);
 144         } else if (overrideZone != null) {
 145             return temporalChrono.zonedDateTime(Instant.from(temporal), overrideZone);
 146         } else {  // overrideChrono != null
 147             // need class here to handle non-standard cases like OffsetDate
 148             final ChronoLocalDate<?> date = overrideChrono.date(temporal);
 149             return new TemporalAccessor() {
 150                 @Override
 151                 public boolean isSupported(TemporalField field) {
 152                     return temporal.isSupported(field);
 153                 }
 154                 @Override
 155                 public ValueRange range(TemporalField field) {
 156                     if (field instanceof ChronoField) {
 157                         if (((ChronoField) field).isDateField()) {
 158                             return date.range(field);
 159                         } else {
 160                             return temporal.range(field);
 161                         }
 162                     }
 163                     return field.doRange(this);
 164                 }
 165                 @Override
 166                 public long getLong(TemporalField field) {
 167                     if (field instanceof ChronoField) {
 168                         if (((ChronoField) field).isDateField()) {
 169                             return date.getLong(field);
 170                         } else {
 171                             return temporal.getLong(field);
 172                         }
 173                     }
 174                     return field.doGet(this);
 175                 }

 176                 @Override
 177                 public <R> R query(TemporalQuery<R> query) {
 178                     if (query == Queries.zoneId() || query == Queries.chrono() || query == Queries.precision()) {



 179                         return temporal.query(query);
 180                     }
 181                     return query.queryFrom(this);
 182                 }
 183             };
 184         }
 185     }
 186 
 187     //-----------------------------------------------------------------------
 188     /**
 189      * Gets the temporal object being output.
 190      *
 191      * @return the temporal object, not null
 192      */
 193     TemporalAccessor getTemporal() {
 194         return temporal;
 195     }
 196 
 197     /**
 198      * Gets the locale.
 199      * <p>
 200      * This locale is used to control localization in the print output except
 201      * where localization is controlled by the symbols.
 202      *
 203      * @return the locale, not null
 204      */
 205     Locale getLocale() {
 206         return formatter.getLocale();
 207     }
 208 
 209     /**
 210      * Gets the formatting symbols.
 211      * <p>
 212      * The symbols control the localization of numeric output.
 213      *
 214      * @return the formatting symbols, not null
 215      */
 216     DateTimeFormatSymbols getSymbols() {
 217         return formatter.getSymbols();
 218     }
 219 
 220     //-----------------------------------------------------------------------




  50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  54  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  57  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  58  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  59  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61  */
  62 package java.time.format;
  63 
  64 import static java.time.temporal.ChronoField.EPOCH_DAY;
  65 import static java.time.temporal.ChronoField.INSTANT_SECONDS;
  66 
  67 import java.time.DateTimeException;
  68 import java.time.Instant;
  69 import java.time.ZoneId;
  70 import java.time.chrono.Chronology;
  71 import java.time.temporal.ChronoField;
  72 import java.time.chrono.ChronoLocalDate;
  73 import java.time.temporal.Queries;
  74 import java.time.temporal.TemporalAccessor;
  75 import java.time.temporal.TemporalField;
  76 import java.time.temporal.TemporalQuery;
  77 import java.time.temporal.ValueRange;
  78 import java.util.Locale;
  79 import java.util.Objects;
  80 
  81 /**
  82  * Context object used during date and time printing.
  83  * <p>
  84  * This class provides a single wrapper to items used in the format.
  85  *
  86  * <h3>Specification for implementors</h3>
  87  * This class is a mutable context intended for use from a single thread.
  88  * Usage of the class is thread-safe within standard printing as the framework creates
  89  * a new instance of the class for each format and printing is single-threaded.
  90  *
  91  * @since 1.8
  92  */
  93 final class DateTimePrintContext {
  94 
  95     /**
  96      * The temporal being output.
  97      */
  98     private TemporalAccessor temporal;
  99     /**
 100      * The formatter, not null.
 101      */
 102     private DateTimeFormatter formatter;
 103     /**
 104      * Whether the current formatter is optional.
 105      */
 106     private int optional;
 107 
 108     /**
 109      * Creates a new instance of the context.
 110      *
 111      * @param temporal  the temporal object being output, not null
 112      * @param formatter  the formatter controlling the format, not null
 113      */
 114     DateTimePrintContext(TemporalAccessor temporal, DateTimeFormatter formatter) {
 115         super();
 116         this.temporal = adjust(temporal, formatter);
 117         this.formatter = formatter;
 118     }
 119 
 120     private static TemporalAccessor adjust(final TemporalAccessor temporal, DateTimeFormatter formatter) {
 121         // normal case first
 122         Chronology overrideChrono = formatter.getChronology();
 123         ZoneId overrideZone = formatter.getZone();
 124         if (overrideChrono == null && overrideZone == null) {
 125             return temporal;
 126         }
 127 
 128         // ensure minimal change
 129         Chronology temporalChrono = Chronology.from(temporal);  // default to ISO, handles Instant
 130         ZoneId temporalZone = temporal.query(Queries.zone());  // zone then offset, handles OffsetDateTime
 131         if (temporal.isSupported(EPOCH_DAY) == false || Objects.equals(overrideChrono, temporalChrono)) {
 132             overrideChrono = null;
 133         }
 134         if (temporal.isSupported(INSTANT_SECONDS) == false || Objects.equals(overrideZone, temporalZone)) {
 135             overrideZone = null;
 136         }
 137         if (overrideChrono == null && overrideZone == null) {
 138             return temporal;
 139         }
 140 
 141         // make adjustment
 142         if (overrideChrono != null && overrideZone != null) {
 143             return overrideChrono.zonedDateTime(Instant.from(temporal), overrideZone);
 144         } else if (overrideZone != null) {
 145             return temporalChrono.zonedDateTime(Instant.from(temporal), overrideZone);
 146         } else {  // overrideChrono != null
 147             // need class here to handle non-standard cases
 148             final ChronoLocalDate date = overrideChrono.date(temporal);
 149             return new TemporalAccessor() {
 150                 @Override
 151                 public boolean isSupported(TemporalField field) {
 152                     return temporal.isSupported(field);
 153                 }
 154                 @Override
 155                 public ValueRange range(TemporalField field) {
 156                     if (field instanceof ChronoField) {
 157                         if (((ChronoField) field).isDateField()) {
 158                             return date.range(field);
 159                         } else {
 160                             return temporal.range(field);
 161                         }
 162                     }
 163                     return field.rangeRefinedBy(this);
 164                 }
 165                 @Override
 166                 public long getLong(TemporalField field) {
 167                     if (field instanceof ChronoField) {
 168                         if (((ChronoField) field).isDateField()) {
 169                             return date.getLong(field);
 170                         } else {
 171                             return temporal.getLong(field);
 172                         }
 173                     }
 174                     return field.getFrom(this);
 175                 }
 176                 @SuppressWarnings("unchecked")
 177                 @Override
 178                 public <R> R query(TemporalQuery<R> query) {
 179                     if (query == Queries.chronology()) {
 180                         return (R) date.getChronology();
 181                     }
 182                     if (query == Queries.zoneId() || query == Queries.precision()) {
 183                         return temporal.query(query);
 184                     }
 185                     return query.queryFrom(this);
 186                 }
 187             };
 188         }
 189     }
 190 
 191     //-----------------------------------------------------------------------
 192     /**
 193      * Gets the temporal object being output.
 194      *
 195      * @return the temporal object, not null
 196      */
 197     TemporalAccessor getTemporal() {
 198         return temporal;
 199     }
 200 
 201     /**
 202      * Gets the locale.
 203      * <p>
 204      * This locale is used to control localization in the format output except
 205      * where localization is controlled by the symbols.
 206      *
 207      * @return the locale, not null
 208      */
 209     Locale getLocale() {
 210         return formatter.getLocale();
 211     }
 212 
 213     /**
 214      * Gets the formatting symbols.
 215      * <p>
 216      * The symbols control the localization of numeric output.
 217      *
 218      * @return the formatting symbols, not null
 219      */
 220     DateTimeFormatSymbols getSymbols() {
 221         return formatter.getSymbols();
 222     }
 223 
 224     //-----------------------------------------------------------------------