存储国家codeS,名称,最好的办法,并在Java的大陆 [英] Best way to store Country codes, names, and Continent in Java

查看:149
本文介绍了存储国家codeS,名称,最好的办法,并在Java的大陆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个列表阵列某种,存储关于每个国家的信息:

I want to have a List or Array of some sort, storing this information about each country:


  • 2字母code

  • 国家名称,如巴西

  • 世界如东欧,北美等
  • 洲/地区。

我将手动各个国家分为区域/大陆(但如果存在一种能够自动做到这一点,你让我知道)。这个问题是关于如何存储和访问的国家。例如,我希望能够找回所有的国家,在北美。

I will classify each country into the region/continent manually (but if there exists a way to do this automatically, do let me know). This question is about how to store and access the countries. For example, I want to be able to retrieve all the countries in North America.

我不想使用本地文本文件或因为这样该项目将使用谷歌Web工具包转换为JavaScript。但在枚举或某种其他资源文件存储,保持从code的其余部分分开,是我真的很后。

I don't want to use local text files or such because this project will be converted to javascript using Google Web Toolkit. But storing in an Enum or another resource file of some sort, keeping it separate from the rest of the code, is what I'm really after.

推荐答案

有246个国家在ISO 3166时,可能会在这回的继电器大枚举。我preFER使用XML文件的国家名单,你可以从的 http://www.iso.org/ 并加载它们(例如,当应用程序被启动)。
不是,因为你需要他们在GWT加载它们早在RPC调用,但记得要缓存的(某种延迟加载),所以你不会,每次加载它们完成。
我认为这将是无论如何比$ C $下保持他们更好,因为你会加载完整列表,每次模块访问结束,即使用户将不再需要使用此列表。

There is 246 countries in ISO 3166, you might get a relay big enum on back of this. I prefer to use XML file with list of countries, you can download one from http://www.iso.org/ and load them (e.g. when app is starting). Than, as you need them in GWT load them in back as RPC call, but remember to cache those (some kind of lazy loading) so you wont finish with loading them each time. I think this would be anyway better than holding them in code, as you will finish with loading full list each time module is accessed, even if user will not need to use this list.

所以,你需要的东西将举行国家:

So you need something which will hold country:

public class Country
{
    private final String name;
    private final String code;

    public Country(String name, String code)
    {
        this.name = name;
        this.code = code;
    }

    public String getName()
    {
        return name;
    }

    public String getCode()
    {
        return code;
    }

    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null || getClass() != obj.getClass())
        {
            return false;
        }

        Country country = (Country) obj;

        return code.equals(country.code);
    }

    public int hashCode()
    {
        return code.hashCode();
    }
}

有关GWT此类需要实现IsSerializable。
您还可以使用加载这些,在服务器端:

For GWT this class would need to implement IsSerializable. And you can load those, on server side using:

import java.util.ArrayList;
import java.util.List;
import java.io.InputStream;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class CountriesService
{
    private static final String EL_COUNTRY = "ISO_3166-1_Entry";
    private static final String EL_COUNTRY_NAME = "ISO_3166-1_Country_name";
    private static final String EL_COUNTRY_CODE = "ISO_3166-1_Alpha-2_Code_element";
    private List<Country> countries = new ArrayList<Country>();

    public CountriesService(InputStream countriesList)
    {
        parseCountriesList(countriesList);
    }

    public List<Country> getCountries()
    {
        return countries;
    }

    private void parseCountriesList(InputStream countriesList)
    {
        countries.clear();
        try
        {
            Document document = parse(countriesList);
            Element root = document.getRootElement();
            //noinspection unchecked
            Iterator<Element> i = root.elementIterator(EL_COUNTRY);
            while (i.hasNext())
            {
                Element countryElement = i.next();
                Element countryName = countryElement.element(EL_COUNTRY_NAME);
                Element countryCode = countryElement.element(EL_COUNTRY_CODE);

                String countryname = countryName.getText();
                countries.add(new Country(countryname, countryCode.getText()));
            }
        }
        catch (DocumentException e)
        {
            log.error(e, "Cannot read countries list");
        }
        catch (IOException e)
        {
            log.error(e, "Cannot read countries list");
        }
    }

    public static Document parse(InputStream inputStream) throws DocumentException
    {
        SAXReader reader = new SAXReader();
        return reader.read(inputStream);
    }
}

当然,如果你需要通过ISO 2字母$ C $找到C国则可能惯于改变清单很可能地图。
如果,正如你提到的,你按大洲需要独立的国家,你可能会从ISO 3166扩展XML并添加自己的元素。只要检查其(ISO网站)许可证。

Of course, if you need to find country by ISO 2 letter code you might wont to change List to Map probably. If, as you mentioned, you need separate countries by continent, you might extend XML from ISO 3166 and add your own elements. Just check their (ISO website) license.

这篇关于存储国家codeS,名称,最好的办法,并在Java的大陆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