Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.whichdoctor.beans; import java.util.ArrayList; import org.apache.commons.lang.StringUtils; /** * The Class AddressBean. * * @author David Harrison 30th May 2003 */ public class AddressBean extends ContactBean { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The address fields. */ private ArrayList<String> addressFields = new ArrayList<String>(); /** The state. */ private String state; /** The state abbreviation. */ private String stateAbbreviation; /** The country. */ private String country; /** The country abbreviation. */ private String countryAbbreviation; /** The region. */ private String region; /** The post code. */ private String postCode; /** The geocode. */ private String geocode; /** The verification status. */ private String verificationStatus; /** The Constant MAX_FIELD_COUNT. */ public static final int MAX_FIELD_COUNT = 7; /** * Gets the address field. * * @param fieldId the field id * * @return the address field */ public final String getAddressField(final int fieldId) { if (this.addressFields == null) { this.addressFields = new ArrayList<String>(); } String addressField = ""; try { addressField = this.addressFields.get(fieldId); } catch (Exception e) { addressField = ""; } return addressField.trim(); } /** * Sets the address field. * * @param field the new address field */ public final void setAddressField(final String field) { if (StringUtils.isNotBlank(field)) { this.addressFields.add(field); } } /** * Gets the suburb. * * @return the suburb */ public final String getSuburb() { String suburb = ""; if (this.addressFields != null) { int addressFieldCount = this.addressFields.size(); if (addressFieldCount > 1) { try { suburb = this.addressFields.get(addressFieldCount - 2); } catch (Exception e) { suburb = ""; } } } return suburb.trim(); } /** * Gets the city. * * @return the city */ public final String getCity() { String city = ""; if (this.addressFields != null) { int addressFieldCount = this.addressFields.size(); if (addressFieldCount > 0) { try { city = this.addressFields.get(addressFieldCount - 1); } catch (Exception e) { city = ""; } } } return city.trim(); } /** * Gets the address field count. * * @return the address field count */ public final int getAddressFieldCount() { int fieldCount = 0; if (this.addressFields != null) { fieldCount = this.addressFields.size(); } return fieldCount; } /** * Sets the state. * * @param stateVal the new state */ public final void setState(final String stateVal) { this.state = stateVal; } /** * Gets the state. * * @return the state */ public final String getState() { if (this.state == null) { this.state = ""; } return this.state.trim(); } /** * Sets the state abbreviation. * * @param stateAbbreviationVal the new state abbreviation */ public final void setStateAbbreviation(final String stateAbbreviationVal) { this.stateAbbreviation = stateAbbreviationVal; } /** * Gets the state abbreviation. * * @return the state abbreviation */ public final String getStateAbbreviation() { if (this.stateAbbreviation == null) { this.stateAbbreviation = ""; } return this.stateAbbreviation.trim(); } /** * Sets the country. * * @param countryVal the new country */ public final void setCountry(final String countryVal) { this.country = countryVal; } /** * Gets the country. * * @return the country */ public final String getCountry() { if (this.country == null) { this.country = ""; } return this.country.trim(); } /** * Sets the country abbreviation. * * @param countryAbbreviationVal the new country abbreviation */ public final void setCountryAbbreviation(final String countryAbbreviationVal) { this.countryAbbreviation = countryAbbreviationVal; } /** * Gets the country abbreviation. * * @return the country abbreviation */ public final String getCountryAbbreviation() { if (this.countryAbbreviation == null) { this.countryAbbreviation = ""; } return this.countryAbbreviation.trim(); } /** * Sets the region. * * @param regionVal the new region */ public final void setRegion(final String regionVal) { this.region = regionVal; } /** * Gets the region. * * @return the region */ public final String getRegion() { if (this.region == null) { this.region = ""; } return this.region.trim(); } /** * Sets the post code. * * @param postCodeVal the new post code */ public final void setPostCode(final String postCodeVal) { this.postCode = postCodeVal; } /** * Gets the post code. * * @return the post code */ public final String getPostCode() { if (this.postCode == null) { this.postCode = ""; } return this.postCode.trim(); } /** * Sets the geocode. * * @param geocodeVal the new geocode */ public final void setGeocode(final String geocodeVal) { this.geocode = geocodeVal; } /** * Gets the geocode. * * @return the geocode */ public final String getGeocode() { if (this.geocode == null) { this.geocode = ""; } return this.geocode; } /** * Gets the latitude. * * @return the latitude */ public final String getLatitude() { String latitude = ""; if (getGeocode().indexOf(",") > 0) { latitude = getGeocode().substring(getGeocode().indexOf("(") + 1, getGeocode().indexOf(",")).trim(); } return latitude; } /** * Gets the longitude. * * @return the longitude */ public final String getLongitude() { String longitude = ""; if (getGeocode().indexOf(",") > 0) { longitude = getGeocode().substring(getGeocode().indexOf(",") + 1, getGeocode().indexOf(")")).trim(); } return longitude; } /** * Sets the verification status. * * @param verificationStatusVal the new verification status value */ public final void setVerificationStatus(final String verificationStatusVal) { this.verificationStatus = verificationStatusVal; } /** * Gets the verification status. * * @param verificationStatusVal the new verification status value */ public final String getVerificationStatus() { if (StringUtils.isBlank(verificationStatus)) { verificationStatus = "Unverified address"; } return verificationStatus; } /** * Checks if the address has been verified. * * @return true, if the address is verified */ public final boolean isVerified() { boolean verified = false; if (StringUtils.startsWithIgnoreCase(this.getVerificationStatus(), "Verified")) { verified = true; } return verified; } /** * Gets the address string. * * @param ignoreField the ignore field * * @return the address string */ public final String getAddressString(final int ignoreField) { StringBuffer address = new StringBuffer(); for (int x = 0; x < MAX_FIELD_COUNT; x++) { if (x >= ignoreField) { if (StringUtils.isNotBlank(this.getAddressField(x))) { address.append(this.getAddressField(x)); address.append(", "); } } } if (this.getState().compareTo("") != 0) { address.append(this.getState()); address.append(", "); } if (this.getCountry().compareTo("") != 0) { address.append(this.getCountry()); } return address.toString().replaceAll(" ", "+"); } /** * Compare the existing address with the updated version. If the two are * different return true. * * @param address the updated address * @param compareOnlyAddress the flag whether to compare only the address * * @return boolean different */ public final boolean compare(final AddressBean address, final boolean compareOnlyAddress) { boolean different = false; if (address == null) { different = true; } else { for (int i = 0; i < MAX_FIELD_COUNT; i++) { final String existingField = this.getAddressField(i); final String updatedField = address.getAddressField(i); if (!StringUtils.equals(existingField, updatedField)) { different = true; } } if (!StringUtils.equals(this.getCity(), address.getCity())) { different = true; } if (!StringUtils.equals(this.getState(), address.getState())) { different = true; } if (!StringUtils.equals(this.getCountry(), address.getCountry())) { different = true; } if (!StringUtils.equals(this.getPostCode(), address.getPostCode())) { different = true; } if (!compareOnlyAddress) { if (this.getPrimary() != address.getPrimary()) { different = true; } String existingType = this.getContactClass() + " - " + this.getContactType(); String updatedType = address.getContactClass() + " - " + address.getContactType(); if (!StringUtils.equals(existingType, updatedType)) { different = true; } } } return different; } /** * Reset the address fields. */ public final void reset() { this.addressFields = new ArrayList<String>(); this.state = ""; this.stateAbbreviation = ""; this.country = ""; this.countryAbbreviation = ""; this.postCode = ""; } }