Skip to content

Commit

Permalink
ctors added and generalized
Browse files Browse the repository at this point in the history
  • Loading branch information
oboehm committed Dec 30, 2017
1 parent 6246e5b commit 7c80ba8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
23 changes: 18 additions & 5 deletions src/main/java/jfortune/Fortune.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,27 @@ public Fortune() {

/**
* Like the default constructor {@link Fortune#Fortune()} the default
* {@link CookieProvider} is used but with language given as argument.
* In v0.5 only "de" and "en" are provide as language but this may change
* in the future.
* {@link CookieProvider} is used but with the given names of the
* resources. If you want all resources of a language use
* {@link Fortune#Fortune(Locale, String...)}.
*
* @param names e.g. "fortunes", "literature"
*/
public Fortune(String... names) {
this(new CookieResourceProvider(names));
}

/**
* Like the default constructor {@link Fortune#Fortune()} the default
* {@link CookieProvider} is used but with language given as argument
* In v0.5 only "de", "es" and "en" are provided as language but this may
* change in the future.
*
* @param language e.g. {@link Locale#GERMAN}
* @param names e.g. "fortunes", "literature"
*/
public Fortune(Locale language) {
this(new CookieResourceProvider(language));
public Fortune(Locale language, String... names) {
this(new CookieResourceProvider(language, names));
}

/**
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/jfortune/provider/CookieResourceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ public CookieResourceProvider() {
}

/**
* Instantiates the class with the given name.
* Instantiates the class with the given names. As names you can use
* "fortunes" or "literature".
*
* @param name name of the resource, e.g. "fortunes"
* @param names names of the resource, e.g. "fortunes"
*/
public CookieResourceProvider(String name) {
public CookieResourceProvider(String... names) {
language = Locale.ENGLISH;
cookies.put(name, readSayings(Locale.ENGLISH, name));
Arrays.stream(names).forEach(s -> cookies.put(s, readSayings(s)));
}

/**
Expand All @@ -55,8 +56,9 @@ public CookieResourceProvider(String name) {
* </p>
*
* @param language e.g. {@link Locale#GERMAN}
* @param names names of the resource, e.g. "fortunes"
*/
public CookieResourceProvider(Locale language) {
public CookieResourceProvider(Locale language, String... names) {
this.language = language;
String prefix = FORTUNES_FOLDER + language.getLanguage() + "/";
ResourcepathMonitor resourceMon = ResourcepathMonitor.getInstance();
Expand All @@ -66,6 +68,7 @@ public CookieResourceProvider(Locale language) {
cookies.put(name, readSayings(language, name));
}
}
Arrays.stream(names).forEach(s -> cookies.put(s, readSayings(s)));
}

/**
Expand Down Expand Up @@ -151,15 +154,20 @@ private List<String> getSayings() {
}

private static List<String> readSayings(Locale lang, String name) {
String from = FORTUNES_FOLDER + lang.getLanguage() + "/" + name;
if (CookieResourceProvider.class.getResource(from) == null) {
from = FORTUNES_FOLDER + name;
String resource = lang.getLanguage() + "/" + name;
if (CookieResourceProvider.class.getResource(FORTUNES_FOLDER + resource) == null) {
resource = name;
}
return readSayings(resource);
}

private static List<String> readSayings(String resource) {
String from = FORTUNES_FOLDER + resource;
List<String> sayings = new ArrayList<>();
LOG.trace("Reading fortunes from {}...", from);
try (InputStream istream = CookieResourceProvider.class.getResourceAsStream(from)) {
if (istream == null) {
throw new IOException(lang + " resource '" + from + "' not found");
throw new IOException("resource '" + from + "' not found");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(istream, StandardCharsets.UTF_8));
String s = readSaying(reader);
Expand All @@ -173,7 +181,7 @@ private static List<String> readSayings(Locale lang, String name) {
LOG.debug("Reading fortunes from {} finished with {} entries.", from, sayings.size());
return sayings;
}

private static String readSaying(BufferedReader reader) throws IOException {
StringBuilder cookie = new StringBuilder();
String line = reader.readLine();
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/jfortune/FortuneTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public void getCookieLiterature() {
assertThat(cookie, is(notNullValue()));
}

/**
* We mix German cookies with some Spanish cookie to see what is the
* result.
*/
@Test
public void getGermanAndOtherCookies() {
Fortune mixed = new Fortune(Locale.GERMAN, "fortunes", "es/lao-tse");
Cookie cookie = fortune.getShortCookie();
assertThat(cookie, is(notNullValue()));
LOG.info(cookie);
}

/**
* Test method for {@link Fortune#main(String[])}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void testGetLongCookieInt() {
}

/**
* Test method for {@link CookieResourceProvider#CookieResourceProvider(String)}.
* Test method for {@link CookieResourceProvider#CookieResourceProvider(String...)}.
*/
@Test
public void testCookieResourceProviderString() {
Expand All @@ -106,7 +106,7 @@ public void testCookieResourceProviderString() {
}

/**
* Test mehthod for {@link CookieResourceProvider#CookieResourceProvider(Locale)}.
* Test mehthod for {@link CookieResourceProvider#CookieResourceProvider(Locale, String...)}.
*/
@Test
public void testGermanCookies() {
Expand Down

0 comments on commit 7c80ba8

Please sign in to comment.