Skip to content

Commit 29352fc

Browse files
committed
Fix javadoc in the "commons" module
See #396.
1 parent d35fe04 commit 29352fc

File tree

13 files changed

+242
-17
lines changed

13 files changed

+242
-17
lines changed

metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ public static Properties loadProperties(final String location)
148148
return loadProperties(getStream(location));
149149
}
150150

151+
/**
152+
* Loads properties from an InputStream.
153+
*
154+
* @param stream properties as InputStream
155+
* @return Properties
156+
* @throws IOException
157+
*/
151158
public static Properties loadProperties(final InputStream stream)
152159
throws IOException {
153160
final Properties properties;
@@ -156,10 +163,24 @@ public static Properties loadProperties(final InputStream stream)
156163
return properties;
157164
}
158165

166+
/**
167+
* Loads properties from an URL.
168+
*
169+
* @param url properties as URL
170+
* @return Properties
171+
* @throws IOException
172+
*/
159173
public static Properties loadProperties(final URL url) throws IOException {
160174
return loadProperties(url.openStream());
161175
}
162176

177+
/**
178+
* Loads a text file.
179+
*
180+
* @param location filename
181+
* @return the content of the file
182+
* @throws IOException
183+
*/
163184
public static String loadTextFile(final String location) throws IOException {
164185
final StringBuilder builder = new StringBuilder();
165186
final BufferedReader reader = new BufferedReader(getReader(location));
@@ -173,6 +194,14 @@ public static String loadTextFile(final String location) throws IOException {
173194
return builder.toString();
174195
}
175196

197+
/**
198+
* * Loads a text file.
199+
*
200+
* @param location the filename
201+
* @param list a List of Strings
202+
* @return a List of Strings of the content of the filename, line-by-line
203+
* @throws IOException
204+
*/
176205
public static List<String> loadTextFile(final String location,
177206
final List<String> list) throws IOException {
178207
final BufferedReader reader = new BufferedReader(getReader(location));
@@ -186,13 +215,28 @@ public static List<String> loadTextFile(final String location,
186215
return list;
187216
}
188217

218+
/**
219+
* Reads an InputStream with the given Charset.
220+
*
221+
* @param inputStream the InputStream
222+
* @param encoding the Charset
223+
* @return a String of the content of the InputStream
224+
* @throws IOException
225+
*/
189226
public static String readAll(final InputStream inputStream, final Charset encoding)
190227
throws IOException {
191228
try (Reader reader = new InputStreamReader(inputStream, encoding)) {
192229
return readAll(reader);
193230
}
194231
}
195232

233+
/**
234+
* Reads a Reader.
235+
*
236+
* @param reader the Reader
237+
* @return a String of the content of the Reader
238+
* @throws IOException
239+
*/
196240
public static String readAll(final Reader reader) throws IOException {
197241
final StringBuilder loadedText = new StringBuilder();
198242
try (Reader bufferedReader = new BufferedReader(reader)) {

metafacture-commons/src/main/java/org/metafacture/commons/StringUtil.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ private StringUtil() {
3434
// no instances allowed
3535
}
3636

37+
/**
38+
* Sets a fallback of an Object if the the Object is null.
39+
*
40+
* @param <O> a class
41+
* @param value the Object
42+
* @param fallbackValue the default Object
43+
* @return an Object
44+
*/
3745
public static <O> O fallback(final O value, final O fallbackValue) {
3846
if (value == null) {
3947
return fallbackValue;
@@ -55,6 +63,17 @@ public static String format(final String format, final boolean ignoreMissingVars
5563
return format(format, DEFAULT_VARSTART, DEFAULT_VAREND, ignoreMissingVars, variables);
5664
}
5765

66+
/**
67+
* Formats a String. If a String has a variable it will be replaced based on a
68+
* Map. The start and the end of indicating this variable must be defined.
69+
*
70+
* @param format the String to be formatted
71+
* @param varStartIndicator a String indicating the start of a variable
72+
* @param varEndIndicator a String indicating the end of a variable
73+
* @param ignoreMissingVars boolean if an unassigned variable should be ignored
74+
* @param variables a Map of variable names and their values
75+
* @return a formatted String
76+
*/
5877
public static String format(final String format, final String varStartIndicator, final String varEndIndicator,
5978
final boolean ignoreMissingVars, final Map<String, String> variables) {
6079
if (format.indexOf(varStartIndicator) < 0) { // shortcut if there is

metafacture-commons/src/main/java/org/metafacture/commons/TimeUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ private TimeUtil() {
3939
// No instances allowed
4040
}
4141

42+
/**
43+
* Formats a duration to human readable abbrevations. See {@link TimeUtilTest}
44+
* how to use it.
45+
*
46+
* @param duration a long value of the duration
47+
* @return a human readable format of the duration
48+
*/
4249
public static String formatDuration(final long duration) {
4350
long major = duration;
4451
long minor = 0;

metafacture-commons/src/main/java/org/metafacture/commons/XmlUtil.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public static String nodeToString(final Node node) {
4949
return nodeToString(node, false);
5050
}
5151

52+
/**
53+
* Converts a Node to a String, with or without an XML declaration
54+
*
55+
* @param node the Node
56+
* @param omitXMLDecl boolean if an XML declaration is ommited or not
57+
* @return a String representation of the Node
58+
*/
5259
public static String nodeToString(final Node node,
5360
final boolean omitXMLDecl) {
5461
final StringWriter writer = new StringWriter();
@@ -79,6 +86,12 @@ public static String nodeToString(final Node node,
7986
return writer.toString();
8087
}
8188

89+
/**
90+
* Converts a NodeList to a String.
91+
*
92+
* @param nodes the NodeList
93+
* @return a String representation od the NodeList
94+
*/
8295
public static String nodeListToString(final NodeList nodes) {
8396
final StringBuilder builder = new StringBuilder();
8497

@@ -89,6 +102,12 @@ public static String nodeListToString(final NodeList nodes) {
89102
return builder.toString();
90103
}
91104

105+
/**
106+
* Checks if a String is an Xml Mime Type.
107+
*
108+
* @param mimeType the Mime Type
109+
* @return boolean if a String is an Xml Mime Type
110+
*/
92111
public static boolean isXmlMimeType(final String mimeType) {
93112
if (mimeType == null) {
94113
return false;
@@ -102,6 +121,14 @@ public static String escape(final String unescaped) {
102121
return escape(unescaped, true);
103122
}
104123

124+
/**
125+
* Escapes XML special characters. May also escape non-ASCII characters (aka
126+
* unicode).
127+
*
128+
* @param unescaped the String to be unescaped
129+
* @param escapeUnicode boolean if unicode should be also escaped
130+
* @return the escaped String
131+
*/
105132
public static String escape(final String unescaped, final boolean escapeUnicode) {
106133
return unescaped.codePoints()
107134
.mapToObj(value -> escapeCodePoint(value, escapeUnicode))

metafacture-commons/src/main/java/org/metafacture/commons/reflection/ConfigurableClass.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public Class<T> getPlainClass() {
5454
return plainClass;
5555
}
5656

57+
/**
58+
* Gets all "public set" methods of this class.
59+
*
60+
* @return the Map of all setter methods of this class
61+
*/
5762
public Map<String, Method> getSetters() {
5863
if (settersCache == null) {
5964
initSettersCache();
@@ -82,6 +87,11 @@ private boolean isSetter(final Method method) {
8287
return false;
8388
}
8489

90+
/**
91+
* Gets the class types of the setter methods.
92+
*
93+
* @return a Map of the setter methods name and their types
94+
*/
8595
public Map<String, Class<?>> getSetterTypes() {
8696
final Map<String, Class<?>> setterTypes = new HashMap<>();
8797
for (final Map.Entry<String, Method> method : getSetters().entrySet()) {
@@ -95,6 +105,14 @@ public T newInstance() {
95105
return newInstance(Collections.emptyMap());
96106
}
97107

108+
/**
109+
* Creates an instance of the class using the first constructor that matches the
110+
* varargs argument of the methods.
111+
*
112+
* @param setterValues
113+
* @param constructorArgs
114+
* @return the new instance
115+
*/
98116
public T newInstance(final Map<String, String> setterValues, final Object... constructorArgs) {
99117
try {
100118
final Constructor<T> constructor = findConstructor(constructorArgs);

metafacture-commons/src/main/java/org/metafacture/commons/reflection/ObjectFactory.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ public class ObjectFactory<T> {
4040
public ObjectFactory() {
4141
}
4242

43+
/**
44+
* Loads classes from a map.
45+
*
46+
* @param classMap the map of classes
47+
* @param baseType the object type of the classes
48+
*/
4349
public final void loadClassesFromMap(final Map<?, ?> classMap, final Class<T> baseType) {
4450
final ClassLoader loader = ReflectionUtil.getContextClassLoader();
4551
for (final Entry<?, ?> entry : classMap.entrySet()) {
@@ -61,6 +67,14 @@ public final T newInstance(final String key, final Object... constructorArgs) {
6167
return newInstance(key, Collections.emptyMap(), constructorArgs);
6268
}
6369

70+
/**
71+
* Returns a new instance of a ConfigurableClass.
72+
*
73+
* @param key the name of the class
74+
* @param values the Map of Strings of the setters
75+
* @param constructorArgs the args of the constructor
76+
* @return a new instance
77+
*/
6478
public final T newInstance(final String key, final Map<String, String> values, final Object... constructorArgs) {
6579
if (!classes.containsKey(key)) {
6680
throw new NoSuchElementException("no registered class for: " + key);

metafacture-commons/src/main/java/org/metafacture/commons/reflection/ReflectionUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ private ReflectionUtil() {
2828
throw new AssertionError("No instances allowed");
2929
}
3030

31+
/**
32+
* @return the context ClassLoader for this thread, or null indicating the
33+
* system class loader (or, failing that, the bootstrap class loader)
34+
*/
3135
public static ClassLoader getContextClassLoader() {
3236
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
3337
if (loader == null) {
@@ -40,6 +44,15 @@ public static <T> ConfigurableClass<? extends T> loadClass(final String classNam
4044
return loadClass(getContextClassLoader(), className, baseType);
4145
}
4246

47+
/**
48+
* Wraps a Class to a ConfigurableClass.
49+
*
50+
* @param <T> the object type of the ConfigurableClass
51+
* @param loader the ClassLoader
52+
* @param className the name of the class
53+
* @param baseType the object typed class to be wrapped
54+
* @return the ConfigurableClass
55+
*/
4356
public static <T> ConfigurableClass<? extends T> loadClass(final ClassLoader loader, final String className, final Class<T> baseType) {
4457
final Class<?> clazz;
4558
try {

metafacture-commons/src/main/java/org/metafacture/commons/tries/SetMatcher.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public final class SetMatcher<T> {
3636
public SetMatcher() {
3737
}
3838

39+
/**
40+
* Puts a value to a key.
41+
*
42+
* @param key the key
43+
* @param value the value
44+
*/
3945
public void put(final String key, final T value) {
4046
if (isPrepared) {
4147
throw new IllegalStateException("keys cannot be added during matching.");
@@ -63,6 +69,12 @@ else if (next.getValue() == null) {
6369
}
6470
}
6571

72+
/**
73+
* Gets the List of Matches of a text.
74+
*
75+
* @param text the text
76+
* @return List of Matches
77+
*/
6678
public List<Match<T>> match(final String text) {
6779
if (!isPrepared) {
6880
prepare();
@@ -173,6 +185,13 @@ public static final class Match<T> {
173185
private final int start;
174186
private final int length;
175187

188+
/**
189+
* Constructs a Match.
190+
*
191+
* @param value the value
192+
* @param start the position
193+
* @param length the length
194+
*/
176195
public Match(final T value, final int start, final int length) {
177196
this.value = value;
178197
this.start = start;

metafacture-commons/src/main/java/org/metafacture/commons/tries/SetReplacer.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.Map;
2626

2727
/**
28+
* Replaces Strings by other Strings.
29+
*
2830
* @author Markus Michael Geipel
2931
*
3032
*/
@@ -34,20 +36,37 @@ public final class SetReplacer {
3436
public SetReplacer() {
3537
}
3638

37-
public void addReplacement(final String key, final String with) {
38-
matcher.put(key, with);
39+
/**
40+
* Adds a replacement of a String by an other String.
41+
*
42+
* @param toReplace String to replace
43+
* @param replacement String of replacement
44+
*/
45+
public void addReplacement(final String toReplace, final String replacement) {
46+
matcher.put(toReplace, replacement);
3947
}
4048

49+
/**
50+
* Adds replacements given as a Map of Strings.
51+
*
52+
* @param replacements the Map of Strings to be replaced
53+
*/
4154
public void addReplacements(final Map<String, String> replacements) {
4255
for (final Entry<String, String> entry : replacements.entrySet()) {
4356
addReplacement(entry.getKey(), entry.getValue());
4457
}
4558
}
4659

60+
/**
61+
* Replaces the Strings defined with {@link #addReplacement(String, String)} in
62+
* the text.
63+
*
64+
* @param text
65+
* @return the text with the replacements
66+
*/
4767
public String replaceIn(final String text) {
4868
final List<SetMatcher.Match<String>> matches = matcher.match(text);
4969
final StringBuilder builder = new StringBuilder();
50-
5170
int lastCut = 0;
5271

5372
Collections.sort(matches, new Comparator<SetMatcher.Match<String>>() {
@@ -56,25 +75,17 @@ public int compare(final Match<String> o1, final Match<String> o2) {
5675
final int delta = o1.getStart() - o2.getStart();
5776
return delta < 0 ? -1 : delta > 0 ? 1 : o1.getLength() > o2.getLength() ? -1 : 1;
5877
}
59-
6078
});
6179

6280
for (final SetMatcher.Match<String> match : matches) {
63-
6481
if (match.getStart() < lastCut) {
6582
continue;
6683
}
67-
68-
// System.out.println(match.getStart() + " "+ match.getValue() +" "+
69-
// match.getLength());
70-
7184
builder.append(text.substring(lastCut, match.getStart()));
7285
builder.append(match.getValue());
73-
7486
lastCut = match.getStart() + match.getLength();
7587
}
7688
builder.append(text.substring(lastCut, text.length()));
77-
7889
return builder.toString();
7990
}
8091

0 commit comments

Comments
 (0)