Android Open Source - ean-android Address






From Project

Back to project page ean-android.

License

The source code is released under:

Copyright (c) 2013, Expedia Affiliate Network All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that redistributions of sour...

If you think the Android project ean-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2013, Expedia Affiliate Network
 * All rights reserved.//w ww  . j av  a  2s. co  m
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that redistributions of source code
 * retain the above copyright notice, these conditions, and the following
 * disclaimer. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those
 * of the authors and should not be interpreted as representing official policies, 
 * either expressed or implied, of the Expedia Affiliate Network or Expedia Inc.
 */

package com.ean.mobile;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

/**
 * Container for address information. Should handle any international address.
 * If the address is invalid in some way, the validationErrors list will delineate in what way it is invalid. This
 * is in lieu of throwing exceptions since there are potentially unaccounted for address types which may be stored
 * with this object, and there's no need to throw exceptions for partial addresses.
 */
public class Address {

    /**
     * The country codes wherein a state code is supported and required.
     */
    public static final Set<String> VALID_STATE_PROVINCE_CODE_COUNTRY_CODES
        = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList("US", "CA", "AU")));

    /**
     * An ordered list of address lines, starting with address line 1 and maxing out around 3.
     */
    public final List<String> lines;

    /**
     * The city associated with this address.
     */
    public final String city;

    /**
     * The two-character code for the state/province containing the specified city. Only valid for cities in Australia,
     * Canada, and The United States.
     */
    public final String stateProvinceCode;

    /**
     * Two-character <a href="http://www.iso.org/iso/home/standards/country_codes/iso-3166-1_decoding_table.htm">
     *     ISO-3166</a> code for the country of this address.
     */
    public final String countryCode;

    /**
     * The postal code associated with this address. Will accept any international format.
     */
    public final String postalCode;

    /**
     * The cached representation of this object as a string. Used to prevent the string from needing to be constructed
     * every time the toString method is called.
     */
    private transient String asString;

    /**
     * Creates an address object from a JSONObject which has the appropriate address fields.
     * @param object The JSONObject from which to construct the address.
     */
    public Address(final JSONObject object) {
        final List<String> localLines = new LinkedList<String>();
        String line;
        for (int i = 1; object.has("address" + i); i++) {
            line = object.optString("address" + i);
            if (line != null && !"".equals(line)) {
                localLines.add(line);
            }
        }

        this.lines = Collections.unmodifiableList(localLines);
        this.city = object.optString("city");
        this.stateProvinceCode = object.optString("stateProvinceCode");
        this.countryCode = object.optString("countryCode");
        this.postalCode = object.optString("postalCode");
    }

    /**
     * Creates an address object from the various parameters.
     * @param addressLines The lines of the address.
     * @param city City of address.
     * @param stateProvinceCode State/province code for state.
     * @param countryCode ISO country code for country.
     * @param postalCode Postal code for country.
     */
    public Address(final List<String> addressLines, final String city, final String stateProvinceCode,
            final String countryCode, final String postalCode) {
        lines = Collections.unmodifiableList(addressLines);
        this.city = city;
        this.stateProvinceCode = stateProvinceCode;
        this.countryCode = countryCode;
        this.postalCode = postalCode;
    }

    /**
     * Gets NameValuePairs for the address information so it can be sent in a rest request.
     * @return The requested NameValuePairs
     */
    public List<NameValuePair> asBookingRequestPairs() {
        final List<NameValuePair> addressPairs = new LinkedList<NameValuePair>();
        final Iterator<String> lineIterator = lines.iterator();
        for (int i = 1; lineIterator.hasNext(); i++) {
            addressPairs.add(new BasicNameValuePair("address" + i, lineIterator.next()));
        }
        addressPairs.add(new BasicNameValuePair("city", city));
        if (VALID_STATE_PROVINCE_CODE_COUNTRY_CODES.contains(countryCode)) {
            addressPairs.add(new BasicNameValuePair("stateProvinceCode", stateProvinceCode));
        }
        addressPairs.add(new BasicNameValuePair("countryCode", countryCode));
        addressPairs.add(new BasicNameValuePair("postalCode", postalCode));

        return Collections.unmodifiableList(addressPairs);
    }

    /**
     * Returns a formatted string representing this address as it would appear on an envelope.
     * @return The formatted address.
     */
    @Override
    public String toString() {
        if (asString != null) {
            return asString;
        }
        final StringBuilder addressBuilder = new StringBuilder();
        for (String line : lines) {
            addressBuilder.append(line);
            addressBuilder.append("\n");
        }
        addressBuilder.append(city);
        addressBuilder.append(" ");
        addressBuilder.append(stateProvinceCode);
        addressBuilder.append(" ");
        addressBuilder.append(postalCode);
        addressBuilder.append("\n");
        addressBuilder.append(countryCode);
        asString = addressBuilder.toString();
        return asString;
    }

}




