package com.cedarsoft.business.contact.areacode;
import com.cedarsoft.business.AbstractCodesProvider;
import com.cedarsoft.business.contact.CityCodeProvider;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Locale;
/**
* Provides the German area codes.
*/
public class GermanAreaCodesProvider extends AbstractCodesProvider implements CityCodeProvider {
@NonNls
private static final String RESOURCE_NAME_DE_PLZ_TXT = "de_area.txt";
/**
* Creates a new GermanAreaCodesProvider. Within this method the area codes are read from a file.
*
* @throws IOException
*/
public GermanAreaCodesProvider() throws IOException {
URL resource = getClass().getResource( RESOURCE_NAME_DE_PLZ_TXT );
if ( resource == null ) {
throw new IOException( "Resource \"" + RESOURCE_NAME_DE_PLZ_TXT + "\" not found" );
}
LineIterator iterator = IOUtils.lineIterator( resource.openStream(), "UTF-8" );
while ( iterator.hasNext() ) {
String line = iterator.nextLine();
String[] parts = line.split( "\t" );
if ( parts.length < 2 ) {
continue;
}
String code = parts[0];
String cityName = parts[1];
int index = cityName.indexOf( ',' );
if ( index > -1 ) {
cityName = cityName.substring( 0, index );
}
if ( code.length() == 0 ) {
throw new IllegalStateException( "Invalid code: " + cityName );
}
if ( cityName.length() == 0 ) {
throw new IllegalStateException( "Invalid city name " + code );
}
code2names.put( code, cityName );
name2codes.put( cityName, code );
}
}
@NotNull
@NonNls
public Collection<String> getCityNames( @NotNull @NonNls String code ) {
return getNames( code );
}
@NotNull
@NonNls
public String getCountryCode() {
return Locale.GERMANY.getCountry();
}
}