You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2012/10/12 14:52:40 UTC

svn commit: r1397546 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/

Author: mduerig
Date: Fri Oct 12 12:52:40 2012
New Revision: 1397546

URL: http://svn.apache.org/viewvc?rev=1397546&view=rev
Log:
OAK-350: Unify PropertyState and CoreValue
- Javadoc
- use Preconditions.check... methods instead of explicit ifs

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/EmptyPropertyState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericPropertyState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericsPropertyState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MultiPropertyState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/SinglePropertyState.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringPropertyState.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/EmptyPropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/EmptyPropertyState.java?rev=1397546&r1=1397545&r2=1397546&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/EmptyPropertyState.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/EmptyPropertyState.java Fri Oct 12 12:52:40 2012
@@ -27,13 +27,23 @@ import com.google.common.collect.Iterabl
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Type;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static org.apache.jackrabbit.oak.api.Type.BINARIES;
 import static org.apache.jackrabbit.oak.api.Type.STRING;
 import static org.apache.jackrabbit.oak.api.Type.STRINGS;
 
+/**
+ * Abstract base class for {@link PropertyState} implementations
+ * providing default implementation which correspond to a property
+ * without any value.
+ */
 abstract class EmptyPropertyState implements PropertyState {
     private final String name;
 
+    /**
+     * Create a new property state with the given {@code name}
+     * @param name  The name of the property state.
+     */
     protected EmptyPropertyState(String name) {
         this.name = name;
     }
@@ -44,39 +54,54 @@ abstract class EmptyPropertyState implem
         return name;
     }
 
+    /**
+     * @return {@code true}
+     */
     @Override
     public boolean isArray() {
         return true;
     }
 
+    /**
+     * @return An empty list if {@code type.isArray()} is {@code true}.
+     * @throws IllegalArgumentException {@code type.isArray()} is {@code false}.
+     */
     @SuppressWarnings("unchecked")
     @Nonnull
     @Override
     public <T> T getValue(Type<T> type) {
-        if (type.isArray()) {
-            return (T) Collections.emptyList();
-        }
-        else {
-            throw new IllegalStateException("Not a single valued property");
-        }
+        checkArgument(type.isArray(), "Type must be an array type");
+        return (T) Collections.emptyList();
     }
 
+    /**
+     * @throws IndexOutOfBoundsException always
+     */
     @Nonnull
     @Override
     public <T> T getValue(Type<T> type, int index) {
         throw new IndexOutOfBoundsException(String.valueOf(index));
     }
 
+    /**
+     * @throws IllegalStateException always
+     */
     @Override
     public long size() {
         throw new IllegalStateException("Not a single valued property");
     }
 
+    /**
+     * @throws IndexOutOfBoundsException always
+     */
     @Override
     public long size(int index) {
         throw new IndexOutOfBoundsException(String.valueOf(index));
     }
 
