package com.bloatit.web.utils;

import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

import com.bloatit.common.FatalErrorException;

/**
 * Class to handle tools related to countries managements.
 */
public class Countries {
	
	private static final String COUNTRIES_PATH = "i18n/countries";
	private static Set<Country> availableCountries = null;
	
	/**
	 * <p>Lists all available countries ordered on their fullname</p>
	 * @return a list of the available countries
	 */
	public static Set<Country> getAvailableCountries(){
		if(availableCountries == null){
			availableCountries = new TreeSet<Country>();
			initAvailableCountries();
		}
		return availableCountries;
	}
	
	
	
	private static void initAvailableCountries(){
		try {
			Properties properties = PropertyLoader.loadProperties(COUNTRIES_PATH);
			for(Entry<?, ?> property : properties.entrySet()){
				String key = (String)property.getKey();
				String value = (String)property.getValue();
				availableCountries.add(new Country(value, key));
			}
		} catch (IOException e) {
			throw new FatalErrorException("File describing available countries is not available at "+COUNTRIES_PATH, e);
		}
	}
	
	/**
	 * Nested class to represent a country
	 */
	public static class Country implements Comparable<Country>{
		public String name;
		public String code;

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

		@Override
		public int compareTo(Country o) {
			return this.name.compareTo(o.name);
		}
	}
}

@@ -1580,18 +1580,388 @@
 o.name);%0A%09%09%7D
+%0A%0A%09%09@Override%0A%09%09public boolean equals(Object obj) %7B%0A%09%09%09if (this == obj)%0A%09%09%09%09return true;%0A%09%09%09if (obj == null)%0A%09%09%09%09return false;%0A%09%09%09if (getClass() != obj.getClass())%0A%09%09%09%09return false;%0A%09%09%09Country other = (Country) obj;%0A%09%09%09if (name == null) %7B%0A%09%09%09%09if (other.name != null)%0A%09%09%09%09%09return false;%0A%09%09%09%7D else if (!name.equals(other.name))%0A%09%09%09%09return false;%0A%09%09%09return true;%0A%09%09%7D%0A%0A%09%09%0A%09%09
 %0A%09%7D%0A%7D%0A

Fix sonar warning: Bad practice - Class defines compareTo(...) and uses Object.equals()