Skip to content

Commit 2e0213a

Browse files
committed
Refactor commons code.
1 parent a760cba commit 2e0213a

6 files changed

Lines changed: 73 additions & 52 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.codec;
19+
20+
import java.io.InputStream;
21+
22+
/**
23+
* Consider this class package private. Helps load resources.
24+
*
25+
* @since 1.12
26+
*/
27+
public class Resources {
28+
29+
/**
30+
* Opens the given named resource from the given class.
31+
*
32+
* @param name The resource name.
33+
* @return An input stream.
34+
*/
35+
public static InputStream getInputStream(final String name) {
36+
final InputStream inputStream = Resources.class.getClassLoader().getResourceAsStream(name);
37+
if (inputStream == null) {
38+
throw new IllegalArgumentException("Unable to resolve required resource: " + name);
39+
}
40+
return inputStream;
41+
}
42+
}

src/main/java/org/apache/commons/codec/language/DaitchMokotoffSoundex.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.commons.codec.language;
1818

19-
import java.io.InputStream;
2019
import java.util.ArrayList;
2120
import java.util.Arrays;
2221
import java.util.Collections;
@@ -30,6 +29,7 @@
3029

3130
import org.apache.commons.codec.CharEncoding;
3231
import org.apache.commons.codec.EncoderException;
32+
import org.apache.commons.codec.Resources;
3333
import org.apache.commons.codec.StringEncoder;
3434

