/*
* @(#)Address.java
*
* Copyright 2009 Instituto Superior Tecnico
* Founding Authors: João Figueiredo, Luis Cruz, Paulo Abrantes, Susana Fernandes
*
* https://fenix-ashes.ist.utl.pt/
*
* This file is part of the Bennu Web Application Infrastructure.
*
* The Bennu Web Application Infrastructure is free software: you can
* redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation, either version
* 3 of the License, or (at your option) any later version.
*
* Bennu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Bennu. If not, see <http://www.gnu.org/licenses/>.
*
*/
package module.finance.util;
import java.io.Serializable;
/**
*
* @author Luis Cruz
*
*/
public class Address implements Serializable {
private static final long serialVersionUID = -2280768861883875056L;
private final String line1;
private final String line2;
private final String postalCode;
private final String location;
private final String country;
public Address(final String line1, final String line2, final String postalCode, final String location, final String country) {
if (line1 == null || line1.isEmpty()) {
throw new IllegalArgumentException("error.address.line1.cannot.be.empty");
}
this.line1 = line1;
this.line2 = line2;
if (location == null || location.isEmpty()) {
throw new IllegalArgumentException("error.address.location.cannot.be.empty");
}
this.location = location;
if (country == null || country.isEmpty()) {
throw new IllegalArgumentException("error.address.country.cannot.be.empty");
}
this.country = country;
if (postalCode == null || postalCode.isEmpty()) {
throw new IllegalArgumentException("error.address.postalCode.cannot.be.empty");
}
this.postalCode = postalCode;
}
public String getLine1() {
return line1;
}
public String getLine2() {
return line2;
}
public String getPostalCode() {
return postalCode;
}
public String getLocation() {
return location;
}
public String getCountry() {
return country;
}
@Override
public boolean equals(Object obj) {
return false;
}
public boolean equals(final Address address) {
return address != null && match(getLine1(), address.getLine1()) && match(getLine2(), address.getLine2())
&& match(getPostalCode(), address.getPostalCode()) && match(getLocation(), address.getLocation())
&& match(getCountry(), address.getCountry());
}
private boolean match(final String string1, final String string2) {
return (string1 == null && string2 == null) || (string1 != null && string1.equals(string2));
}
static final String CHARACTER = ":";
static final String SEPARATOR = CHARACTER + CHARACTER;
public String exportAsString() {
final StringBuilder builder = new StringBuilder();
builder.append(escapeFieldSeparator(getLine1()));
builder.append(SEPARATOR);
builder.append(escapeFieldSeparator(getLine2()));
builder.append(SEPARATOR);
builder.append(escapeFieldSeparator(getPostalCode()));
builder.append(SEPARATOR);
builder.append(escapeFieldSeparator(getLocation()));
builder.append(SEPARATOR);
builder.append(escapeFieldSeparator(getCountry()));
return builder.toString();
}
private String escapeFieldSeparator(String field) {
return field != null ? field.replaceAll(CHARACTER, "\\\\" + CHARACTER + "\\\\") : "";
}
public static Address importFromString(String string) {
String[] split = string.split(SEPARATOR);
return new Address(removeEscapeFieldSeparator(split[0]), removeEscapeFieldSeparator(split[1]),
removeEscapeFieldSeparator(split[2]), removeEscapeFieldSeparator(split[3]), removeEscapeFieldSeparator(split[4]));
}
private static String removeEscapeFieldSeparator(String field) {
return field != null ? field.replaceAll("\\\\" + CHARACTER + "\\\\", CHARACTER) : null;
}
}