jdk/src/java.base/share/classes/java/time/chrono/JapaneseEra.java
changeset 42696 a0df1f76b88e
parent 28772 5259f2a59d83
child 44368 268fa1f3e670
equal deleted inserted replaced
42695:11e9b19ebd81 42696:a0df1f76b88e
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    71 import java.io.ObjectInputStream;
    71 import java.io.ObjectInputStream;
    72 import java.io.ObjectStreamException;
    72 import java.io.ObjectStreamException;
    73 import java.io.Serializable;
    73 import java.io.Serializable;
    74 import java.time.DateTimeException;
    74 import java.time.DateTimeException;
    75 import java.time.LocalDate;
    75 import java.time.LocalDate;
       
    76 import java.time.format.TextStyle;
    76 import java.time.temporal.ChronoField;
    77 import java.time.temporal.ChronoField;
    77 import java.time.temporal.TemporalField;
    78 import java.time.temporal.TemporalField;
    78 import java.time.temporal.UnsupportedTemporalTypeException;
    79 import java.time.temporal.UnsupportedTemporalTypeException;
    79 import java.time.temporal.ValueRange;
    80 import java.time.temporal.ValueRange;
    80 import java.util.Arrays;
    81 import java.util.Arrays;
       
    82 import java.util.Locale;
    81 import java.util.Objects;
    83 import java.util.Objects;
    82 
    84 
    83 import sun.util.calendar.CalendarDate;
    85 import sun.util.calendar.CalendarDate;
    84 
    86 
    85 /**
    87 /**
   123      * The singleton instance for the 'Heisei' era (1989-01-08 - current)
   125      * The singleton instance for the 'Heisei' era (1989-01-08 - current)
   124      * which has the value 2.
   126      * which has the value 2.
   125      */
   127      */
   126     public static final JapaneseEra HEISEI = new JapaneseEra(2, LocalDate.of(1989, 1, 8));
   128     public static final JapaneseEra HEISEI = new JapaneseEra(2, LocalDate.of(1989, 1, 8));
   127 
   129 
   128     // the number of defined JapaneseEra constants.
   130     // The number of predefined JapaneseEra constants.
   129     // There could be an extra era defined in its configuration.
   131     // There may be a supplemental era defined by the property.
   130     private static final int N_ERA_CONSTANTS = HEISEI.getValue() + ERA_OFFSET;
   132     private static final int N_ERA_CONSTANTS = HEISEI.getValue() + ERA_OFFSET;
   131 
   133 
   132     /**
   134     /**
   133      * Serialization version.
   135      * Serialization version.
   134      */
   136      */
   235      */
   237      */
   236     public static JapaneseEra[] values() {
   238     public static JapaneseEra[] values() {
   237         return Arrays.copyOf(KNOWN_ERAS, KNOWN_ERAS.length);
   239         return Arrays.copyOf(KNOWN_ERAS, KNOWN_ERAS.length);
   238     }
   240     }
   239 
   241 
       
   242     /**
       
   243      * Gets the textual representation of this era.
       
   244      * <p>
       
   245      * This returns the textual name used to identify the era,
       
   246      * suitable for presentation to the user.
       
   247      * The parameters control the style of the returned text and the locale.
       
   248      * <p>
       
   249      * If no textual mapping is found then the {@link #getValue() numeric value}
       
   250      * is returned.
       
   251      *
       
   252      * @param style  the style of the text required, not null
       
   253      * @param locale  the locale to use, not null
       
   254      * @return the text value of the era, not null
       
   255      * @since 9
       
   256      */
       
   257     @Override
       
   258     public String getDisplayName(TextStyle style, Locale locale) {
       
   259         // If this JapaneseEra is a supplemental one, obtain the name from
       
   260         // the era definition.
       
   261         if (getValue() > N_ERA_CONSTANTS - ERA_OFFSET) {
       
   262             Objects.requireNonNull(locale, "locale");
       
   263             return style.asNormal() == TextStyle.NARROW ? getAbbreviation() : getName();
       
   264         }
       
   265         return Era.super.getDisplayName(style, locale);
       
   266     }
       
   267 
   240     //-----------------------------------------------------------------------
   268     //-----------------------------------------------------------------------
   241     /**
   269     /**
   242      * Obtains an instance of {@code JapaneseEra} from a date.
   270      * Obtains an instance of {@code JapaneseEra} from a date.
   243      *
   271      *
   244      * @param date  the date, not null
   272      * @param date  the date, not null
   336         return Era.super.range(field);
   364         return Era.super.range(field);
   337     }
   365     }
   338 
   366 
   339     //-----------------------------------------------------------------------
   367     //-----------------------------------------------------------------------
   340     String getAbbreviation() {
   368     String getAbbreviation() {
   341         int index = ordinal(getValue());
   369         return ERA_CONFIG[ordinal(getValue())].getAbbreviation();
   342         if (index == 0) {
       
   343             return "";
       
   344         }
       
   345         return ERA_CONFIG[index].getAbbreviation();
       
   346     }
   370     }
   347 
   371 
   348     String getName() {
   372     String getName() {
   349         return ERA_CONFIG[ordinal(getValue())].getName();
   373         return ERA_CONFIG[ordinal(getValue())].getName();
   350     }
   374     }