fixed checkstyle

This commit is contained in:
jendave
2011-09-09 15:32:27 +00:00
parent 52ef410927
commit e28fb2fcee
3 changed files with 87 additions and 69 deletions

View File

@@ -19,26 +19,26 @@ public interface PropertyElement {
* *
* @return a {@link java.lang.String} object. * @return a {@link java.lang.String} object.
*/ */
public String getKey(); String getKey();
/** /**
* Returns the type of the element. * Returns the type of the element.
* *
* @return a {@link java.lang.Class} object. * @return a {@link java.lang.Class} object.
*/ */
public Class<?> getType(); Class<?> getType();
/** /**
* Returns the value of the element. * Returns the value of the element.
* *
* @return a {@link java.lang.Object} object. * @return a {@link java.lang.Object} object.
*/ */
public Object getValue(); Object getValue();
/** /**
* Sets the property value as a string. * Sets the property value as a string.
* *
* @param value a {@link java.lang.String} object. * @param value a {@link java.lang.String} object.
*/ */
public void setValue(String value); void setValue(String value);
} }

View File

@@ -24,14 +24,14 @@ public interface PropertyType<T> {
* *
* @return a {@link java.lang.String} object. * @return a {@link java.lang.String} object.
*/ */
public String getSuffix(); String getSuffix();
/** /**
* The class that identifies this content type. * The class that identifies this content type.
* *
* @return a {@link java.lang.Class} object. * @return a {@link java.lang.Class} object.
*/ */
public Class<T> getType(); Class<T> getType();
/** /**
* Returns an object for the specified value, in the context of a TreeProperties. * Returns an object for the specified value, in the context of a TreeProperties.
@@ -40,5 +40,5 @@ public interface PropertyType<T> {
* @param s a {@link java.lang.String} object. * @param s a {@link java.lang.String} object.
* @return a T object. * @return a T object.
*/ */
public T toObject(TreeProperties p, String s); T toObject(TreeProperties p, String s);
} }

View File

