From b720070701a07bf3cd627723ccfd4a4a1a90dec7 Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Thu, 22 Aug 2013 15:50:51 +0000 Subject: Move the entire project into the new subdirectory for Java source code. --- .../com/android/i18n/addressinput/FormOptions.java | 287 +++++++++++++++++++++ 1 file changed, 287 insertions(+) create mode 100644 java/src/com/android/i18n/addressinput/FormOptions.java (limited to 'java/src/com/android/i18n/addressinput/FormOptions.java') diff --git a/java/src/com/android/i18n/addressinput/FormOptions.java b/java/src/com/android/i18n/addressinput/FormOptions.java new file mode 100644 index 0000000..07bc38e --- /dev/null +++ b/java/src/com/android/i18n/addressinput/FormOptions.java @@ -0,0 +1,287 @@ +/* + * Copyright (C) 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.i18n.addressinput; + +import java.util.EnumMap; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +/** + * Configuration Options that can be used by Address Display components for things like show/hide + * fields or make them readonly. By default, all the fields are visible and editable. + * + *

Also, provides the ability to add additional required fields, for e.g. {@link + * AddressField#RECIPIENT}. + */ +public class FormOptions { + + private final String mBaseId; + + private final EnumSet mHiddenFields; + + private final EnumSet mReadonlyFields; + + private final EnumSet mRequiredFields; + + private final EnumMap mCustomLabels = + new EnumMap(AddressField.class); + + private final Map mOverrideFieldOrder = + new HashMap(); + + private final EnumMap mMaxLengths = + new EnumMap(AddressField.class); + + private final String mServerUrl; + + private FormOptions(Builder builder) { + // copy values from builder + mBaseId = builder.mBaseId; + mHiddenFields = EnumSet.copyOf(builder.mHiddenFields); + mReadonlyFields = EnumSet.copyOf(builder.mReadonlyFields); + mRequiredFields = EnumSet.copyOf(builder.mRequiredFields); + mCustomLabels.putAll(builder.mCustomLabels); + mOverrideFieldOrder.putAll(builder.mOverrideFieldOrder); + mMaxLengths.putAll(builder.mMaxLengths); + mServerUrl = builder.mServerUrl; + } + + /** + * Gets base ID of the address form. Default is "addressform". + */ + String getBaseId() { + return mBaseId; + } + + boolean isHidden(AddressField field) { + return mHiddenFields.contains(field); + } + + boolean isReadonly(AddressField field) { + return mReadonlyFields.contains(field); + } + + boolean isRequired(AddressField field) { + return mRequiredFields.contains(field); + } + + EnumSet getRequiredFields() { + return mRequiredFields; + } + + /** + * Gets the customized label for the {@code field}, or returns null if none. + */ + String getCustomLabel(AddressField field) { + return mCustomLabels.get(field); + } + + /** + * Gets the URL of the Address Data Server. + */ + String getUrl() { + return mServerUrl; + } + + /** + * Gets the overridden field orders with their corresponding region code. Returns null if field + * orders for {@code regionCode} is not specified. + */ + AddressField[] getCustomFieldOrder(String regionCode) { + if (regionCode == null) { + throw new RuntimeException("regionCode cannot be null."); + } + return mOverrideFieldOrder.get(regionCode); + } + + /** + * Gets the customized max length for the {@code field}, or null if none. + */ + Integer getCustomMaxLength(AddressField field) { + return mMaxLengths.get(field); + } + + /** + * Class to build the form, specifying the attributes for each field. + */ + public static class Builder { + + private String mBaseId = "addressform"; + + private final EnumSet mRequiredFields = + EnumSet.noneOf(AddressField.class); + + private final EnumSet mHiddenFields = + EnumSet.noneOf(AddressField.class); + + private final EnumSet mReadonlyFields = + EnumSet.noneOf(AddressField.class); + + private final EnumMap mCustomLabels = + new EnumMap(AddressField.class); + + private final Map mOverrideFieldOrder = + new HashMap(); + + private final EnumMap mMaxLengths = + new EnumMap(AddressField.class); + + /** + * Uses the default server URL from CacheData. + */ + private String mServerUrl = new CacheData().getUrl(); + + /** + * Sets the base ID of the address form. + */ + public Builder baseId(String baseId) { + if (baseId == null) { + throw new RuntimeException("baseId cannot be null."); + } + mBaseId = baseId; + return this; + } + + public Builder hide(AddressField field) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + mHiddenFields.add(field); + return this; + } + + /** + * Make a field read-only. + */ + public Builder readonly(AddressField field) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + mReadonlyFields.add(field); + return this; + } + + /** + * Make a field required. + */ + public Builder required(AddressField field) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + mRequiredFields.add(field); + return this; + } + + /** + * Customizes label for an {@code AddressField}. + */ + public Builder customizeLabel(AddressField field, String label) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + if (label == null) { + throw new RuntimeException("Label cannot be null."); + } + mCustomLabels.put(field, label); + return this; + } + + /** + * Sets the field order for a region code. The order you set here will override the + * predefined one. For example, you can set field order for US to be first {@code + * AddressField#ORGANIZATION} then {@code AddressField#RECIPIENT}. Repeated address fields + * in {@code fields} are not allowed. Size of {@code fields} has to be larger than one. + * Input {@code fields} can be partial or even contain field not needed in the specified + * {@code regionCode}. For example, German addresses contain the following fields + * (in order):
+ {@link AddressField#RECIPIENT}, {@link AddressField#ORGANIZATION}, {@link + * AddressField#STREET_ADDRESS}, {@link AddressField#POSTAL_CODE}, {@link + * AddressField#LOCALITY}.
+ * + *

With the following call:
+ * + * customizeFieldOrder("DE", AddressField.ORGANIZATION, AddressField.RECIPIENT, + * AddressField.ADMIN_AREA); + * + *

Field order for Germany will become:
{@link AddressField#ORGANIZATION}, {@link + * AddressField#RECIPIENT}, {@link AddressField#STREET_ADDRESS}, {@link + * AddressField#POSTAL_CODE}, {@link AddressField#LOCALITY}.

+ * + *

Notice that:

  1. {@link AddressField#ORGANIZATION} comes before {@link + * AddressField#RECIPIENT} after reordering.
  2. + *
  3. Fields not specified stays the same.
  4. + *
  5. {@link AddressField#ADMIN_AREA} is specified but since it is not in German address + * format, it is simpled neglected.
+ * + * @param fields the overridden field order. + */ + public Builder customizeFieldOrder(String regionCode, AddressField... fields) { + if (regionCode == null) { + throw new RuntimeException("regionCode cannot be null."); + } + if (fields == null) { + throw new RuntimeException("Fields cannot be null."); + } + if (fields.length <= 1) { + throw new RuntimeException("There must be more than one field."); + } + HashSet checkList = new HashSet(); + AddressField[] f = new AddressField[fields.length]; + int i = 0; + for (AddressField field : fields) { + // Can't contain repeated address fields. + if (checkList.contains(field)) { + throw new RuntimeException("Address fields cannot be repeated."); + } + checkList.add(field); + f[i] = field; + i++; + } + mOverrideFieldOrder.put(regionCode, f); + return this; + } + + /** + * Sets the URL of address data server. {@code url} cannot be null. This url will override + * the default address server url. + */ + public Builder setUrl(String url) { + if (url == null) { + throw new RuntimeException("Can't set address server URL to null."); + } + mServerUrl = url; + return this; + } + + /** + * Customizes max length for a {@code AddressField}. + */ + public Builder customizeMaxLength(AddressField field, int maxLength) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + mMaxLengths.put(field, maxLength); + return this; + } + + public FormOptions build() { + return new FormOptions(this); + } + } +} -- cgit v1.2.3 From 29761c802c0df839dfbfae7dd9c77f66278bab48 Mon Sep 17 00:00:00 2001 From: Lara Scheidegger Date: Wed, 22 Oct 2014 16:46:17 +0200 Subject: First version of code (tests error) --- .../com/android/i18n/addressinput/FormOptions.java | 428 ++++++++++----------- 1 file changed, 214 insertions(+), 214 deletions(-) (limited to 'java/src/com/android/i18n/addressinput/FormOptions.java') diff --git a/java/src/com/android/i18n/addressinput/FormOptions.java b/java/src/com/android/i18n/addressinput/FormOptions.java index 07bc38e..2b9bee6 100644 --- a/java/src/com/android/i18n/addressinput/FormOptions.java +++ b/java/src/com/android/i18n/addressinput/FormOptions.java @@ -31,257 +31,257 @@ import java.util.Map; */ public class FormOptions { - private final String mBaseId; + private final String baseId; + + private final EnumSet hiddenFields; + + private final EnumSet readonlyFields; + + private final EnumSet requiredFields; + + private final EnumMap customLabels = + new EnumMap(AddressField.class); + + private final Map overrideFieldOrder = + new HashMap(); + + private final EnumMap maxLengths = + new EnumMap(AddressField.class); + + private final String serverUrl; + + private FormOptions(Builder builder) { + // copy values from builder + baseId = builder.baseId; + hiddenFields = EnumSet.copyOf(builder.hiddenFields); + readonlyFields = EnumSet.copyOf(builder.readonlyFields); + requiredFields = EnumSet.copyOf(builder.requiredFields); + customLabels.putAll(builder.customLabels); + overrideFieldOrder.putAll(builder.overrideFieldOrder); + maxLengths.putAll(builder.maxLengths); + serverUrl = builder.serverUrl; + } + + /** + * Gets base ID of the address form. Default is "addressform". + */ + String getBaseId() { + return baseId; + } + + boolean isHidden(AddressField field) { + return hiddenFields.contains(field); + } + + boolean isReadonly(AddressField field) { + return readonlyFields.contains(field); + } + + boolean isRequired(AddressField field) { + return requiredFields.contains(field); + } + + EnumSet getRequiredFields() { + return requiredFields; + } + + /** + * Gets the customized label for the {@code field}, or returns null if none. + */ + String getCustomLabel(AddressField field) { + return customLabels.get(field); + } + + /** + * Gets the URL of the Address Data Server. + */ + String getUrl() { + return serverUrl; + } + + /** + * Gets the overridden field orders with their corresponding region code. Returns null if field + * orders for {@code regionCode} is not specified. + */ + AddressField[] getCustomFieldOrder(String regionCode) { + if (regionCode == null) { + throw new RuntimeException("regionCode cannot be null."); + } + return overrideFieldOrder.get(regionCode); + } - private final EnumSet mHiddenFields; + /** + * Gets the customized max length for the {@code field}, or null if none. + */ + Integer getCustomMaxLength(AddressField field) { + return maxLengths.get(field); + } - private final EnumSet mReadonlyFields; + /** + * Class to build the form, specifying the attributes for each field. + */ + public static class Builder { - private final EnumSet mRequiredFields; + private String baseId = "addressform"; - private final EnumMap mCustomLabels = - new EnumMap(AddressField.class); + private final EnumSet requiredFields = + EnumSet.noneOf(AddressField.class); - private final Map mOverrideFieldOrder = - new HashMap(); + private final EnumSet hiddenFields = + EnumSet.noneOf(AddressField.class); - private final EnumMap mMaxLengths = - new EnumMap(AddressField.class); + private final EnumSet readonlyFields = + EnumSet.noneOf(AddressField.class); - private final String mServerUrl; + private final EnumMap customLabels = + new EnumMap(AddressField.class); - private FormOptions(Builder builder) { - // copy values from builder - mBaseId = builder.mBaseId; - mHiddenFields = EnumSet.copyOf(builder.mHiddenFields); - mReadonlyFields = EnumSet.copyOf(builder.mReadonlyFields); - mRequiredFields = EnumSet.copyOf(builder.mRequiredFields); - mCustomLabels.putAll(builder.mCustomLabels); - mOverrideFieldOrder.putAll(builder.mOverrideFieldOrder); - mMaxLengths.putAll(builder.mMaxLengths); - mServerUrl = builder.mServerUrl; - } + private final Map overrideFieldOrder = + new HashMap(); + + private final EnumMap maxLengths = + new EnumMap(AddressField.class); /** - * Gets base ID of the address form. Default is "addressform". + * Uses the default server URL from CacheData. */ - String getBaseId() { - return mBaseId; - } + private String serverUrl = new CacheData().getUrl(); - boolean isHidden(AddressField field) { - return mHiddenFields.contains(field); - } - - boolean isReadonly(AddressField field) { - return mReadonlyFields.contains(field); + /** + * Sets the base ID of the address form. + */ + public Builder baseId(String baseId) { + if (baseId == null) { + throw new RuntimeException("baseId cannot be null."); + } + baseId = baseId; + return this; } - boolean isRequired(AddressField field) { - return mRequiredFields.contains(field); + public Builder hide(AddressField field) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + hiddenFields.add(field); + return this; } - EnumSet getRequiredFields() { - return mRequiredFields; + /** + * Make a field read-only. + */ + public Builder readonly(AddressField field) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + readonlyFields.add(field); + return this; } /** - * Gets the customized label for the {@code field}, or returns null if none. + * Make a field required. */ - String getCustomLabel(AddressField field) { - return mCustomLabels.get(field); + public Builder required(AddressField field) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + requiredFields.add(field); + return this; } /** - * Gets the URL of the Address Data Server. + * Customizes label for an {@code AddressField}. */ - String getUrl() { - return mServerUrl; + public Builder customizeLabel(AddressField field, String label) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + if (label == null) { + throw new RuntimeException("Label cannot be null."); + } + customLabels.put(field, label); + return this; } /** - * Gets the overridden field orders with their corresponding region code. Returns null if field - * orders for {@code regionCode} is not specified. + * Sets the field order for a region code. The order you set here will override the + * predefined one. For example, you can set field order for US to be first {@code + * AddressField#ORGANIZATION} then {@code AddressField#RECIPIENT}. Repeated address fields + * in {@code fields} are not allowed. Size of {@code fields} has to be larger than one. + * Input {@code fields} can be partial or even contain field not needed in the specified + * {@code regionCode}. For example, German addresses contain the following fields + * (in order):
+ {@link AddressField#RECIPIENT}, {@link AddressField#ORGANIZATION}, {@link + * AddressField#STREET_ADDRESS}, {@link AddressField#POSTAL_CODE}, {@link + * AddressField#LOCALITY}.
+ * + *

With the following call:
+ * + * customizeFieldOrder("DE", AddressField.ORGANIZATION, AddressField.RECIPIENT, + * AddressField.ADMIN_AREA); + * + *

Field order for Germany will become:
{@link AddressField#ORGANIZATION}, {@link + * AddressField#RECIPIENT}, {@link AddressField#STREET_ADDRESS}, {@link + * AddressField#POSTAL_CODE}, {@link AddressField#LOCALITY}.

+ * + *

Notice that:

  1. {@link AddressField#ORGANIZATION} comes before {@link + * AddressField#RECIPIENT} after reordering.
  2. + *
  3. Fields not specified stays the same.
  4. + *
  5. {@link AddressField#ADMIN_AREA} is specified but since it is not in German address + * format, it is simpled neglected.
+ * + * @param fields the overridden field order. */ - AddressField[] getCustomFieldOrder(String regionCode) { - if (regionCode == null) { - throw new RuntimeException("regionCode cannot be null."); + public Builder customizeFieldOrder(String regionCode, AddressField... fields) { + if (regionCode == null) { + throw new RuntimeException("regionCode cannot be null."); + } + if (fields == null) { + throw new RuntimeException("Fields cannot be null."); + } + if (fields.length <= 1) { + throw new RuntimeException("There must be more than one field."); + } + HashSet checkList = new HashSet(); + AddressField[] f = new AddressField[fields.length]; + int i = 0; + for (AddressField field : fields) { + // Can't contain repeated address fields. + if (checkList.contains(field)) { + throw new RuntimeException("Address fields cannot be repeated."); } - return mOverrideFieldOrder.get(regionCode); + checkList.add(field); + f[i] = field; + i++; + } + overrideFieldOrder.put(regionCode, f); + return this; } /** - * Gets the customized max length for the {@code field}, or null if none. + * Sets the URL of address data server. {@code url} cannot be null. This url will override + * the default address server url. */ - Integer getCustomMaxLength(AddressField field) { - return mMaxLengths.get(field); + public Builder setUrl(String url) { + if (url == null) { + throw new RuntimeException("Can't set address server URL to null."); + } + serverUrl = url; + return this; } /** - * Class to build the form, specifying the attributes for each field. + * Customizes max length for a {@code AddressField}. */ - public static class Builder { - - private String mBaseId = "addressform"; - - private final EnumSet mRequiredFields = - EnumSet.noneOf(AddressField.class); - - private final EnumSet mHiddenFields = - EnumSet.noneOf(AddressField.class); - - private final EnumSet mReadonlyFields = - EnumSet.noneOf(AddressField.class); - - private final EnumMap mCustomLabels = - new EnumMap(AddressField.class); - - private final Map mOverrideFieldOrder = - new HashMap(); - - private final EnumMap mMaxLengths = - new EnumMap(AddressField.class); - - /** - * Uses the default server URL from CacheData. - */ - private String mServerUrl = new CacheData().getUrl(); - - /** - * Sets the base ID of the address form. - */ - public Builder baseId(String baseId) { - if (baseId == null) { - throw new RuntimeException("baseId cannot be null."); - } - mBaseId = baseId; - return this; - } - - public Builder hide(AddressField field) { - if (field == null) { - throw new RuntimeException("AddressField field cannot be null."); - } - mHiddenFields.add(field); - return this; - } - - /** - * Make a field read-only. - */ - public Builder readonly(AddressField field) { - if (field == null) { - throw new RuntimeException("AddressField field cannot be null."); - } - mReadonlyFields.add(field); - return this; - } - - /** - * Make a field required. - */ - public Builder required(AddressField field) { - if (field == null) { - throw new RuntimeException("AddressField field cannot be null."); - } - mRequiredFields.add(field); - return this; - } - - /** - * Customizes label for an {@code AddressField}. - */ - public Builder customizeLabel(AddressField field, String label) { - if (field == null) { - throw new RuntimeException("AddressField field cannot be null."); - } - if (label == null) { - throw new RuntimeException("Label cannot be null."); - } - mCustomLabels.put(field, label); - return this; - } - - /** - * Sets the field order for a region code. The order you set here will override the - * predefined one. For example, you can set field order for US to be first {@code - * AddressField#ORGANIZATION} then {@code AddressField#RECIPIENT}. Repeated address fields - * in {@code fields} are not allowed. Size of {@code fields} has to be larger than one. - * Input {@code fields} can be partial or even contain field not needed in the specified - * {@code regionCode}. For example, German addresses contain the following fields - * (in order):
- {@link AddressField#RECIPIENT}, {@link AddressField#ORGANIZATION}, {@link - * AddressField#STREET_ADDRESS}, {@link AddressField#POSTAL_CODE}, {@link - * AddressField#LOCALITY}.
- * - *

With the following call:
- * - * customizeFieldOrder("DE", AddressField.ORGANIZATION, AddressField.RECIPIENT, - * AddressField.ADMIN_AREA); - * - *

Field order for Germany will become:
{@link AddressField#ORGANIZATION}, {@link - * AddressField#RECIPIENT}, {@link AddressField#STREET_ADDRESS}, {@link - * AddressField#POSTAL_CODE}, {@link AddressField#LOCALITY}.

- * - *

Notice that:

  1. {@link AddressField#ORGANIZATION} comes before {@link - * AddressField#RECIPIENT} after reordering.
  2. - *
  3. Fields not specified stays the same.
  4. - *
  5. {@link AddressField#ADMIN_AREA} is specified but since it is not in German address - * format, it is simpled neglected.
- * - * @param fields the overridden field order. - */ - public Builder customizeFieldOrder(String regionCode, AddressField... fields) { - if (regionCode == null) { - throw new RuntimeException("regionCode cannot be null."); - } - if (fields == null) { - throw new RuntimeException("Fields cannot be null."); - } - if (fields.length <= 1) { - throw new RuntimeException("There must be more than one field."); - } - HashSet checkList = new HashSet(); - AddressField[] f = new AddressField[fields.length]; - int i = 0; - for (AddressField field : fields) { - // Can't contain repeated address fields. - if (checkList.contains(field)) { - throw new RuntimeException("Address fields cannot be repeated."); - } - checkList.add(field); - f[i] = field; - i++; - } - mOverrideFieldOrder.put(regionCode, f); - return this; - } - - /** - * Sets the URL of address data server. {@code url} cannot be null. This url will override - * the default address server url. - */ - public Builder setUrl(String url) { - if (url == null) { - throw new RuntimeException("Can't set address server URL to null."); - } - mServerUrl = url; - return this; - } - - /** - * Customizes max length for a {@code AddressField}. - */ - public Builder customizeMaxLength(AddressField field, int maxLength) { - if (field == null) { - throw new RuntimeException("AddressField field cannot be null."); - } - mMaxLengths.put(field, maxLength); - return this; - } + public Builder customizeMaxLength(AddressField field, int maxLength) { + if (field == null) { + throw new RuntimeException("AddressField field cannot be null."); + } + maxLengths.put(field, maxLength); + return this; + } - public FormOptions build() { - return new FormOptions(this); - } + public FormOptions build() { + return new FormOptions(this); } + } } -- cgit v1.2.3 From b6bf3a8c83b1ceb0bd11fcd89833ecbc18580ed2 Mon Sep 17 00:00:00 2001 From: Lara Scheidegger Date: Wed, 22 Oct 2014 21:13:33 +0200 Subject: More whitespace/line-break changes, fixed tests by using this where appropriate. --- java/src/com/android/i18n/addressinput/FormOptions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'java/src/com/android/i18n/addressinput/FormOptions.java') diff --git a/java/src/com/android/i18n/addressinput/FormOptions.java b/java/src/com/android/i18n/addressinput/FormOptions.java index 2b9bee6..ffa8c10 100644 --- a/java/src/com/android/i18n/addressinput/FormOptions.java +++ b/java/src/com/android/i18n/addressinput/FormOptions.java @@ -154,7 +154,7 @@ public class FormOptions { if (baseId == null) { throw new RuntimeException("baseId cannot be null."); } - baseId = baseId; + this.baseId = baseId; return this; } -- cgit v1.2.3