+    /**
+     * @return {@code 0}
+     */
     @Override
     public int count() {
         return 0;

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericPropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericPropertyState.java?rev=1397546&r1=1397545&r2=1397546&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericPropertyState.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericPropertyState.java Fri Oct 12 12:52:40 2012
@@ -24,6 +24,9 @@ public class GenericPropertyState extend
     private final String value;
     private final Type<?> type;
 
+    /**
+     * @throws IllegalArgumentException if {@code type.isArray()} is {@code true}
+     */
     protected GenericPropertyState(String name, String value, Type<?> type) {
         super(name);
         checkArgument(!type.isArray());

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericsPropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericsPropertyState.java?rev=1397546&r1=1397545&r2=1397546&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericsPropertyState.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/GenericsPropertyState.java Fri Oct 12 12:52:40 2012
@@ -27,6 +27,9 @@ import static com.google.common.base.Pre
 public class GenericsPropertyState extends MultiPropertyState<String> {
     private final Type<?> type;
 
+    /**
+     * @throws IllegalArgumentException if {@code type.isArray()} is {@code false}
+     */
     protected GenericsPropertyState(String name, List<String> values, Type<?> type) {
         super(name, values);
         checkArgument(type.isArray());

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MultiPropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MultiPropertyState.java?rev=1397546&r1=1397545&r2=1397546&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MultiPropertyState.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/MultiPropertyState.java Fri Oct 12 12:52:40 2012
@@ -29,17 +29,41 @@ import com.google.common.collect.Iterabl
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.Type;
 
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Abstract base class for multi valued {@code PropertyState} implementations.
+ */
 abstract class MultiPropertyState<T> extends EmptyPropertyState {
     protected final List<T> values;
 
+    /**
+     * Create a new property state with the given {@code name}
+     * and {@code values}
+     * @param name  The name of the property state.
+     * @param values  The values of the property state.
+     */
     protected MultiPropertyState(String name, List<T> values) {
         super(name);
         this.values = values;
     }
 
+    /**
+     * @return  {@code Iterable} of the string representations of the values
+     * of the property state.
+     */
     protected abstract Iterable<String> getStrings();
+
+    /**
+     * @param index
+     * @return  String representation of the value at {@code index }of the
+     * property state.
+     */
     protected abstract String getString(int index);
 
+    /**
+     * @return  The values of this property state as {@link Blob}s
+     */
     protected Iterable<Blob> getBlobs() {
         return Iterables.transform(getStrings(), new Function<String, Blob>() {
             @Override
@@ -49,6 +73,9 @@ abstract class MultiPropertyState<T> ext
         });
     }
 
+    /**
+     * @return  The values of this property state as {@code Long}s
+     */
     protected Iterable<Long> getLongs() {
         return Iterables.transform(getStrings(), new Function<String, Long>() {
             @Override
@@ -58,6 +85,9 @@ abstract class MultiPropertyState<T> ext
         });
     }
 
+    /**
+     * @return  The values of this property state as {@code Double}s
+     */
     protected Iterable<Double> getDoubles() {
         return Iterables.transform(getStrings(), new Function<String, Double>() {
             @Override
@@ -67,10 +97,21 @@ abstract class MultiPropertyState<T> ext
         });
     }
 
+    /**
+     * @return  The values of this property state as {@code Booleans}s
+     */
     protected Iterable<Boolean> getBooleans() {
-        throw new UnsupportedOperationException("Unsupported conversion.");
+        return Iterables.transform(getStrings(), new Function<String, Boolean>() {
+            @Override
+            public Boolean apply(String value) {
+                return StringPropertyState.getBoolean(value);
+            }
+        });
     }
 
+    /**
+     * @return  The values of this property state as {@code BigDecimal}s
+     */
     protected Iterable<BigDecimal> getDecimals() {
         return Iterables.transform(getStrings(), new Function<String, BigDecimal>() {
             @Override
@@ -80,34 +121,55 @@ abstract class MultiPropertyState<T> ext
         });
     }
 
+    /**
+     * @param index
+     * @return  The value at the given {@code index} as {@link Blob}
+     */
     protected Blob getBlob(int index) {
         return new StringBasedBlob(getString(index));
     }
 
+    /**
+     * @param index
+     * @return  The value at the given {@code index} as {@code long}
+     */
     protected long getLong(int index) {
         return Long.parseLong(getString(index));
     }
 
+    /**
+     * @param index
+     * @return  The value at the given {@code index} as {@code double}
+     */
     protected double getDouble(int index) {
         return Double.parseDouble(getString(index));
     }
 
+    /**
+     * @param index
+     * @return  The value at the given {@code index} as {@code boolean}
+     */
     protected boolean getBoolean(int index) {
-        throw new UnsupportedOperationException("Unsupported conversion.");
+        return StringPropertyState.getBoolean(getString(index));
     }
 
+    /**
+     * @param index
+     * @return  The value at the given {@code index} as {@code BigDecimal}
+     */
     protected BigDecimal getDecimal(int index) {
         return new BigDecimal(getString(index));
     }
 
+    /**
+     * @throws IllegalArgumentException if {@code type} is not one of the
+     * values defined in {@link Type} or if {@code type.isArray()} is {@code false}.
+     */
     @SuppressWarnings("unchecked")
     @Nonnull
     @Override
     public <T> T getValue(Type<T> type) {
-        if (!type.isArray()) {
-            throw new IllegalStateException("Not a single valued property");
-        }
-
+        checkArgument(type.isArray(), "Type must not be an array type");
         switch (type.tag()) {
             case PropertyType.STRING: return (T) getStrings();
             case PropertyType.BINARY: return (T) getBlobs();
@@ -125,13 +187,16 @@ abstract class MultiPropertyState<T> ext
         }
     }
 
+    /**
+     * @throws IllegalArgumentException if {@code type} is not one of the
+     * values defined in {@link Type} or if {@code type.isArray()} is {@code true}
+     * @throws IndexOutOfBoundsException if {@code index >= count()}.
+     */
     @SuppressWarnings("unchecked")
     @Nonnull
     @Override
     public <T> T getValue(Type<T> type, int index) {
-        if (type.isArray()) {
-            throw new IllegalArgumentException("Nested arrays not supported");
-        }
+        checkArgument(!type.isArray(), "Type must not be an array type");
         if (index >= count()) {
             throw new IndexOutOfBoundsException(String.valueOf(index));
         }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/SinglePropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/SinglePropertyState.java?rev=1397546&r1=1397545&r2=1397546&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/SinglePropertyState.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/SinglePropertyState.java Fri Oct 12 12:52:40 2012
@@ -26,53 +26,112 @@ import javax.jcr.PropertyType;
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.api.Type;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Collections.singleton;
 
+/**
+ * Abstract base class for single valued {@code PropertyState} implementations.
+ */
 abstract class SinglePropertyState extends EmptyPropertyState {
 
+    /**
+     * Create a new property state with the given {@code name}
+     * @param name  The name of the property state.
+     */
+    protected SinglePropertyState(String name) {
+        super(name);
+    }
+
+    /**
+     * Utility method defining the conversion from {@code String}
+     * to {@code long}.
+     * @param value  The string to convert to a long
+     * @return  The long value parsed from {@code value}
+     * @throws NumberFormatException  if the string does not contain a
+     * parseable long.
+     */
     public static long getLong(String value) {
         return Long.parseLong(value);
     }
 
+    /**
+     * Utility method defining the conversion from {@code String}
+     * to {@code double}.
+     * @param value  The string to convert to a double
+     * @return  The double value parsed from {@code value}
+     * @throws NumberFormatException  if the string does not contain a
+     * parseable double.
+     */
     public static double getDouble(String value) {
         return Double.parseDouble(value);
     }
 
+    /**
+     * Utility method defining the conversion from {@code String}
+     * to {@code BigDecimal}.
+     * @param value  The string to convert to a BigDecimal
+     * @return  The BigDecimal value parsed from {@code value}
+     * @throws NumberFormatException  if the string does not contain a
+     * parseable BigDecimal.
+     */
     public static BigDecimal getDecimal(String value) {
         return new BigDecimal(value);
     }
 
-    protected SinglePropertyState(String name) {
-        super(name);
-    }
-
+    /**
+     * String representation of the value of the property state.
+     * @return
+     */
     protected abstract String getString();
 
+    /**
+     * @return  A {@link StringBasedBlob} instance created by calling
+     * {@link #getString()}.
+     */
     protected Blob getBlob() {
         return new StringBasedBlob(getString());
     }
 
+    /**
+     * @return  {@code getLong(getString())}
+     */
     protected long getLong() {
         return getLong(getString());
     }
 
+    /**
+     * @return  {@code getDouble(getString())}
+     */
     protected double getDouble() {
         return getDouble(getString());
     }
 
+    /**
+     * @return  {@code StringPropertyState.getBoolean(getString())}
+     */
     protected boolean getBoolean() {
-        return Boolean.parseBoolean(getString());
+        return StringPropertyState.getBoolean(getString());
     }
 
+    /**
+     * @return  {@code getDecimal(getString())}
+     */
     protected BigDecimal getDecimal() {
         return getDecimal(getString());
     }
 
+    /**
+     * @return  {@code false}
+     */
     @Override
     public boolean isArray() {
         return false;
     }
 
+    /**
+     * @throws IllegalArgumentException if {@code type} is not one of the
+     * values defined in {@link Type}.
+     */
     @SuppressWarnings("unchecked")
     @Nonnull
     @Override
@@ -113,23 +172,32 @@ abstract class SinglePropertyState exten
         }
     }
 
+    /**
+     * @throws IllegalArgumentException  if {@code type.isArray} is {@code true}
+     * @throws IndexOutOfBoundsException  if {@code index != 0}
+     */
     @Nonnull
     @Override
     public <T> T getValue(Type<T> type, int index) {
-        if (type.isArray()) {
-            throw new IllegalArgumentException("Nested arrows not supported");
-        }
+        checkArgument(!type.isArray(), "Type must not be an array type");
         if (index != 0) {
             throw new IndexOutOfBoundsException(String.valueOf(index));
         }
         return getValue(type);
     }
 
+    /**
+     * @return  {@code getString().length()}
+     */
     @Override
     public long size() {
         return getString().length();
     }
 
+    /**
+     * @return  {@code size}
+     * @throws IndexOutOfBoundsException  if {@code index != 0}
+     */
     @Override
     public long size(int index) {
         if (index != 0) {
@@ -138,6 +206,9 @@ abstract class SinglePropertyState exten
         return size();
     }
 
+    /**
+     * @return {@code 1}
+     */
     @Override
     public int count() {
         return 1;

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringPropertyState.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringPropertyState.java?rev=1397546&r1=1397545&r2=1397546&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringPropertyState.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/memory/StringPropertyState.java Fri Oct 12 12:52:40 2012
@@ -30,6 +30,12 @@ public class StringPropertyState extends
         this.value = value;
     }
 
+    /**
+     * Utility method defining the conversion from {@code String}
+     * to {@code boolean}.
+     * @param value  The string to convert to a boolean
+     * @return  The boolean value parsed from {@code value}
+     */
     public static boolean getBoolean(String value) {
         return Boolean.parseBoolean(value);
     }