3535
/**
@@ -226,12 +226,7 @@ public String toString() {
226226
private static final Map<Character, Character> FOLDINGS = new HashMap<>();
227227

228228
static {
229-
final InputStream rulesIS = DaitchMokotoffSoundex.class.getClassLoader().getResourceAsStream(RESOURCE_FILE);
230-
if (rulesIS == null) {
231-
throw new IllegalArgumentException("Unable to load resource: " + RESOURCE_FILE);
232-
}
233-
234-
try (final Scanner scanner = new Scanner(rulesIS, CharEncoding.UTF_8)) {
229+
try (final Scanner scanner = new Scanner(Resources.getInputStream(RESOURCE_FILE), CharEncoding.UTF_8)) {
235230
parseRules(scanner, RESOURCE_FILE, RULES, FOLDINGS);
236231
}
237232

src/main/java/org/apache/commons/codec/language/bm/Lang.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.commons.codec.language.bm;
1919

20-
import java.io.InputStream;
2120
import java.util.ArrayList;
2221
import java.util.Arrays;
2322
import java.util.Collections;
@@ -30,6 +29,8 @@
3029
import java.util.Set;
3130
import java.util.regex.Pattern;
3231

32+
import org.apache.commons.codec.Resources;
33+
3334
/**
3435
* Language guessing utility.
3536
* <p>
@@ -128,13 +129,8 @@ public static Lang instance(final NameType nameType) {
128129
*/
129130
public static Lang loadFromResource(final String languageRulesResourceName, final Languages languages) {
130131
final List<LangRule> rules = new ArrayList<>();
131-
final InputStream lRulesIS = Lang.class.getClassLoader().getResourceAsStream(languageRulesResourceName);
132-
133-
if (lRulesIS == null) {
134-
throw new IllegalStateException("Unable to resolve required resource:" + LANGUAGE_RULES_RN);
135-
}
136-
137-
try (final Scanner scanner = new Scanner(lRulesIS, ResourceConstants.ENCODING)) {
132+
try (final Scanner scanner = new Scanner(Resources.getInputStream(languageRulesResourceName),
133+
ResourceConstants.ENCODING)) {
138134
boolean inExtendedComment = false;
139135
while (scanner.hasNextLine()) {
140136
final String rawLine = scanner.nextLine();

src/main/java/org/apache/commons/codec/language/bm/Languages.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.commons.codec.language.bm;
1919

20-
import java.io.InputStream;
2120
import java.util.Collections;
2221
import java.util.EnumMap;
2322
import java.util.HashSet;
@@ -26,20 +25,24 @@
2625
import java.util.Scanner;
2726
import java.util.Set;
2827

28+
import org.apache.commons.codec.Resources;
29+
2930
/**
3031
* Language codes.
3132
* <p>
32-
* Language codes are typically loaded from resource files. These are UTF-8 encoded text files. They are
33-
* systematically named following the pattern:
34-
* <blockquote>org/apache/commons/codec/language/bm/${{@link NameType#getName()} languages.txt</blockquote>
33+
* Language codes are typically loaded from resource files. These are UTF-8
34+
* encoded text files. They are systematically named following the pattern:
35+
* <blockquote>org/apache/commons/codec/language/bm/${{@link NameType#getName()}
36+
* languages.txt</blockquote>
3537
* <p>
3638
* The format of these resources is the following:
3739
* <ul>
3840
* <li><b>Language:</b> a single string containing no whitespace</li>
39-
* <li><b>End-of-line comments:</b> Any occurrence of '//' will cause all text following on that line to be
40-
* discarded as a comment.</li>
41-
* <li><b>Multi-line comments:</b> Any line starting with '/*' will start multi-line commenting mode.
42-
* This will skip all content until a line ending in '*' and '/' is found.</li>
41+
* <li><b>End-of-line comments:</b> Any occurrence of '//' will cause all text
42+
* following on that line to be discarded as a comment.</li>
43+
* <li><b>Multi-line comments:</b> Any line starting with '/*' will start
44+
* multi-line commenting mode. This will skip all content until a line ending in
45+
* '*' and '/' is found.</li>
4346
* <li><b>Blank lines:</b> All blank lines will be skipped.</li>
4447
* </ul>
4548
* <p>
@@ -51,9 +54,12 @@
5154
* @version $Id$
5255
*/
5356
public class Languages {
54-
// Implementation note: This class is divided into two sections. The first part is a static factory interface that
55-
// exposes org/apache/commons/codec/language/bm/%s_languages.txt for %s in NameType.* as a list of supported
56-
// languages, and a second part that provides instance methods for accessing this set for supported languages.
57+
// Implementation note: This class is divided into two sections. The first part
58+
// is a static factory interface that
59+
// exposes org/apache/commons/codec/language/bm/%s_languages.txt for %s in
60+
// NameType.* as a list of supported
61+
// languages, and a second part that provides instance methods for accessing
62+
// this set for supported languages.
5763

5864
/**
5965
* A set of languages.
@@ -139,7 +145,7 @@ public LanguageSet merge(final LanguageSet other) {
139145
final SomeLanguages sl = (SomeLanguages) other;
140146
final Set<String> ls = new HashSet<>(languages);
141147
for (final String lang : sl.languages) {
142-
ls.add(lang);
148+
ls.add(lang);
143149
}
144150
return from(ls);
145151
}
@@ -169,13 +175,8 @@ public static Languages getInstance(final NameType nameType) {
169175
public static Languages getInstance(final String languagesResourceName) {
170176
// read languages list
171177
final Set<String> ls = new HashSet<>();
172-
final InputStream langIS = Languages.class.getClassLoader().getResourceAsStream(languagesResourceName);
173-
174-
if (langIS == null) {
175-
throw new IllegalArgumentException("Unable to resolve required resource: " + languagesResourceName);
176-
}
177-
178-
try (final Scanner lsScanner = new Scanner(langIS, ResourceConstants.ENCODING)) {
178+
try (final Scanner lsScanner = new Scanner(Resources.getInputStream(languagesResourceName),
179+
ResourceConstants.ENCODING)) {
179180
boolean inExtendedComment = false;
180181
while (lsScanner.hasNextLine()) {
181182
final String line = lsScanner.nextLine().trim();
@@ -191,9 +192,8 @@ public static Languages getInstance(final String languagesResourceName) {
191192
}
192193
}
193194
}
195+
return new Languages(Collections.unmodifiableSet(ls));
194196
}
195-
196-
return new Languages(Collections.unmodifiableSet(ls));
197197
}
198198

199199
private static String langResourceName(final NameType nameType) {

src/main/java/org/apache/commons/codec/language/bm/Rule.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.commons.codec.language.bm;
1919

20-
import java.io.InputStream;
2120
import java.util.ArrayList;
2221
import java.util.Arrays;
2322
import java.util.Collections;
@@ -32,6 +31,7 @@
3231
import java.util.regex.Matcher;
3332
import java.util.regex.Pattern;
3433

34+
import org.apache.commons.codec.Resources;
3535
import org.apache.commons.codec.language.bm.Languages.LanguageSet;
3636

3737
/**
@@ -254,24 +254,12 @@ private static String createResourceName(final NameType nameType, final RuleType
254254

255255
private static Scanner createScanner(final NameType nameType, final RuleType rt, final String lang) {
256256
final String resName = createResourceName(nameType, rt, lang);
257-
final InputStream rulesIS = Languages.class.getClassLoader().getResourceAsStream(resName);
258-
259-
if (rulesIS == null) {
260-
throw new IllegalArgumentException("Unable to load resource: " + resName);
261-
}
262-
263-
return new Scanner(rulesIS, ResourceConstants.ENCODING);
257+
return new Scanner(Resources.getInputStream(resName), ResourceConstants.ENCODING);
264258
}
265259

266260
private static Scanner createScanner(final String lang) {
267261
final String resName = String.format("org/apache/commons/codec/language/bm/%s.txt", lang);
268-
final InputStream rulesIS = Languages.class.getClassLoader().getResourceAsStream(resName);
269-
270-
if (rulesIS == null) {
271-
throw new IllegalArgumentException("Unable to load resource: " + resName);
272-
}
273-
274-
return new Scanner(rulesIS, ResourceConstants.ENCODING);
262+
return new Scanner(Resources.getInputStream(resName), ResourceConstants.ENCODING);
275263
}
276264

277265
private static boolean endsWith(final CharSequence input, final CharSequence suffix) {

src/test/java/org/apache/commons/codec/language/bm/BeiderMorseEncoderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void testInvalidLangIllegalArgumentException() {
115115
Rule.getInstance(NameType.GENERIC, RuleType.APPROX, "noSuchLanguage");
116116
}
117117

118-
@Test(expected = IllegalStateException.class)
118+
@Test(expected = IllegalArgumentException.class)
119119
public void testInvalidLangIllegalStateException() {
120120
Lang.loadFromResource("thisIsAMadeUpResourceName", Languages.getInstance(NameType.GENERIC));
121121
}

0 commit comments

Comments
 (0)