Android Open Source - ean-android Room Occupancy






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.//from w w w.  j  a  v  a  2s  . c  o 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.hotel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONObject;

/**
 * The holder for information about a particular room's adult and child occupancy.
 */
public final class RoomOccupancy {
    /**
     * The number of adults expected to occupy the room.
     */
    public final int numberOfAdults;

    /**
     * The list of children's ages that will be occupying said room.
     */
    public final List<Integer> childAges;

    /**
     * The hash code calculated at construction.
     */
    private final int hashCode;

    /**
     * The primary constructor setting the final variables in this class.
     * @param numberOfAdults The number of adults in this occupancy.
     * @param childAges The list of children's ages for this room.
     */
    public RoomOccupancy(final int numberOfAdults, final List<Integer> childAges) {
        this.numberOfAdults = numberOfAdults;
        this.childAges
            = childAges == null
            ? Collections.<Integer>emptyList()
            : Collections.unmodifiableList(childAges);
        this.hashCode = preCalculateHashCode(this);
    }

    /**
     * This constructs a room occupancy object from a JSONObject who has the field numberOfAdults and one of
     * [numberOfChildren] and [childAges]. If the object has both [numberOfChildren] and [childAges], child ages
     * is used for the basis of the list, and child ages of 0 are appended to the list.
     * @param object The JSONObject from which to construct the list.
     */
    public RoomOccupancy(final JSONObject object) {
        this.numberOfAdults = object.optInt("numberOfAdults");
        final List<Integer> localChildAges;
        if (object.has("childAges")) {
            final String[] ageStrings = object.optString("childAges").split(",");
            localChildAges = new ArrayList<Integer>(ageStrings.length);
            for (String age : ageStrings) {
                localChildAges.add(Integer.parseInt(age));
            }
            if (object.has("numberOfChildren") && object.optInt("numberOfChildren") > localChildAges.size()) {
                localChildAges.addAll(Collections.nCopies(object.optInt("numberOfChildren"), 0));
            }
        } else if (object.has("numberOfChildren")) {
            localChildAges = Collections.nCopies(object.optInt("numberOfChildren"), 0);
        } else {
            localChildAges = Collections.emptyList();
        }

        this.childAges = Collections.unmodifiableList(localChildAges);
        this.hashCode = preCalculateHashCode(this);
    }

    /**
     * Gets the hash code for a room occupancy.
     * @param occupancy The room occupancy for which to calculate the hash code.
     * @return The hash code for the occupancy
     */
    private static int preCalculateHashCode(final RoomOccupancy occupancy) {
        final int primeNumber = 31;
        int hashCode = occupancy.numberOfAdults == 0 ? 1 : occupancy.numberOfAdults;
        hashCode *= primeNumber * occupancy.childAges.size() == 0 ? 1 : occupancy.childAges.size();
        return hashCode;

    }

    /**
     * Gets this object as a string in the abbreviated form required by the rest requests of the api.
     * @return The string formatted as follows: [numberOfAdults],[childAge],[childAge],... where each child age
     * is printed.
     */
    public String asAbbreviatedRequestString() {
        final StringBuilder adultsAndChildren = new StringBuilder((childAges.size() * 2) + 1);
        adultsAndChildren.append(numberOfAdults);
        for (int childAge : childAges) {
            adultsAndChildren.append(",");
            adultsAndChildren.append(childAge);
        }
        return adultsAndChildren.toString();
    }

    /**
     * A pair of room occupancies which have the same NUMBER of children and the same NUMBER of adults are declared
     * to be equal.
     *
     * {@inheritDoc}
     */
    @Override
    public boolean equals(final Object obj) {
        return obj instanceof RoomOccupancy && this.hashCode() == obj.hashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return hashCode;
    }
}




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