Java Source Code List

android.text.Html.java
android.text.Spanned.java
android.util.Log.java
com.ean.mobile.Address.java
com.ean.mobile.Constants.java
com.ean.mobile.CustomerAddress.java
com.ean.mobile.Destination.java
com.ean.mobile.Individual.java
com.ean.mobile.LatLongAddress.java
com.ean.mobile.Name.java
com.ean.mobile.activity.BookingSummary.java
com.ean.mobile.activity.HotelInformation.java
com.ean.mobile.activity.HotelList.java
com.ean.mobile.activity.ReservationDisplay.java
com.ean.mobile.activity.StartupSearch.java
com.ean.mobile.activity.package-info.java
com.ean.mobile.app.HotelImageDrawableMap.java
com.ean.mobile.app.HotelImageDrawable.java
com.ean.mobile.app.ImageFetcher.java
com.ean.mobile.app.SampleApp.java
com.ean.mobile.app.SampleConstants.java
com.ean.mobile.app.StarRating.java
com.ean.mobile.app.package-info.java
com.ean.mobile.exception.CommonParameterValidationException.java
com.ean.mobile.exception.DataValidationException.java
com.ean.mobile.exception.EanWsError.java
com.ean.mobile.exception.UriCreationException.java
com.ean.mobile.exception.UrlRedirectionException.java
com.ean.mobile.exception.package-info.java
com.ean.mobile.hotel.CancellationPolicy.java
com.ean.mobile.hotel.Cancellation.java
com.ean.mobile.hotel.ConfirmationStatus.java
com.ean.mobile.hotel.HotelImageTuple.java
com.ean.mobile.hotel.HotelInformation.java
com.ean.mobile.hotel.HotelList.java
com.ean.mobile.hotel.HotelRoom.java
com.ean.mobile.hotel.Hotel.java
com.ean.mobile.hotel.Itinerary.java
com.ean.mobile.hotel.NightlyRate.java
com.ean.mobile.hotel.Rate.java
com.ean.mobile.hotel.ReservationRoom.java
com.ean.mobile.hotel.Reservation.java
com.ean.mobile.hotel.RoomOccupancy.java
com.ean.mobile.hotel.SupplierType.java
com.ean.mobile.hotel.request.BookingRequest.java
com.ean.mobile.hotel.request.CancellationRequest.java
com.ean.mobile.hotel.request.InformationRequest.java
com.ean.mobile.hotel.request.ItineraryRequest.java
com.ean.mobile.hotel.request.ListRequest.java
com.ean.mobile.hotel.request.RoomAvailabilityRequest.java
com.ean.mobile.hotel.request.package-info.java
com.ean.mobile.hotel.package-info.java
com.ean.mobile.request.CommonParameters.java
com.ean.mobile.request.DestinationRequest.java
com.ean.mobile.request.RequestProcessor.java
com.ean.mobile.request.Request.java
com.ean.mobile.request.package-info.java
com.ean.mobile.task.ImageDrawableLoaderTask.java
com.ean.mobile.task.SuggestionFactory.java
com.ean.mobile.task.package-info.java
com.ean.mobile.package-info.java