/** * Returns the first country code from the Taggable's iso_country_code tag. BEWARE: This method * may be non-deterministic as it depends on the ordering of the country codes * * @param taggable * The {@link Taggable} to get the first country code from * @return The first country code for the item if it exists */ static Optional<IsoCountry> first(final Taggable taggable) { return Iterables.first(all(taggable)); }
/** * Tests if all of the item's iso_country_codes can be found in a list of countries * * @param countries * A set of countries we want to check in * @return Predicate used to test if all of the item's iso_country_codes can be found in the * list of countries */ static Predicate<Taggable> allIn(final IsoCountry... countries) { if (countries.length == 0) { return taggable -> false; } return taggable -> { final List<IsoCountry> checkCountries = Arrays.asList(countries); for (final IsoCountry isoCountry : all(taggable)) { // if any don't match, return false if (!checkCountries.contains(isoCountry)) { return false; } } return true; }; }
for (final IsoCountry isoCountry : all(taggable))
@Test public void testKorea() { final TestTaggable testable = new TestTaggable(ISOCountryTag.KEY, "KOR"); final List<IsoCountry> isoCountries = Lists.newArrayList(ISOCountryTag.all(testable)); Assert.assertTrue(isoCountries.size() == 1); Assert.assertTrue(isoCountries.contains(IsoCountry.forCountryCode("KOR").get())); final Iterable<IsoCountry> possibleCountry = ISOCountryTag.all(testable); Assert.assertTrue(Iterables.size(possibleCountry) > 0); final IsoCountry country = possibleCountry.iterator().next(); Assert.assertEquals(IsoCountry.forCountryCode("KOR"), Optional.of(country)); }
@Test public void testAll() { final TestTaggable testable = new TestTaggable(ISOCountryTag.KEY, "PRK,KOR,USA"); final List<IsoCountry> isoCountries = Lists.newArrayList(ISOCountryTag.all(testable)); Assert.assertTrue(isoCountries.size() == 3); Assert.assertTrue(isoCountries.contains(IsoCountry.forCountryCode("PRK").get())); Assert.assertTrue(isoCountries.contains(IsoCountry.forCountryCode("KOR").get())); Assert.assertTrue(isoCountries.contains(IsoCountry.forCountryCode("USA").get())); }
@Test public void testBestKorea() { final TestTaggable testable = new TestTaggable(ISOCountryTag.KEY, "PRK"); final List<IsoCountry> isoCountries = Lists.newArrayList(ISOCountryTag.all(testable)); Assert.assertTrue(isoCountries.size() == 1); Assert.assertTrue(isoCountries.contains(IsoCountry.forCountryCode("PRK").get())); final Iterable<IsoCountry> possibleCountry = ISOCountryTag.all(testable); Assert.assertTrue(Iterables.size(possibleCountry) > 0); Assert.assertNotEquals(IsoCountry.forCountryCode("KOR"), Optional.of(possibleCountry.iterator().next())); Assert.assertEquals(IsoCountry.forCountryCode("PRK"), Optional.of(possibleCountry.iterator().next())); }
@Test public void testFromOrdering() { // We expect PRK to be the country code because it's the first in the list final TestTaggable testable = new TestTaggable(ISOCountryTag.KEY, "PRK,KOR,USA"); final Iterable<IsoCountry> possibleCountry = ISOCountryTag.all(testable); Assert.assertTrue(Iterables.size(possibleCountry) > 0); Assert.assertEquals(IsoCountry.forCountryCode("PRK"), Optional.of(possibleCountry.iterator().next())); }