1 /*
2  * hunt-time: A time library for D programming language.
3  *
4  * Copyright (C) 2015-2018 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.time.format.TextStyle;
13 
14 import hunt.time.util.Calendar;
15 import hunt.Enum;
16 import std.concurrency : initOnce;
17 
18 /**
19  * Enumeration of the style of text formatting and parsing.
20  * !(p)
21  * Text styles define three sizes for the formatted text - 'full', 'short' and 'narrow'.
22  * Each of these three sizes is available _in both 'standard' and 'stand-alone' variations.
23  * !(p)
24  * The difference between the three sizes is obvious _in most languages.
25  * For example, _in English the 'full' month is 'January', the 'short' month is 'Jan'
26  * and the 'narrow' month is 'J'. Note that the narrow size is often not unique.
27  * For example, 'January', 'June' and 'July' all have the 'narrow' text 'J'.
28  * !(p)
29  * The difference between the 'standard' and 'stand-alone' forms is trickier to describe
30  * as there is no difference _in English. However, _in other languages there is a difference
31  * _in the word used when the text is used alone, as opposed to _in a complete date.
32  * For example, the word used for a month when used alone _in a date picker is different
33  * to the word used for month _in association with a day and year _in a date.
34  *
35  * @implSpec
36  * This is immutable and thread-safe enum.
37  *
38  * @since 1.8
39  */
40 class TextStyle : AbstractEnum!TextStyle {
41     // ordered from large to small
42     // ordered so that bit 0 of the ordinal indicates stand-alone.
43 
44     /**
45      * Full text, typically the full description.
46      * For example, day-of-week Monday might output "Monday".
47      */
48     static TextStyle FULL() {
49         __gshared TextStyle t;
50         return initOnce!t(new TextStyle(Calendar.LONG_FORMAT, 0, "FULL", 0));
51     }
52     /**
53      * Full text for stand-alone use, typically the full description.
54      * For example, day-of-week Monday might output "Monday".
55      */
56     static TextStyle FULL_STANDALONE() {
57         __gshared TextStyle t;
58         return initOnce!t(new TextStyle(Calendar.LONG_STANDALONE, 0, "FULL_STANDALONE", 1));
59     }
60     /**
61      * Short text, typically an abbreviation.
62      * For example, day-of-week Monday might output "Mon".
63      */
64     static TextStyle SHORT() {
65         __gshared TextStyle t;
66         return initOnce!t(new TextStyle(Calendar.SHORT_FORMAT, 1, "SHORT", 2));
67     }
68     /**
69      * Short text for stand-alone use, typically an abbreviation.
70      * For example, day-of-week Monday might output "Mon".
71      */
72     static TextStyle SHORT_STANDALONE() {
73         __gshared TextStyle t;
74         return initOnce!t(new TextStyle(Calendar.SHORT_STANDALONE, 1, "SHORT_STANDALONE", 3));
75     }
76     /**
77      * Narrow text, typically a single letter.
78      * For example, day-of-week Monday might output "M".
79      */
80     static TextStyle NARROW() {
81         __gshared TextStyle t;
82         return initOnce!t(new TextStyle(Calendar.NARROW_FORMAT, 1, "NARROW", 4));
83     }
84     /**
85      * Narrow text for stand-alone use, typically a single letter.
86      * For example, day-of-week Monday might output "M".
87      */
88     static TextStyle NARROW_STANDALONE() {
89         __gshared TextStyle t;
90         return initOnce!t(new TextStyle(Calendar.NARROW_STANDALONE, 1, "NARROW_STANDALONE", 5));
91     }
92 
93     private int _calendarStyle;
94     private int _zoneNameStyleIndex;
95 
96     protected this(int calendarStyle, int zoneNameStyleIndex, string name, int ordinal) {
97         super(name, ordinal);
98         this._calendarStyle = calendarStyle;
99         this._zoneNameStyleIndex = zoneNameStyleIndex;
100     }
101 
102     static TextStyle[] values() {
103         __gshared TextStyle[] d;
104         return initOnce!d({
105             TextStyle[] arr;
106             arr ~= FULL();
107             arr ~= FULL_STANDALONE();
108             arr ~= SHORT();
109             arr ~= SHORT_STANDALONE();
110             arr ~= NARROW();
111             arr ~= NARROW_STANDALONE();
112             return arr;
113         }());
114     }
115     /**
116      * Returns true if the Style is a stand-alone style.
117      * @return true if the style is a stand-alone style.
118      */
119     bool isStandalone() {
120         return (ordinal() & 1) == 1;
121     }
122 
123     /**
124      * Returns the stand-alone style with the same size.
125      * @return the stand-alone style with the same size
126      */
127     TextStyle asStandalone() {
128         return values()[ordinal() | 1];
129     }
130 
131     /**
132      * Returns the normal style with the same size.
133      *
134      * @return the normal style with the same size
135      */
136     TextStyle asNormal() {
137         return values()[ordinal() & ~1];
138     }
139 
140     /**
141      * Returns the {@code Calendar} style corresponding to this {@code TextStyle}.
142      *
143      * @return the corresponding {@code Calendar} style
144      */
145     int toCalendarStyle() {
146         return _calendarStyle;
147     }
148 
149     /**
150      * Returns the relative index value to an element of the {@link
151      * java.text.DateFormatSymbols#getZoneStrings() DateFormatSymbols.getZoneStrings()}
152      * value, 0 for long names and 1 for short names (abbreviations). Note that these values
153      * do !(em)not</em> correspond to the {@link java.util.TimeZone#LONG} and {@link
154      * java.util.TimeZone#SHORT} values.
155      *
156      * @return the relative index value to time zone names array
157      */
158     int zoneNameStyleIndex() {
159         return _zoneNameStyleIndex;
160     }
161 }