001/* 002 * Copyright (c) 2007-2013, Stephen Colebourne & Michael Nascimento Santos 003 * 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or without 007 * modification, are permitted provided that the following conditions are met: 008 * 009 * * Redistributions of source code must retain the above copyright notice, 010 * this list of conditions and the following disclaimer. 011 * 012 * * Redistributions in binary form must reproduce the above copyright notice, 013 * this list of conditions and the following disclaimer in the documentation 014 * and/or other materials provided with the distribution. 015 * 016 * * Neither the name of JSR-310 nor the names of its contributors 017 * may be used to endorse or promote products derived from this software 018 * without specific prior written permission. 019 * 020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 021 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 022 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 023 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 024 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 031 */ 032package org.threeten.bp.jdk8; 033 034import static org.threeten.bp.temporal.ChronoField.ERA; 035 036import java.util.Locale; 037 038import org.threeten.bp.DateTimeException; 039import org.threeten.bp.format.DateTimeFormatterBuilder; 040import org.threeten.bp.format.TextStyle; 041import org.threeten.bp.temporal.Chrono; 042import org.threeten.bp.temporal.ChronoField; 043import org.threeten.bp.temporal.ChronoLocalDate; 044import org.threeten.bp.temporal.Era; 045import org.threeten.bp.temporal.Temporal; 046import org.threeten.bp.temporal.TemporalField; 047import org.threeten.bp.temporal.TemporalQueries; 048import org.threeten.bp.temporal.TemporalQuery; 049 050/** 051 * A temporary class providing implementations that will become default interface 052 * methods once integrated into JDK 8. 053 * 054 * @param <C> the chronology of this era 055 */ 056public abstract class DefaultInterfaceEra<C extends Chrono<C>> 057 extends DefaultInterfaceTemporalAccessor 058 implements Era<C> { 059 060 @Override 061 public ChronoLocalDate<C> date(int year, int month, int day) { 062 return getChrono().date(this, year, month, day); 063 } 064 065 @Override 066 public ChronoLocalDate<C> dateYearDay(int year, int dayOfYear) { 067 return getChrono().dateYearDay(this, year, dayOfYear); 068 } 069 070 //----------------------------------------------------------------------- 071 @Override 072 public boolean isSupported(TemporalField field) { 073 if (field instanceof ChronoField) { 074 return field == ERA; 075 } 076 return field != null && field.doIsSupported(this); 077 } 078 079 @Override 080 public int get(TemporalField field) { 081 if (field == ERA) { 082 return getValue(); 083 } 084 return range(field).checkValidIntValue(getLong(field), field); 085 } 086 087 @Override 088 public long getLong(TemporalField field) { 089 if (field == ERA) { 090 return getValue(); 091 } else if (field instanceof ChronoField) { 092 throw new DateTimeException("Unsupported field: " + field.getName()); 093 } 094 return field.doGet(this); 095 } 096 097 //------------------------------------------------------------------------- 098 @Override 099 public Temporal adjustInto(Temporal temporal) { 100 return temporal.with(ERA, getValue()); 101 } 102 103 @Override 104 public <R> R query(TemporalQuery<R> query) { 105 if (query == TemporalQueries.chrono()) { 106 return (R) getChrono(); 107 } 108 return super.query(query); 109 } 110 111 //----------------------------------------------------------------------- 112 @Override 113 public String getText(TextStyle style, Locale locale) { 114 return new DateTimeFormatterBuilder().appendText(ERA, style).toFormatter(locale).print(this); 115 } 116 117}