LANG-829 FastDateParser could use Calendar.getDisplayNames for all text fields

Remove unnecessary KeyValue instances

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1390937 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-09-27 10:59:30 +00:00
parent 8c87749695
commit e5ae84a6fc
1 changed files with 14 additions and 57 deletions

View File

@ -22,9 +22,7 @@
import java.text.ParseException; import java.text.ParseException;
import java.text.ParsePosition; import java.text.ParsePosition;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -331,57 +329,16 @@ private static StringBuilder escapeRegex(StringBuilder regex, String value, bool
return regex; return regex;
} }
/**
* A class to store Key / Value pairs
*/
private static class KeyValue {
public String key;
public int value;
/**
* Construct a Key / Value pair
* @param key The key
* @param value The value
*/
public KeyValue(String key, int value) {
this.key= key;
this.value= value;
}
}
/**
* ignore case comparison of keys
*/
private static final Comparator<KeyValue> IGNORE_CASE_COMPARATOR = new Comparator<KeyValue> () {
@Override
public int compare(KeyValue left, KeyValue right) {
return left.key.compareToIgnoreCase(right.key);
}
};
/** /**
* Get the short and long values displayed for a field * Get the short and long values displayed for a field
* @param field The field of interest * @param field The field of interest
* @param definingCalendar The calendar to obtain the short and long values * @param definingCalendar The calendar to obtain the short and long values
* @param locale The locale of dislay names * @param locale The locale of display names
* @return A sorted array of the field key / value pairs * @return A Map of the field key / value pairs
*/ */
private static KeyValue[] getDisplayNames(int field, Calendar definingCalendar, Locale locale) { private static Map<String, Integer> getDisplayNames(int field, Calendar definingCalendar, Locale locale) {
List<KeyValue> keyValues = new ArrayList<KeyValue>(24); return definingCalendar.getDisplayNames(field, Calendar.ALL_STYLES, locale);
addNamesToKeyValues(keyValues, definingCalendar.getDisplayNames(field, Calendar.ALL_STYLES, locale));
return createKeyValues(keyValues);
}
private static void addNamesToKeyValues(List<KeyValue> keyValues, Map<String, Integer> displayNames) {
for(Map.Entry<String, Integer> me : displayNames.entrySet()) {
keyValues.add(new KeyValue(me.getKey(), me.getValue()));
}
}
private static KeyValue[] createKeyValues(List<KeyValue> keyValues) {
KeyValue[] fieldKeyValues= keyValues.toArray(new KeyValue[keyValues.size()]);
Arrays.sort(fieldKeyValues, IGNORE_CASE_COMPARATOR);
return fieldKeyValues;
} }
/** /**
@ -502,7 +459,7 @@ private Strategy getStrategy(String formatField, Calendar definingCalendar) {
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked") // OK because we are creating an array with no entries
private static ConcurrentMap<Locale, Strategy>[] caches = new ConcurrentMap[Calendar.FIELD_COUNT]; private static ConcurrentMap<Locale, Strategy>[] caches = new ConcurrentMap[Calendar.FIELD_COUNT];
/** /**
@ -528,7 +485,7 @@ private static ConcurrentMap<Locale, Strategy> getCache(int field) {
*/ */
private Strategy getLocaleSpecificStrategy(int field, Calendar definingCalendar) { private Strategy getLocaleSpecificStrategy(int field, Calendar definingCalendar) {
ConcurrentMap<Locale,Strategy> cache = getCache(field); ConcurrentMap<Locale,Strategy> cache = getCache(field);
Strategy strategy= cache.get(field); Strategy strategy= cache.get(Integer.valueOf(field));
if(strategy==null) { if(strategy==null) {
strategy= field==Calendar.ZONE_OFFSET strategy= field==Calendar.ZONE_OFFSET
? new TimeZoneStrategy(locale) ? new TimeZoneStrategy(locale)
@ -589,7 +546,7 @@ public void setCalendar(FastDateParser parser, Calendar cal, String value) {
*/ */
private static class TextStrategy implements Strategy { private static class TextStrategy implements Strategy {
private final int field; private final int field;
private final KeyValue[] keyValues; private final Map<String, Integer> keyValues;
/** /**
* Construct a Strategy that parses a Text field * Construct a Strategy that parses a Text field
@ -614,8 +571,8 @@ public boolean isNumber() {
@Override @Override
public boolean addRegex(FastDateParser parser, StringBuilder regex) { public boolean addRegex(FastDateParser parser, StringBuilder regex) {
regex.append('('); regex.append('(');
for(KeyValue textKeyValue : keyValues) { for(String textKeyValue : keyValues.keySet()) {
escapeRegex(regex, textKeyValue.key, false).append('|'); escapeRegex(regex, textKeyValue, false).append('|');
} }
regex.setCharAt(regex.length()-1, ')'); regex.setCharAt(regex.length()-1, ')');
return true; return true;
@ -626,17 +583,17 @@ public boolean addRegex(FastDateParser parser, StringBuilder regex) {
*/ */
@Override @Override
public void setCalendar(FastDateParser parser, Calendar cal, String value) { public void setCalendar(FastDateParser parser, Calendar cal, String value) {
int idx= Arrays.binarySearch(keyValues, new KeyValue(value, -1), IGNORE_CASE_COMPARATOR); Integer iVal = keyValues.get(value);
if(idx<0) { if(iVal == null) {
StringBuilder sb= new StringBuilder(value); StringBuilder sb= new StringBuilder(value);
sb.append(" not in ("); sb.append(" not in (");
for(KeyValue textKeyValue : keyValues) { for(String textKeyValue : keyValues.keySet()) {
sb.append(textKeyValue.key).append(' '); sb.append(textKeyValue).append(' ');
} }
sb.setCharAt(sb.length()-1, ')'); sb.setCharAt(sb.length()-1, ')');
throw new IllegalArgumentException(sb.toString()); throw new IllegalArgumentException(sb.toString());
} }
cal.set(field, keyValues[idx].value); cal.set(field, iVal.intValue());
} }
} }