1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.i18n.addressinput;
18
19import java.util.HashMap;
20import java.util.Map;
21
22/**
23 * Defines the character codes used in the metadata to specify the types of fields used in address
24 * formatting. Note that the metadata also has a character for newlines, which is not defined here.
25 */
26public enum AddressField {
27    ADMIN_AREA('S'),
28    LOCALITY('C'),
29    RECIPIENT('N'),
30    ORGANIZATION('O'),
31    // Deprecated - use A instead.
32    ADDRESS_LINE_1('1'),
33    // Deprecated - use A instead.
34    ADDRESS_LINE_2('2'),
35    DEPENDENT_LOCALITY('D'),
36    POSTAL_CODE('Z'),
37    SORTING_CODE('X'),
38    STREET_ADDRESS('A'),
39
40    COUNTRY('R');
41
42    /**
43     * Enum for width types of address input fields.
44     */
45    public enum WidthType {
46        LONG,
47        SHORT;
48    }
49
50    private static final Map<Character, AddressField> FIELD_MAPPING
51            = new HashMap<Character, AddressField>();
52
53    static {
54        for (AddressField value : values()) {
55            FIELD_MAPPING.put(value.getChar(), value);
56        }
57    }
58
59    private final char mField;
60
61    private AddressField(char field) {
62        mField = field;
63    }
64
65    /**
66     * Gets the corresponding AddressField for the character code. Returns null if the character is
67     * not recognized.
68     */
69    static AddressField of(char field) {
70        return FIELD_MAPPING.get(field);
71    }
72
73    /**
74     * Gets the field's identification character, as used in the metadata.
75     *
76     * @return identification char.
77     */
78    char getChar() {
79        return mField;
80    }
81
82    /** Returns default width type of the address field. */
83    WidthType getDefaulWidthType() {
84        return this.equals(POSTAL_CODE) || this.equals(SORTING_CODE)
85                ? WidthType.SHORT : WidthType.LONG;
86    }
87}
88