Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added data structure for exposing country descriptions #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions src/main/java/de/jollyday/AbstractI18nObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Copyright 2019 Sven Diedrichsen
*
* 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 de.jollyday;

import java.util.Locale;

import de.jollyday.util.ResourceUtil;

/**
* Represents a localizable object.
*
* @author Christoph Weitkamp
* @version $Id: $
*/
public abstract class AbstractI18nObject {
/**
* The calculated hashcode cached for performance.
*/
protected int hashCode = 0;
/**
* The properties key to retrieve the description with.
*/
protected final String propertiesKey;

/**
* Utility for accessing resources.
*/
protected final ResourceUtil resourceUtil = new ResourceUtil();

/**
* Constructs a country using the provided ISO code to retrieve the
* description with.
*
* @param propertiesKey
* a {@link java.lang.String} object.
*/
public AbstractI18nObject(String propertiesKey) {
super();
this.propertiesKey = propertiesKey;
}

/**
* <p>
* Getter for the <code>propertiesKey</code>.
* </p>
*
* @return the country properties key
*/
public String getPropertiesKey() {
return propertiesKey;
}

/**
* The description read with the default locale.
*
* @return Description for this object
*/
public String getDescription() {
return getDescription(Locale.getDefault());
}

/**
* The description read with the provided locale.
*
* @param locale
* a {@link java.util.Locale} object.
* @return Description for this object
*/
public String getDescription(Locale locale) {
return resourceUtil.getCountryDescription(locale, propertiesKey);
}

@Override
public String toString() {
return propertiesKey + " (" + getDescription() + ")";
}

@Override
public abstract int hashCode();
}
126 changes: 126 additions & 0 deletions src/main/java/de/jollyday/City.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* Copyright 2019 Sven Diedrichsen
*
* 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 de.jollyday;

/**
* Represents a city and contains the city code and a localized description.
*
* @author Christoph Weitkamp
* @version $Id: $
*/
public final class City extends AbstractI18nObject implements Comparable<City> {
/**
* The ISO code to retrieve the description with.
*/
private final String isoCode;
/**
* The region code to retrieve the description with.
*/
private final String regionCode;
/**
* The city code to retrieve the description with.
*/
private final String code;

/**
* Constructs a city using the provided code to retrieve the description
* with.
*
* @param isoCode
* a {@link java.lang.String} object.
* @param regionCode
* a {@link java.lang.String} object.
* @param code
* a {@link java.lang.String} object.
*/
public City(String isoCode, String regionCode, String code) {
super(isoCode + "." + regionCode + "." + code);
this.isoCode = isoCode;
this.regionCode = regionCode;
this.code = code;
}

/**
* <p>
* Getter for the field <code>isoCode</code>.
* </p>
*
* @return the ISO code
*/
public String getISOCode() {
return isoCode;
}

/**
* <p>
* Getter for the field <code>regionCode</code>.
* </p>
*
* @return the region code
*/
public String getRegionCode() {
return regionCode;
}

/**
* <p>
* Getter for the field <code>code</code>.
* </p>
*
* @return the city code
*/
public String getCode() {
return code;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof City) {
City other = (City) obj;
return isoCode.equals(other.isoCode) && regionCode.equals(other.regionCode) && code.equals(other.code);
}
return false;
}

@Override
public int hashCode() {
if (hashCode == 0) {
int hash = 1;
hash = hash * 31 + isoCode.hashCode();
hash = hash * 31 + regionCode.hashCode();
hash = hash * 31 + code.hashCode();
hashCode = hash;
}
return hashCode;
}

/**
* Compares this city to another city.
*
* The comparison is primarily based on the city code.
*
* @param other
* the other city to compare to, not null
* @return the comparator value, negative if less, positive if greater
*/
@Override
public int compareTo(City other) {
return code.compareTo(other.code);
}
}
128 changes: 128 additions & 0 deletions src/main/java/de/jollyday/Country.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/**
* Copyright 2019 Sven Diedrichsen
*
* 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 de.jollyday;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* Represents a country. It contains the ISO code, a localized description and a
* list of regions.
*
* @author Christoph Weitkamp
* @version $Id: $
*/
public final class Country extends AbstractI18nObject implements Comparable<Country> {
/**
* The ISO code to retrieve the description with.
*/
private final String isoCode;
/**
* A map of regions inside this country
*/
private final Map<String, Region> regions = new HashMap<>(0);

/**
* Constructs a country using the provided ISO code to retrieve the
* description with.
*
* @param isoCode
* a {@link java.lang.String} object.
*/
public Country(String isoCode) {
super(isoCode);
this.isoCode = isoCode;
}

/**
* <p>
* Getter for the field <code>isoCode</code>.
* </p>
*
* @return the ISO code
*/
public String getISOCode() {
return isoCode;
}

/**
* Adds a {@link Region} to this country.
*
* @param region
* a {@link Region} object.
*/
public void addRegion(Region region) {
regions.put(region.getCode(), region);
}

/**
* The {@link Region}s of this country.
*
* @return an unmodifiable list of all {@link Region}s of this country
*/
public Collection<Region> getRegions() {
return Collections.unmodifiableCollection(regions.values());
}

/**
* Returns a specific {@link Region}s of this country.
*
* @param regionCode
* {@link java.lang.String}the region code.
* @return a {@link Region} (could be <code>null</code>)
*/
public Region getRegion(String regionCode) {
return regions.get(regionCode);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Country) {
Country other = (Country) obj;
return isoCode.equals(other.isoCode) && regions.equals(other.regions);
}
return false;
}

@Override
public int hashCode() {
if (hashCode == 0) {
int hash = 1;
hash = hash * 31 + isoCode.hashCode();
hashCode = hash;
}
return hashCode;
}

/**
* Compares this country to another country.
*
* The comparison is primarily based on the ISO code.
*
* @param other
* the other country to compare to, not null
* @return the comparator value, negative if less, positive if greater
*/
@Override
public int compareTo(Country other) {
return isoCode.compareTo(other.isoCode);
}
}
Loading