Skip to content

Commit

Permalink
Added data structure for exposing country descriptions
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp committed Oct 29, 2019
1 parent 0227015 commit 8a9f552
Show file tree
Hide file tree
Showing 6 changed files with 594 additions and 74 deletions.
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.
*/
private 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 resourceUtil.getHolidayDescription(Locale.getDefault(), propertiesKey);
}

/**
* 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.getHolidayDescription(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);
}
}
116 changes: 116 additions & 0 deletions src/main/java/de/jollyday/Country.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* 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.Collections;
import java.util.HashSet;
import java.util.Set;

/**
* 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 list of regions inside this country
*/
private final Set<Region> regions = new HashSet<>(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.add(region);
}

/**
* The {@link Region}s of this country.
*
* @return an unmodifiable set of {@link Region}s of this country
*/
public Set<Region> getRegions() {
return Collections.unmodifiableSet(regions);
}

@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

0 comments on commit 8a9f552

Please sign in to comment.