Skip to content

Commit

Permalink
Update the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
blongho committed Jun 6, 2022
1 parent d8dddcf commit 1b3ea87
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ swedishFlag.setImageResource(R.drawable.se);
6. Get a Country with attributes like `"id":4,"name":"Afghanistan","alpha2":"af","alpha3":"afg", flag:imageResource"`

```java
final Country afghanistan = World.getCountryFrom("af|afg|afghanistan|4");
final Country afghanistan = World.getCountryFrom("af|afg|afghanistan|4|kabul");
// Log.d(TAG, afghanistan.toString());
```

Expand All @@ -172,7 +172,10 @@ final List<Country> africanCounties = World.getCountriesFrom(Continent.AFRICA);
*NEW*
9. Get the list of languages spoken in a given country
```java
final List<String> languages = country.getLanguages();
final List<String> languages = World.getLanguagesFrom("af|afg|afghanistan|4|kabul");
//or
final Country afghanistan = World.getCountryFrom("af|afg|afghanistan|4|kabul");
final List<String> languages = afghanistan.getLanguages();
// Returns comma separated list of country languages e.g [Swedish (sv-SE), Northern Sami (se), Southern Sami (sma), Finnish (fi-SE)]
```
Link to javadoc --> [javadoc link](https://blongho.github.io/worldCountryData/doc/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,11 @@ void setCurrency(final Currency currency) {

/* package */
boolean hasProperty(final String attribute) {
return attribute.equalsIgnoreCase(alpha2) || attribute.equalsIgnoreCase(alpha3)
|| attribute.equalsIgnoreCase(name) || attribute.equalsIgnoreCase(String.valueOf(getId()));
return attribute.equalsIgnoreCase(alpha2)
|| attribute.equalsIgnoreCase(alpha3)
|| attribute.equalsIgnoreCase(name)
|| attribute.equalsIgnoreCase(capital)
|| attribute.equalsIgnoreCase(String.valueOf(getId()));
}

/**
Expand Down
33 changes: 30 additions & 3 deletions country_data/src/main/java/com/blongho/country_data/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.blongho.country_data.exception.CountryDataException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -124,7 +126,7 @@ public static Country getCountryFrom(final int numericCode) {
/**
* Get a country from any of its identifiers
*
* @param countryIdentifier The country name, alpha2 or alpha3 values, case insensitive
* @param countryIdentifier The country name, alpha2 or alpha3 values, capital, case insensitive
* @return A country a country with any of the attributes or a Earth
*/
public static Country getCountryFrom(final String countryIdentifier) {
Expand All @@ -138,8 +140,8 @@ public static Country getCountryFrom(final String countryIdentifier) {
/**
* Get a list of all the countries with their flags
*
* @return List of all the countries. <br> Attempting to modify this list invokes an
* com.blongho.country_data.exception and your app will crash.
* @return List of all the countries. <br> Attempting to modify this list invokes an {@link
* CountryDataException} and your app will crash.
*/
public static List<Country> getAllCountries() {
if (instance == null) {
Expand All @@ -162,6 +164,31 @@ public static List<Currency> getAllCurrencies() {
return WorldData.currencies();
}

/**
* Get the languages spoken by a country
*
* @param countryIdentifier the country identifier as described in {@link World#getCountryFrom(int)}
* @return comma-separated list of languages and their symbols or an empty list
*/
public static List<String> getLanguagesFrom(final String countryIdentifier) {
if (instance == null) {
throw new CountryDataException(
"You have to call World.init(getApplicationContext()) before this method.");
}
return WorldData.languagesFrom(countryIdentifier);
}

/**
* Get spoken languages from a country. Same as in {@link World#getLanguagesFrom(String)} except
* that this one takes the country's numeric code
*
* @param countryIdentifier the numeric country code
* @return @see {@link World#getLanguagesFrom(String)}
*/
public static List<String> getLanguagesFrom(final int countryIdentifier) {
return getLanguagesFrom(String.valueOf(countryIdentifier));
}

/**
* Get Countries from a continent
*
Expand Down
33 changes: 10 additions & 23 deletions country_data/src/main/java/com/blongho/country_data/WorldData.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* @since 2020-02-29 Changes classname from WorldBuilder to WorldData. *Builder is misleading since
* this class does not follow th Builder pattern
* @since 2021-01-12 Filters countries to exclude data with null values and updates country data
* @since 2022-06-07 Adds languages to the country information and how to get them
*/

import android.content.Context;
Expand All @@ -49,7 +50,7 @@

final class WorldData {

public final static String CURRENT_VERSION = "1.5.1";
public final static String CURRENT_VERSION = "1.5.3";
private static final String TAG = "WorldData";
private static final Map<String, Currency> currencyMap = new HashMap<>(); // {alpha2, Currency}
private static final Map<Country, Integer> countryFlagMap = new HashMap<>();
Expand Down Expand Up @@ -93,12 +94,7 @@ static WorldData getInstance(Context ctx) {
/* package */
static List<Currency> currencies() {
List<Currency> currencyList = new ArrayList<>(currencyMap.values());
Collections.sort(currencyList, new Comparator<Currency>() {
@Override
public int compare(final Currency o1, final Currency o2) {
return o1.getCountry().compareTo(o2.getCountry());
}
});
Collections.sort(currencyList, (o1, o2) -> o1.getCountry().compareTo(o2.getCountry()));
return currencyList;
}

Expand Down Expand Up @@ -159,6 +155,13 @@ public int compare(final Country o1, final Country o2) {
return countryList;
}


/* package*/
static List<String> languagesFrom(String countryIdentifier) {
final List<String> languages = countryFrom(countryIdentifier).getLanguages();
return (languages == null ? new ArrayList<>() : languages);
}

/**
* Load the countries and their flags in a Map container
* <br>
Expand All @@ -183,9 +186,6 @@ private void loadAllData(Context context) {
country.setFlagResource(countryFlag);
country.setCurrency(currencyMap.get(country.getAlpha2().toLowerCase()));
countryFlagMap.put(country, countryFlag);
/* if (isValid(country)) {
countryFlagMap.put(country, countryFlag);
}*/
if (country.getAlpha2().equalsIgnoreCase("xx")) {
universe = country;
universe.setFlagResource(globe());
Expand Down Expand Up @@ -214,20 +214,7 @@ private void loadCurrencies(Context context) {
final Currency[] currencies = gson.fromJson(currencyArray, Currency[].class);
for (final Currency currency : currencies) {
currencyMap.put(currency.getCountry().toLowerCase(), currency);
/* if (isValid(currency)) {
currencyMap.put(currency.getCountry().toLowerCase(), currency);
}*/
}
}

private boolean isValid(final Country country) {
return country != null && country.getCurrency() != null && country.getContinent() != null
&& country.getName() != null;
}

private boolean isValid(final Currency currency) {
return currency != null && currency.getSymbol() != null && currency.getName() != null
&& currency.getCode() != null;
}
}

0 comments on commit 1b3ea87

Please sign in to comment.