Repository: mina Updated Branches: refs/heads/2.0 fbccb58cc -> e29bc0315
Added missing javadoc Project: http://git-wip-us.apache.org/repos/asf/mina/repo Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/e29bc031 Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/e29bc031 Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/e29bc031 Branch: refs/heads/2.0 Commit: e29bc0315d1e31b2c16668fefb0c193599cc3ef6 Parents: fbccb58 Author: Emmanuel Lécharny <[email protected]> Authored: Mon Dec 5 10:00:10 2016 +0100 Committer: Emmanuel Lécharny <[email protected]> Committed: Mon Dec 5 10:00:10 2016 +0100 ---------------------------------------------------------------------- .../beans/AbstractPropertyEditor.java | 37 ++++++++- .../mina/integration/beans/ArrayEditor.java | 20 +++++ .../integration/beans/CollectionEditor.java | 19 ++++- .../mina/integration/beans/DateEditor.java | 17 +++- .../mina/integration/beans/EnumEditor.java | 16 +++- .../mina/integration/beans/ListEditor.java | 10 ++- .../mina/integration/beans/MapEditor.java | 84 ++++++++++++-------- .../integration/beans/PropertiesEditor.java | 6 ++ .../beans/PropertyEditorFactory.java | 50 ++++++++---- .../mina/integration/beans/SetEditor.java | 11 ++- 10 files changed, 211 insertions(+), 59 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/AbstractPropertyEditor.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/AbstractPropertyEditor.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/AbstractPropertyEditor.java index 6adc06e..0466f1b 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/AbstractPropertyEditor.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/AbstractPropertyEditor.java @@ -39,19 +39,29 @@ public abstract class AbstractPropertyEditor extends PropertyEditorSupport { this.trimText = trimText; } + /** + * {@inheritDoc} + */ @Override public String getAsText() { return text; } + /** + * {@inheritDoc} + */ @Override public Object getValue() { return value; } + /** + * {@inheritDoc} + */ @Override - public void setAsText(String text) throws IllegalArgumentException { + public void setAsText(String text) { this.text = text; + if (text == null) { value = defaultValue(); } else { @@ -59,9 +69,13 @@ public abstract class AbstractPropertyEditor extends PropertyEditorSupport { } } + /** + * {@inheritDoc} + */ @Override public void setValue(Object value) { this.value = value; + if (value == null) { text = defaultText(); } else { @@ -69,16 +83,33 @@ public abstract class AbstractPropertyEditor extends PropertyEditorSupport { } } + /** + * @return The default text + */ protected String defaultText() { return null; } + /** + * @return The default value + */ protected Object defaultValue() { return null; } + /** + * Returns a String representation of the given value + * + * @param value The value + * @return A String representation of the value + */ protected abstract String toText(Object value); - protected abstract Object toValue(String text) throws IllegalArgumentException; - + /** + * Returns an instance from a String representation of an object + * + * @param text The String representation to convert + * @return A instance of an object + */ + protected abstract Object toValue(String text); } http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ArrayEditor.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ArrayEditor.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ArrayEditor.java index fcd0844..a7353af 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ArrayEditor.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ArrayEditor.java @@ -34,6 +34,11 @@ import java.util.regex.Matcher; public class ArrayEditor extends AbstractPropertyEditor { private final Class<?> componentType; + /** + * Creates a new ArrayEditor instance + * + * @param componentType The component type + */ public ArrayEditor(Class<?> componentType) { if (componentType == null) { throw new IllegalArgumentException("componentType"); @@ -46,27 +51,35 @@ public class ArrayEditor extends AbstractPropertyEditor { private PropertyEditor getComponentEditor() { PropertyEditor e = PropertyEditorFactory.getInstance(componentType); + if (e == null) { throw new IllegalArgumentException("No " + PropertyEditor.class.getSimpleName() + " found for " + componentType.getSimpleName() + '.'); } + return e; } + /** + * {@inheritDoc} + */ @Override protected String toText(Object value) { Class<?> componentType = value.getClass().getComponentType(); + if (componentType == null) { throw new IllegalArgumentException("not an array: " + value); } PropertyEditor e = PropertyEditorFactory.getInstance(componentType); + if (e == null) { throw new IllegalArgumentException("No " + PropertyEditor.class.getSimpleName() + " found for " + componentType.getSimpleName() + '.'); } StringBuilder buf = new StringBuilder(); + for (int i = 0; i < Array.getLength(value); i++) { e.setValue(Array.get(value, i)); // TODO normalize. @@ -79,9 +92,13 @@ public class ArrayEditor extends AbstractPropertyEditor { if (buf.length() >= 2) { buf.setLength(buf.length() - 2); } + return buf.toString(); } + /** + * {@inheritDoc} + */ @Override protected Object toValue(String text) throws IllegalArgumentException { PropertyEditor e = getComponentEditor(); @@ -106,14 +123,17 @@ public class ArrayEditor extends AbstractPropertyEditor { matchedDelimiter = false; if (m.group(2) != null || m.group(3) != null) { // Skip the last '"'. + m.region(m.end() + 1, m.regionEnd()); } } Object answer = Array.newInstance(componentType, values.size()); + for (int i = 0; i < Array.getLength(answer); i++) { Array.set(answer, i, values.get(i)); } + return answer; } } http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/CollectionEditor.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/CollectionEditor.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/CollectionEditor.java index 98e4c5d..cd4065f 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/CollectionEditor.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/CollectionEditor.java @@ -39,6 +39,11 @@ public class CollectionEditor extends AbstractPropertyEditor { private final Class<?> elementType; + /** + * Creates a new CollectionEditor instance + * + * @param elementType The Element type + */ public CollectionEditor(Class<?> elementType) { if (elementType == null) { throw new IllegalArgumentException("elementType"); @@ -58,19 +63,25 @@ public class CollectionEditor extends AbstractPropertyEditor { return e; } + /** + * {@inheritDoc} + */ @Override protected final String toText(Object value) { StringBuilder buf = new StringBuilder(); + for (Object v : (Collection<?>) value) { if (v == null) { v = defaultElement(); } PropertyEditor e = PropertyEditorFactory.getInstance(v); + if (e == null) { throw new IllegalArgumentException("No " + PropertyEditor.class.getSimpleName() + " found for " + v.getClass().getSimpleName() + '.'); } + e.setValue(v); // TODO normalize. String s = e.getAsText(); @@ -82,9 +93,13 @@ public class CollectionEditor extends AbstractPropertyEditor { if (buf.length() >= 2) { buf.setLength(buf.length() - 2); } + return buf.toString(); } + /** + * {@inheritDoc} + */ @Override protected final Object toValue(String text) throws IllegalArgumentException { PropertyEditor e = getElementEditor(); @@ -107,6 +122,7 @@ public class CollectionEditor extends AbstractPropertyEditor { answer.add(e.getValue()); matchedDelimiter = false; + if (m.group(2) != null || m.group(3) != null) { // Skip the last '"'. m.region(m.end() + 1, m.regionEnd()); @@ -117,11 +133,12 @@ public class CollectionEditor extends AbstractPropertyEditor { } protected Collection<Object> newCollection() { - return new ArrayList<Object>(); + return new ArrayList<>(); } protected Object defaultElement() { PropertyEditor e = PropertyEditorFactory.getInstance(elementType); + if (e == null) { return null; } http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/DateEditor.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/DateEditor.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/DateEditor.java index 6eff93c..d7b5abc 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/DateEditor.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/DateEditor.java @@ -43,31 +43,45 @@ public class DateEditor extends AbstractPropertyEditor { new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH), new SimpleDateFormat("yyyy-MM", Locale.ENGLISH), new SimpleDateFormat("yyyy", Locale.ENGLISH), }; + /** + * Creates a new DateEditor instance + */ public DateEditor() { for (DateFormat f : formats) { f.setLenient(true); } } + /** + * {@inheritDoc} + */ @Override protected String toText(Object value) { if (value instanceof Number) { long time = ((Number) value).longValue(); + if (time <= 0) { return null; } + value = new Date(time); } + return formats[0].format((Date) value); } + /** + * {@inheritDoc} + */ @Override - protected Object toValue(String text) throws IllegalArgumentException { + protected Object toValue(String text) { if (MILLIS.matcher(text).matches()) { long time = Long.parseLong(text); + if (time <= 0) { return null; } + return new Date(time); } @@ -75,6 +89,7 @@ public class DateEditor extends AbstractPropertyEditor { try { return f.parse(text); } catch (ParseException e) { + throw new IllegalArgumentException("Wrong date: " + text); } } http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/EnumEditor.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/EnumEditor.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/EnumEditor.java index fcaeb68..be2a9ef 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/EnumEditor.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/EnumEditor.java @@ -38,6 +38,11 @@ public class EnumEditor extends AbstractPropertyEditor { private final Set<Enum<?>> enums; + /** + * Creates a new EnumEditor instance + * + * @param enumType The type of Enum + */ public EnumEditor(Class enumType) { if (enumType == null) { throw new IllegalArgumentException("enumType"); @@ -47,15 +52,22 @@ public class EnumEditor extends AbstractPropertyEditor { this.enums = EnumSet.allOf(enumType); } + /** + * {@inheritDoc} + */ @Override protected String toText(Object value) { - return (value == null ? "" : value.toString()); + return value == null ? "" : value.toString(); } + /** + * {@inheritDoc} + */ @Override - protected Object toValue(String text) throws IllegalArgumentException { + protected Object toValue(String text) { if (ORDINAL.matcher(text).matches()) { int ordinal = Integer.parseInt(text); + for (Enum<?> e : enums) { if (e.ordinal() == ordinal) { return e; http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ListEditor.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ListEditor.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ListEditor.java index b6536c1..0dc99b2 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ListEditor.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/ListEditor.java @@ -32,12 +32,20 @@ import java.util.List; */ public class ListEditor extends CollectionEditor { + /** + * Creates a new DateEditor instance + * + * @param elementType The type of element + */ public ListEditor(Class<?> elementType) { super(elementType); } + /** + * {@inheritDoc} + */ @Override protected Collection<Object> newCollection() { - return new ArrayList<Object>(); + return new ArrayList<>(); } } http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/MapEditor.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/MapEditor.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/MapEditor.java index b3db62a..bc45361 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/MapEditor.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/MapEditor.java @@ -20,6 +20,7 @@ package org.apache.mina.integration.beans; import java.beans.PropertyEditor; +import java.text.MessageFormat; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; @@ -43,14 +44,25 @@ public class MapEditor extends AbstractPropertyEditor { private final Class<?> keyType; private final Class<?> valueType; - + + private static final String NO_VALUE = "No value {1} found for {2}."; + private static final String NO_KEY = "No key {1} found for {2}."; + + /** + * Creates a new DateEditor instance + * + * @param keyType The key type + * @param valueType The value type + */ public MapEditor(Class<?> keyType, Class<?> valueType) { if (keyType == null) { throw new IllegalArgumentException("keyType"); } + if (valueType == null) { throw new IllegalArgumentException("valueType"); } + this.keyType = keyType; this.valueType = valueType; getKeyEditor(); @@ -60,19 +72,23 @@ public class MapEditor extends AbstractPropertyEditor { private PropertyEditor getKeyEditor() { PropertyEditor e = PropertyEditorFactory.getInstance(keyType); + if (e == null) { - throw new IllegalArgumentException("No key " + PropertyEditor.class.getSimpleName() + " found for " - + keyType.getSimpleName() + '.'); + throw new IllegalArgumentException(MessageFormat.format(NO_KEY, PropertyEditor.class.getSimpleName(), + keyType.getSimpleName())); } + return e; } private PropertyEditor getValueEditor() { PropertyEditor e = PropertyEditorFactory.getInstance(valueType); + if (e == null) { - throw new IllegalArgumentException("No value " + PropertyEditor.class.getSimpleName() + " found for " - + valueType.getSimpleName() + '.'); + throw new IllegalArgumentException(MessageFormat.format(NO_VALUE, PropertyEditor.class.getSimpleName(), + valueType.getSimpleName())); } + return e; } @@ -80,23 +96,26 @@ public class MapEditor extends AbstractPropertyEditor { protected final String toText(Object value) { StringBuilder buf = new StringBuilder(); - for (Object o : ((Map<?,?>) value).entrySet()) { - Map.Entry<?,?> entry = (Map.Entry<?,?>) o; + for (Map.Entry<?,?> entry : ((Map<?,?>) value).entrySet()) { Object ekey = entry.getKey(); Object evalue = entry.getValue(); PropertyEditor ekeyEditor = PropertyEditorFactory.getInstance(ekey); + if (ekeyEditor == null) { - throw new IllegalArgumentException("No key " + PropertyEditor.class.getSimpleName() + " found for " - + ekey.getClass().getSimpleName() + '.'); + throw new IllegalArgumentException(MessageFormat.format(NO_KEY, PropertyEditor.class.getSimpleName(), + ekey.getClass().getSimpleName())); } + ekeyEditor.setValue(ekey); PropertyEditor evalueEditor = PropertyEditorFactory.getInstance(evalue); + if (evalueEditor == null) { - throw new IllegalArgumentException("No value " + PropertyEditor.class.getSimpleName() + " found for " - + evalue.getClass().getSimpleName() + '.'); + throw new IllegalArgumentException(MessageFormat.format(NO_VALUE, PropertyEditor.class.getSimpleName(), + evalue.getClass().getSimpleName())); } + ekeyEditor.setValue(ekey); evalueEditor.setValue(evalue); @@ -113,26 +132,23 @@ public class MapEditor extends AbstractPropertyEditor { if (buf.length() >= 2) { buf.setLength(buf.length() - 2); } + return buf.toString(); } @Override - protected final Object toValue(String text) throws IllegalArgumentException { + protected final Object toValue(String text) { PropertyEditor keyEditor = getKeyEditor(); PropertyEditor valueEditor = getValueEditor(); Map<Object, Object> answer = newMap(); Matcher m = ELEMENT.matcher(text); TokenType lastTokenType = TokenType.ENTRY_DELIM; Object key = null; - Object value = null; + Object value; while (m.find()) { if (m.group(1) != null) { - switch (lastTokenType) { - case VALUE: - case ENTRY_DELIM: - break; - default: + if ((lastTokenType != TokenType.VALUE) && (lastTokenType != TokenType.ENTRY_DELIM)) { throw new IllegalArgumentException("Unexpected entry delimiter: " + text); } @@ -158,20 +174,20 @@ public class MapEditor extends AbstractPropertyEditor { } switch (lastTokenType) { - case ENTRY_DELIM: - keyEditor.setAsText(region); - key = keyEditor.getValue(); - lastTokenType = TokenType.KEY; - break; - case KEY_VALUE_DELIM: - valueEditor.setAsText(region); - value = valueEditor.getValue(); - lastTokenType = TokenType.VALUE; - answer.put(key, value); - break; - case KEY: - case VALUE: - throw new IllegalArgumentException("Unexpected key or value: " + text); + case ENTRY_DELIM: + keyEditor.setAsText(region); + key = keyEditor.getValue(); + lastTokenType = TokenType.KEY; + break; + case KEY_VALUE_DELIM: + valueEditor.setAsText(region); + value = valueEditor.getValue(); + lastTokenType = TokenType.VALUE; + answer.put(key, value); + break; + case KEY: + case VALUE: + throw new IllegalArgumentException("Unexpected key or value: " + text); } } @@ -179,10 +195,10 @@ public class MapEditor extends AbstractPropertyEditor { } protected Map<Object, Object> newMap() { - return new LinkedHashMap<Object, Object>(); + return new LinkedHashMap<>(); } - private static enum TokenType { + private enum TokenType { ENTRY_DELIM, KEY_VALUE_DELIM, KEY, VALUE, } } http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertiesEditor.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertiesEditor.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertiesEditor.java index 84ab331..39c02b8 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertiesEditor.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertiesEditor.java @@ -31,11 +31,17 @@ import java.util.Properties; */ public class PropertiesEditor extends MapEditor { + /** + * Creates a new DateEditor instance + */ public PropertiesEditor() { super(String.class, String.class); setTrimText(false); } + /** + * {@inheritDoc} + */ @Override protected Map<Object, Object> newMap() { return new Properties(); http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertyEditorFactory.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertyEditorFactory.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertyEditorFactory.java index 73b2ec0..08370f9 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertyEditorFactory.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/PropertyEditorFactory.java @@ -33,6 +33,15 @@ import java.util.Set; * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ public final class PropertyEditorFactory { + private PropertyEditorFactory() { + } + + /** + * Creates a new instance of editor, depending on the given object's type + * + * @param object The object we need an editor to be created for + * @return The created editor + */ @SuppressWarnings("unchecked") public static PropertyEditor getInstance(Object object) { if (object == null) { @@ -45,6 +54,7 @@ public final class PropertyEditorFactory { for (Object e : (Collection<Object>) object) { if (e != null) { elementType = e.getClass(); + break; } } @@ -72,6 +82,7 @@ public final class PropertyEditorFactory { if ((e.getKey() != null) && (e.getValue() != null)) { keyType = e.getKey().getClass(); valueType = e.getValue().getClass(); + break; } } @@ -84,7 +95,12 @@ public final class PropertyEditorFactory { return getInstance(object.getClass()); } - // parent type / property name / property type + /** + * Creates a new instance of editor, depending on the given type + * + * @param type The type of editor to create + * @return The created editor + */ public static PropertyEditor getInstance(Class<?> type) { if (type == null) { throw new IllegalArgumentException("type"); @@ -118,13 +134,12 @@ public final class PropertyEditorFactory { return new PropertiesEditor(); } - type = filterPrimitiveType(type); - try { return (PropertyEditor) PropertyEditorFactory.class .getClassLoader() .loadClass( - PropertyEditorFactory.class.getPackage().getName() + '.' + type.getSimpleName() + "Editor") + PropertyEditorFactory.class.getPackage().getName() + '.' + + filterPrimitiveType(type).getSimpleName() + "Editor") .newInstance(); } catch (Exception e) { return null; @@ -134,33 +149,38 @@ public final class PropertyEditorFactory { private static Class<?> filterPrimitiveType(Class<?> type) { if (type.isPrimitive()) { if (type == boolean.class) { - type = Boolean.class; + return Boolean.class; } + if (type == byte.class) { - type = Byte.class; + return Byte.class; } + if (type == char.class) { - type = Character.class; + return Character.class; } + if (type == double.class) { - type = Double.class; + return Double.class; } + if (type == float.class) { - type = Float.class; + return Float.class; } + if (type == int.class) { - type = Integer.class; + return Integer.class; } + if (type == long.class) { - type = Long.class; + return Long.class; } + if (type == short.class) { - type = Short.class; + return Short.class; } } + return type; } - - private PropertyEditorFactory() { - } } http://git-wip-us.apache.org/repos/asf/mina/blob/e29bc031/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/SetEditor.java ---------------------------------------------------------------------- diff --git a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/SetEditor.java b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/SetEditor.java index 813dd25..1ba7fac 100644 --- a/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/SetEditor.java +++ b/mina-integration-beans/src/main/java/org/apache/mina/integration/beans/SetEditor.java @@ -31,13 +31,20 @@ import java.util.Set; * @author <a href="http://mina.apache.org">Apache MINA Project</a> */ public class SetEditor extends CollectionEditor { - + /** + * Creates a new SetEditor instance + * + * @param elementType The Element type + */ public SetEditor(Class<?> elementType) { super(elementType); } + /** + * {@inheritDoc} + */ @Override protected Collection<Object> newCollection() { - return new LinkedHashSet<Object>(); + return new LinkedHashSet<>(); } }
