forked from BaseXdb/basex
/
Formatter.java
99 lines (87 loc) · 2.74 KB
/
Formatter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.basex.util.locale;
import java.util.HashMap;
import org.basex.util.Util;
/**
* Abstract class for formatting data in different languages.
* Implementations are expected to return strings in a capitalized syntax.
*
* @author Workgroup DBIS, University of Konstanz 2005-10, ISC License
* @author Christian Gruen
*/
public abstract class Formatter {
/** Language code: English. */
private static final String EN = "en";
/** Formatter instances. */
private static final HashMap<String, Formatter> MAP =
new HashMap<String, Formatter>();
// initialize hash map with English formatter as default
static { MAP.put(EN, new FormatterEN()); }
/**
* Returns a formatter for the specified language.
* @param ln language
* @return formatter instance
*/
public static Formatter get(final String ln) {
// check if formatter has already been created
Formatter form = MAP.get(ln);
if(form == null) {
try {
// create new instance (class name + language in upper case)
final String clz = Util.name(Formatter.class) + ln.toUpperCase();
form = (Formatter) Class.forName(clz).newInstance();
MAP.put(ln, form);
} catch(final Exception ex) {
// instantiation not successful: return default formatter
form = MAP.get(EN);
}
}
return form;
}
/**
* Returns a word representation for the specified number.
* @param n number to be formatted
* @param ord ordinal suffix
* @return token
*/
public abstract byte[] word(final long n, final String ord);
/**
* Returns an ordinal representation for the specified number.
* @param n number to be formatted
* @param ord ordinal suffix
* @return ordinal
*/
public abstract byte[] ordinal(final long n, final String ord);
/**
* Returns the specified month (0-11).
* @param n number to be formatted
* @param min minimum length
* @param max maximum length
* @return month
*/
public abstract byte[] month(final int n, final int min, final int max);
/**
* Returns the specified day of the week (0-6, Sunday-Saturday).
* @param n number to be formatted
* @param min minimum length
* @param max maximum length
* @return day of week
*/
public abstract byte[] day(final int n, final int min, final int max);
/**
* Returns the am/pm marker.
* @param am am flag
* @return am/pm marker
*/
public abstract byte[] ampm(final boolean am);
/**
* Returns the calendar.
* @return calendar
*/
public abstract byte[] calendar();
/**
* Returns the era.
* @param year year
* @return era
*/
public abstract byte[] era(final int year);
}