@@ -82,19 +82,19 @@ import static java.util.Collections.unmodifiableList;
* @see Properties * @see Properties
*/ */
public class TreeProperties implements Iterable<PropertyElement> { public class TreeProperties implements Iterable<PropertyElement> {
/** Constant <code>suffixes</code> */ /** Constant <code>suffixes</code>. */
private static final Map<String, PropertyType<?>> suffixes; private static final Map<String, PropertyType<?>> SUFFIXES;
/** Constant <code>types</code> */ /** Constant <code>types</code>. */
private static final Map<Class<?>, PropertyType<?>> types; private static final Map<Class<?>, PropertyType<?>> TYPES;
/** Constant <code>transparent="transparent-properties"</code> */ /** Constant <code>transparent="transparent-properties"</code>. */
private static final String transparent = "transparent-properties"; private static final String TRANSPARENT = "transparent-properties";
/** Constant <code>child="properties"</code> */ /** Constant <code>child="properties"</code>. */
private static final String child = "properties"; private static final String CHILD = "properties";
static { static {
types = new HashMap<Class<?>, PropertyType<?>>(); TYPES = new HashMap<Class<?>, PropertyType<?>>();
suffixes = new HashMap<String, PropertyType<?>>(); SUFFIXES = new HashMap<String, PropertyType<?>>();
PropertyType<?>[] pt = {new FileType()}; PropertyType<?>[] pt = {new FileType()};
for (PropertyType<?> type : pt) { for (PropertyType<?> type : pt) {
addType(type); addType(type);
@@ -112,9 +112,9 @@ public class TreeProperties implements Iterable<PropertyElement> {
* *
* @param type a {@link treeProperties.PropertyType} object. * @param type a {@link treeProperties.PropertyType} object.
*/ */
public static void addType(PropertyType<?> type) { public static void addType(final PropertyType<?> type) {
types.put(type.getType(), type); TYPES.put(type.getType(), type);
suffixes.put(type.getSuffix(), type); SUFFIXES.put(type.getSuffix(), type);
} }
/** /**
@@ -122,29 +122,29 @@ public class TreeProperties implements Iterable<PropertyElement> {
* *
* @param type a {@link treeProperties.PropertyType} object. * @param type a {@link treeProperties.PropertyType} object.
*/ */
public static void removeType(PropertyType<?> type) { public static void removeType(final PropertyType<?> type) {
types.remove(type.getType()); TYPES.remove(type.getType());
suffixes.remove(type.getSuffix()); SUFFIXES.remove(type.getSuffix());
} }
/** /**
* Delegate to {@link #TreeProperties(File)} with a new {@link File#File(String)} * Delegate to {@link #TreeProperties(File)} with a new {@link File#File(String)}.
* *
* @param f a {@link java.lang.String} object. * @param f a {@link java.lang.String} object.
* @throws java.io.IOException if any. * @throws java.io.IOException if any.
*/ */
public TreeProperties(String f) throws IOException { public TreeProperties(final String f) throws IOException {
this(new File(f)); this(new File(f));
} }
/** /**
* Delegate to {@link #TreeProperties(File)} with a new {@link File#File(File, String)} * Delegate to {@link #TreeProperties(File)} with a new {@link File#File(File, String)}.
* *
* @param parent a {@link java.io.File} object. * @param parent a {@link java.io.File} object.
* @param f a {@link java.lang.String} object. * @param f a {@link java.lang.String} object.
* @throws java.io.IOException if any. * @throws java.io.IOException if any.
*/ */
public TreeProperties(File parent, String f) throws IOException { public TreeProperties(final File parent, final String f) throws IOException {
this(new File(parent, f)); this(new File(parent, f));
} }
@@ -157,11 +157,13 @@ public class TreeProperties implements Iterable<PropertyElement> {
* @param f a {@link java.io.File} object. * @param f a {@link java.io.File} object.
* @throws java.io.IOException if any. * @throws java.io.IOException if any.
*/ */
public TreeProperties(File f) throws IOException { public TreeProperties(final File f) throws IOException {
if (f == null) throw new FileNotFoundException("null"); if (f == null) {
throw new FileNotFoundException("null");
}
this.path = f.getParentFile(); this.path = f.getParentFile();
instanceTypes = new HashMap<Class<?>, PropertyType<?>>(types); instanceTypes = new HashMap<Class<?>, PropertyType<?>>(TYPES);
instanceSuffixes = new HashMap<String, PropertyType<?>>(suffixes); instanceSuffixes = new HashMap<String, PropertyType<?>>(SUFFIXES);
Properties p = new Properties(); Properties p = new Properties();
// BufferedReader r = new BufferedReader(new FileReader(f)); // BufferedReader r = new BufferedReader(new FileReader(f));
@@ -186,13 +188,15 @@ public class TreeProperties implements Iterable<PropertyElement> {
result = value; result = value;
} else { } else {
//suffix //suffix
if (parts[1].equals(transparent) || parts[1].equals(child)) { if (parts[1].equals(TRANSPARENT) || parts[1].equals(CHILD)) {
TreeProperties child = new TreeProperties(path, FileType.getPath(value)); TreeProperties child = new TreeProperties(path, FileType.getPath(value));
exceptions.addAll(child.exceptions); exceptions.addAll(child.exceptions);
result = child; result = child;
} else { } else {
PropertyType<?> t = instanceSuffixes.get(parts[1]); PropertyType<?> t = instanceSuffixes.get(parts[1]);
if (t == null) throw new IllegalArgumentException("No content type: " + parts[1]); if (t == null) {
throw new IllegalArgumentException("No content type: " + parts[1]);
}
result = t.toObject(this, value); result = t.toObject(this, value);
} }
} }
@@ -209,7 +213,7 @@ public class TreeProperties implements Iterable<PropertyElement> {
* *
* @return a {@link java.util.List} object. * @return a {@link java.util.List} object.
*/ */
public List<Exception> getExceptions() { public final List<Exception> getExceptions() {
return exceptions; return exceptions;
} }
@@ -219,8 +223,10 @@ public class TreeProperties implements Iterable<PropertyElement> {
* *
* @throws java.io.IOException if any. * @throws java.io.IOException if any.
*/ */
public void rethrow() throws IOException { public final void rethrow() throws IOException {
if (exceptions.isEmpty()) return; if (exceptions.isEmpty()) {
return;
}
StringBuilder sb = new StringBuilder("The following exceptions occurred:"); StringBuilder sb = new StringBuilder("The following exceptions occurred:");
for (Exception ex : exceptions) { for (Exception ex : exceptions) {
sb.append("\n"); sb.append("\n");
@@ -234,7 +240,7 @@ public class TreeProperties implements Iterable<PropertyElement> {
* *
* @return a {@link java.io.File} object. * @return a {@link java.io.File} object.
*/ */
public File getPath() { public final File getPath() {
return path; return path;
} }
@@ -244,8 +250,10 @@ public class TreeProperties implements Iterable<PropertyElement> {
* *
* @param key a {@link java.lang.String} object. * @param key a {@link java.lang.String} object.
*/ */
private void checkQueryKey(String key) { private void checkQueryKey(final String key) {
if (key.contains("--")) throw new IllegalArgumentException("Invalid key: " + key); if (key.contains("--")) {
throw new IllegalArgumentException("Invalid key: " + key);
}
} }
/** /**
@@ -254,7 +262,7 @@ public class TreeProperties implements Iterable<PropertyElement> {
* @param key a {@link java.lang.String} object. * @param key a {@link java.lang.String} object.
* @return a {@link java.lang.String} object. * @return a {@link java.lang.String} object.
*/ */
public String getProperty(String key) { public final String getProperty(final String key) {
return getProperty(key, String.class); return getProperty(key, String.class);
} }
@@ -264,7 +272,7 @@ public class TreeProperties implements Iterable<PropertyElement> {
* @param key a {@link java.lang.String} object. * @param key a {@link java.lang.String} object.
* @return a {@link java.io.File} object. * @return a {@link java.io.File} object.
*/ */
public File getFile(String key) { public final File getFile(final String key) {
return getProperty(key, File.class); return getProperty(key, File.class);
} }
@@ -274,8 +282,8 @@ public class TreeProperties implements Iterable<PropertyElement> {
* @param key a {@link java.lang.String} object. * @param key a {@link java.lang.String} object.
* @return a {@link treeProperties.TreeProperties} object. * @return a {@link treeProperties.TreeProperties} object.
*/ */
public TreeProperties getChildProperties(String key) { public final TreeProperties getChildProperties(final String key) {
return (TreeProperties) getProperty(key, "--" + child, true); return (TreeProperties) getProperty(key, "--" + CHILD, true);
} }
/** /**
@@ -284,8 +292,8 @@ public class TreeProperties implements Iterable<PropertyElement> {
* @param key a {@link java.lang.String} object. * @param key a {@link java.lang.String} object.
* @return a {@link treeProperties.TreeProperties} object. * @return a {@link treeProperties.TreeProperties} object.
*/ */
public TreeProperties getTransparentProperties(String key) { public final TreeProperties getTransparentProperties(final String key) {
return (TreeProperties) getProperty(key, "--" + transparent, true); return (TreeProperties) getProperty(key, "--" + TRANSPARENT, true);
} }
/** /**
@@ -297,10 +305,11 @@ public class TreeProperties implements Iterable<PropertyElement> {
* @param <T> a T object. * @param <T> a T object.
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getProperty(String key, Class<T> cls) { public final <T> T getProperty(final String key, final Class<T> cls) {
String suffix; String suffix;
if (cls == String.class) suffix = ""; if (cls == String.class) {
else { suffix = "";
} else {
PropertyType<?> t = instanceTypes.get(cls); PropertyType<?> t = instanceTypes.get(cls);
suffix = "--" + t.getSuffix(); suffix = "--" + t.getSuffix();
} }
@@ -315,18 +324,20 @@ public class TreeProperties implements Iterable<PropertyElement> {
* @param top a boolean. * @param top a boolean.
* @return a {@link java.lang.Object} object. * @return a {@link java.lang.Object} object.
*/ */
private Object getProperty(String key, String suffix, boolean top) { private Object getProperty(final String key, final String suffix, final boolean top) {
checkQueryKey(key); checkQueryKey(key);
//first, try the key in the current file, as if there were no slash //first, try the key in the current file, as if there were no slash
//No subpath - either directly in the properties... //No subpath - either directly in the properties...
Object result; Object result;
if ((result = properties.get(key + suffix)) != null) return result; if ((result = properties.get(key + suffix)) != null) {
return result;
}
//...or in a transparent properties //...or in a transparent properties
//look for all --transparent-properties //look for all --transparent-properties
for (Entry<String, Object> entry : properties.entrySet()) { for (Entry<String, Object> entry : properties.entrySet()) {
if (entry.getKey().endsWith("--" + transparent)) { if (entry.getKey().endsWith("--" + TRANSPARENT)) {
TreeProperties p = (TreeProperties) entry.getValue(); TreeProperties p = (TreeProperties) entry.getValue();
if ((result = p.getProperty(key, suffix, false)) != null) { if ((result = p.getProperty(key, suffix, false)) != null) {
return result; return result;
@@ -342,8 +353,10 @@ public class TreeProperties implements Iterable<PropertyElement> {
while ((index = key.indexOf('/', index + 1)) != -1) { while ((index = key.indexOf('/', index + 1)) != -1) {
String first = key.substring(0, index), second = key.substring(index + 1); String first = key.substring(0, index), second = key.substring(index + 1);
TreeProperties p = (TreeProperties) getProperty(first, "--" + child, false); TreeProperties p = (TreeProperties) getProperty(first, "--" + CHILD, false);
if (p == null) continue; if (p == null) {
continue;
}
if ((result = p.getProperty(second, suffix, false)) != null) { if ((result = p.getProperty(second, suffix, false)) != null) {
return result; return result;
} }
@@ -362,11 +375,9 @@ public class TreeProperties implements Iterable<PropertyElement> {
* *
* @return a {@link java.util.Iterator} object. * @return a {@link java.util.Iterator} object.
*/ */
public Iterator<PropertyElement> iterator() { public final Iterator<PropertyElement> iterator() {
return iterator(""); return iterator("");
} };
;
/** /**
* <p>iterator.</p> * <p>iterator.</p>
@@ -374,7 +385,7 @@ public class TreeProperties implements Iterable<PropertyElement> {
* @param prefix a {@link java.lang.String} object. * @param prefix a {@link java.lang.String} object.
* @return a {@link treeProperties.TreeProperties.TreePropertiesIterator} object. * @return a {@link treeProperties.TreeProperties.TreePropertiesIterator} object.
*/ */
private TreePropertiesIterator iterator(String prefix) { private TreePropertiesIterator iterator(final String prefix) {
return new TreePropertiesIterator(prefix); return new TreePropertiesIterator(prefix);
} }
@@ -384,7 +395,7 @@ public class TreeProperties implements Iterable<PropertyElement> {
private TreePropertiesIterator child; private TreePropertiesIterator child;
private PropertyElement next; private PropertyElement next;
private TreePropertiesIterator(String prefix) { private TreePropertiesIterator(final String prefix) {
entries = properties.entrySet().iterator(); entries = properties.entrySet().iterator();
this.prefix = prefix; this.prefix = prefix;
} }
@@ -392,9 +403,11 @@ public class TreeProperties implements Iterable<PropertyElement> {
//After this call, the next element is determined, or the child iterator has next //After this call, the next element is determined, or the child iterator has next
public boolean hasNext() { public boolean hasNext() {
if (next != null) return true; if (next != null) {
else if (child != null && child.hasNext()) return true; return true;
else if (entries.hasNext()) { } else if (child != null && child.hasNext()) {
return true;
} else if (entries.hasNext()) {
Entry<String, Object> entry = entries.next(); Entry<String, Object> entry = entries.next();
final String[] parts = entry.getKey().split("--"); final String[] parts = entry.getKey().split("--");
final Class<?> cls; final Class<?> cls;
@@ -402,11 +415,11 @@ public class TreeProperties implements Iterable<PropertyElement> {
if (parts.length == 1) { if (parts.length == 1) {
cls = String.class; cls = String.class;
} else if (parts[1].equals(transparent)) { } else if (parts[1].equals(TRANSPARENT)) {
child = ((TreeProperties) entry.getValue()).iterator(prefix); child = ((TreeProperties) entry.getValue()).iterator(prefix);
//recursive, for the case that the child iterator is empty //recursive, for the case that the child iterator is empty
return hasNext(); return hasNext();
} else if (parts[1].equals(TreeProperties.child)) { } else if (parts[1].equals(TreeProperties.CHILD)) {
child = ((TreeProperties) entry.getValue()).iterator(prefix + parts[0] + "/"); child = ((TreeProperties) entry.getValue()).iterator(prefix + parts[0] + "/");
//recursive, for the case that the child iterator is empty //recursive, for the case that the child iterator is empty
return hasNext(); return hasNext();
@@ -432,21 +445,26 @@ public class TreeProperties implements Iterable<PropertyElement> {
} }
public void setValue(String value) { public void setValue(final String value) {
} }
}; };
return true; return true;
} else return false; } else {
return false;
}
} }
public PropertyElement next() { public PropertyElement next() {
if (!hasNext()) throw new NoSuchElementException(); if (!hasNext()) {
else if (next != null) { throw new NoSuchElementException();
} else if (next != null) {
PropertyElement next = this.next; PropertyElement next = this.next;
this.next = null; this.next = null;
return next; return next;
} else return child.next(); } else {
return child.next();
}
} }