jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1476167 - in /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak: api/Type.java plugins/nodetype/EffectiveType.java plugins/nodetype/PropertyDefinitionImpl.java
Date Fri, 26 Apr 2013 12:22:54 GMT
Author: jukka
Date: Fri Apr 26 12:22:53 2013
New Revision: 1476167

URL: http://svn.apache.org/r1476167
Log:
OAK-66: JCR Node Type Management

Unify handling of property type names by making the Type<?> constants carry also the
all-uppercase versions of the type names used in nt:propertyDefinition

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Type.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/EffectiveType.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionImpl.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Type.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Type.java?rev=1476167&r1=1476166&r2=1476167&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Type.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/api/Type.java Fri
Apr 26 12:22:53 2013
@@ -19,6 +19,7 @@
 package org.apache.jackrabbit.oak.api;
 
 import java.math.BigDecimal;
+import java.util.Map;
 
 import javax.jcr.PropertyType;
 
@@ -26,6 +27,7 @@ import com.google.common.base.Objects;
 import com.google.common.collect.ComparisonChain;
 
 import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.collect.Maps.newHashMap;
 
 /**
  * Instances of this class map Java types to {@link PropertyType property types}.
@@ -35,88 +37,128 @@ import static com.google.common.base.Pre
  */
 public final class Type<T> implements Comparable<Type<?>> {
 
+    private static final Map<String, Type<?>> TYPES = newHashMap();
+
+    private static <T> Type<T> create(int tag, boolean array, String string)
{
+        Type<T> type = new Type<T>(tag, array, string);
+        TYPES.put(string, type);
+        return type;
+    }
+
     /** Map {@code String} to {@link PropertyType#STRING} */
-    public static final Type<String> STRING = create(PropertyType.STRING, false);
+    public static final Type<String> STRING =
+            create(PropertyType.STRING, false, "STRING");
 
     /** Map {@code Blob} to {@link PropertyType#BINARY} */
-    public static final Type<Blob> BINARY = create(PropertyType.BINARY, false);
+    public static final Type<Blob> BINARY =
+            create(PropertyType.BINARY, false, "BINARY");
 
     /** Map {@code Long} to {@link PropertyType#LONG} */
-    public static final Type<Long> LONG = create(PropertyType.LONG, false);
+    public static final Type<Long> LONG =
+            create(PropertyType.LONG, false, "LONG");
 
     /** Map {@code Double} to {@link PropertyType#DOUBLE} */
-    public static final Type<Double> DOUBLE = create(PropertyType.DOUBLE, false);
+    public static final Type<Double> DOUBLE =
+            create(PropertyType.DOUBLE, false, "DOUBLE");
 
     /** Map {@code Long} to {@link PropertyType#DATE} */
-    public static final Type<Long> DATE = create(PropertyType.DATE, false);
+    public static final Type<Long> DATE =
+            create(PropertyType.DATE, false, "DATE");
 
     /** Map {@code Boolean} to {@link PropertyType#BOOLEAN} */
-    public static final Type<Boolean> BOOLEAN = create(PropertyType.BOOLEAN, false);
+    public static final Type<Boolean> BOOLEAN =
+            create(PropertyType.BOOLEAN, false, "BOOLEAN");
 
     /** Map {@code String} to {@link PropertyType#STRING} */
-    public static final Type<String> NAME = create(PropertyType.NAME, false);
+    public static final Type<String> NAME =
+            create(PropertyType.NAME, false, "NAME");
 
     /** Map {@code String} to {@link PropertyType#PATH} */
-    public static final Type<String> PATH = create(PropertyType.PATH, false);
+    public static final Type<String> PATH =
+            create(PropertyType.PATH, false, "PATH");
 
     /** Map {@code String} to {@link PropertyType#REFERENCE} */
-    public static final Type<String> REFERENCE = create(PropertyType.REFERENCE, false);
+    public static final Type<String> REFERENCE =
+            create(PropertyType.REFERENCE, false, "REFERENCE");
 
     /** Map {@code String} to {@link PropertyType#WEAKREFERENCE} */
-    public static final Type<String> WEAKREFERENCE = create(PropertyType.WEAKREFERENCE,
false);
+    public static final Type<String> WEAKREFERENCE =
+            create(PropertyType.WEAKREFERENCE, false, "WEAKREFERENCE");
 
     /** Map {@code String} to {@link PropertyType#URI} */
-    public static final Type<String> URI = create(PropertyType.URI, false);
+    public static final Type<String> URI =
+            create(PropertyType.URI, false, "URI");
 
     /** Map {@code BigDecimal} to {@link PropertyType#DECIMAL} */
-    public static final Type<BigDecimal> DECIMAL = create(PropertyType.DECIMAL, false);
+    public static final Type<BigDecimal> DECIMAL =
+            create(PropertyType.DECIMAL, false, "DECIMAL");
 
     /** Map {@code Iterable<String>} to array of {@link PropertyType#STRING} */
-    public static final Type<Iterable<String>> STRINGS = create(PropertyType.STRING,
true);
+    public static final Type<Iterable<String>> STRINGS =
+            create(PropertyType.STRING, true, "STRINGS");
 
     /** Map {@code Iterable<Blob>} to array of {@link PropertyType#BINARY} */
-    public static final Type<Iterable<Blob>> BINARIES = create(PropertyType.BINARY,
true);
+    public static final Type<Iterable<Blob>> BINARIES =
+            create(PropertyType.BINARY, true, "BINARIES");
 
     /** Map {@code Iterable<Long>} to array of {@link PropertyType#LONG} */
-    public static final Type<Iterable<Long>> LONGS = create(PropertyType.LONG,
true);
+    public static final Type<Iterable<Long>> LONGS =
+            create(PropertyType.LONG, true, "LONGS");
 
     /** Map {@code Iterable<Double>} to array of {@link PropertyType#DOUBLE} */
-    public static final Type<Iterable<Double>> DOUBLES = create(PropertyType.DOUBLE,
true);
+    public static final Type<Iterable<Double>> DOUBLES =
+            create(PropertyType.DOUBLE, true, "DOUBLES");
 
     /** Map {@code Iterable<Long>} to array of {@link PropertyType#DATE} */
-    public static final Type<Iterable<Long>> DATES = create(PropertyType.DATE,
true);
+    public static final Type<Iterable<Long>> DATES =
+            create(PropertyType.DATE, true, "DATES");
 
     /** Map {@code Iterable<Boolean>} to array of {@link PropertyType#BOOLEAN} */
-    public static final Type<Iterable<Boolean>> BOOLEANS = create(PropertyType.BOOLEAN,
true);
+    public static final Type<Iterable<Boolean>> BOOLEANS =
+            create(PropertyType.BOOLEAN, true, "BOOLEANS");
 
     /** Map {@code Iterable<String>} to array of {@link PropertyType#NAME} */
-    public static final Type<Iterable<String>> NAMES = create(PropertyType.NAME,
true);
+    public static final Type<Iterable<String>> NAMES =
+            create(PropertyType.NAME, true, "NAMES");
 
     /** Map {@code Iterable<String>} to array of {@link PropertyType#PATH} */
-    public static final Type<Iterable<String>> PATHS = create(PropertyType.PATH,
true);
+    public static final Type<Iterable<String>> PATHS =
+            create(PropertyType.PATH, true, "PATHS");
 
     /** Map {@code Iterable<String>} to array of {@link PropertyType#REFERENCE} */
-    public static final Type<Iterable<String>> REFERENCES = create(PropertyType.REFERENCE,
true);
+    public static final Type<Iterable<String>> REFERENCES =
+            create(PropertyType.REFERENCE, true, "REFERENCES");
 
     /** Map {@code Iterable<String>} to array of {@link PropertyType#WEAKREFERENCE}
*/
-    public static final Type<Iterable<String>> WEAKREFERENCES = create(PropertyType.WEAKREFERENCE,
true);
+    public static final Type<Iterable<String>> WEAKREFERENCES =
+            create(PropertyType.WEAKREFERENCE, true, "WEAKREFERENCES");
 
     /** Map {@code Iterable<String>} to array of {@link PropertyType#URI} */
-    public static final Type<Iterable<String>> URIS = create(PropertyType.URI,
true);
+    public static final Type<Iterable<String>> URIS =
+            create(PropertyType.URI, true, "URIS");
 
     /** Map {@code Iterable<BigDecimal>} to array of {@link PropertyType#DECIMAL} */
-    public static final Type<Iterable<BigDecimal>> DECIMALS = create(PropertyType.DECIMAL,
true);
+    public static final Type<Iterable<BigDecimal>> DECIMALS =
+            create(PropertyType.DECIMAL, true, "DECIMALS");
+
+    /** The special "undefined" type, never encountered in normal values */
+    public static final Type<Void> UNDEFINED =
+            create(PropertyType.UNDEFINED, false, "UNDEFINED");
+
+    /** Multi-valued "undefined" type, never encountered in normal values */
+    public static final Type<Iterable<Void>> UNDEFINEDS =
+            create(PropertyType.UNDEFINED, true, "UNDEFINEDS");
 
     private final int tag;
+
     private final boolean array;
 
-    private Type(int tag, boolean array){
+    private final String string;
+
+    private Type(int tag, boolean array, String string) {
         this.tag = tag;
         this.array = array;
-    }
-
-    private static <T> Type<T> create(int tag, boolean array) {
-        return new Type<T>(tag, array);
+        this.string = string;
     }
 
     /**
@@ -156,11 +198,26 @@ public final class Type<T> implements Co
             case PropertyType.WEAKREFERENCE: return array ? WEAKREFERENCES : WEAKREFERENCE;
             case PropertyType.URI: return array ? URIS: URI;
             case PropertyType.DECIMAL: return array ? DECIMALS : DECIMAL;
+            case PropertyType.UNDEFINED: return array ? UNDEFINEDS : UNDEFINED;
             default: throw new IllegalArgumentException("Invalid type tag: " + tag);
         }
     }
 
     /**
+     * Returns the {@code Type} with the given string representation.
+     *
+     * @param string type string
+     * @return matching type
+     */
+    public static Type<?> fromString(String string) {
+        Type<?> type = TYPES.get(string);
+        if (type == null) {
+            throw new IllegalArgumentException("Invalid type name: " + string);
+        }
+        return type;
+    }
+
+    /**
      * Determine the base type of array types
      * @return  base type
      * @throws IllegalStateException if {@code isArray} is false.
@@ -194,9 +251,7 @@ public final class Type<T> implements Co
 
     @Override
     public String toString() {
-        return isArray()
-            ? "[]" + PropertyType.nameFromValue(getBaseType().tag)
-            : PropertyType.nameFromValue(tag);
+        return string;
     }
 
     @Override

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/EffectiveType.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/EffectiveType.java?rev=1476167&r1=1476166&r2=1476167&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/EffectiveType.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/EffectiveType.java
Fri Apr 26 12:22:53 2013
@@ -24,6 +24,8 @@ import static org.apache.jackrabbit.JcrC
 import static org.apache.jackrabbit.JcrConstants.JCR_UUID;
 import static org.apache.jackrabbit.oak.api.Type.NAME;
 import static org.apache.jackrabbit.oak.api.Type.NAMES;
+import static org.apache.jackrabbit.oak.api.Type.UNDEFINED;
+import static org.apache.jackrabbit.oak.api.Type.UNDEFINEDS;
 
 import java.util.List;
 import java.util.Set;
@@ -93,12 +95,12 @@ class EffectiveType {
             escapedName = propertyName;
         }
 
-        String definedType = getTypeKey(propertyType);
+        String definedType = propertyType.toString();
         String undefinedType;
         if (propertyType.isArray()) {
-            undefinedType = "UNDEFINEDS";
+            undefinedType = UNDEFINEDS.toString();
         } else {
-            undefinedType = "UNDEFINED";
+            undefinedType = UNDEFINED.toString();
         }
 
         // Find matching named property definition
@@ -200,60 +202,4 @@ class EffectiveType {
         return names;
     }
 
-    //-----------------------------------------------------------< private >--
-
-    private String getTypeKey(Type<?> type) {
-        if (type == Type.BINARIES) {
-            return "BINARIES";
-        } else if (type == Type.BINARY) {
-            return "BINARY";
-        } else if (type == Type.BOOLEAN) {
-            return "BOOLEAN";
-        } else if (type == Type.BOOLEANS) {
-            return "BOOLEANS";
-        } else if (type == Type.DATE) {
-            return "DATE";
-        } else if (type == Type.DATES) {
-            return "DATES";
-        } else if (type == Type.DECIMAL) {
-            return "DECIMAL";
-        } else if (type == Type.DECIMALS) {
-            return "DECIMALS";
-        } else if (type == Type.DOUBLE) {
-            return "DOUBLE";
-        } else if (type == Type.DOUBLES) {
-            return "DOUBLES";
-        } else if (type == Type.LONG) {
-            return "LONG";
-        } else if (type == Type.LONGS) {
-            return "LONGS";
-        } else if (type == Type.NAME) {
-            return "NAME";
-        } else if (type == Type.NAMES) {
-            return "NAMES";
-        } else if (type == Type.PATH) {
-            return "PATH";
-        } else if (type == Type.PATHS) {
-            return "PATHS";
-        } else if (type == Type.REFERENCE) {
-            return "REFERENCE";
-        } else if (type == Type.REFERENCES) {
-            return "REFERENCES";
-        } else if (type == Type.STRING) {
-            return "STRING";
-        } else if (type == Type.STRINGS) {
-            return "STRINGS";
-        } else if (type == Type.URI) {
-            return "URI";
-        } else if (type == Type.URIS) {
-            return "URIS";
-        } else if (type == Type.WEAKREFERENCE) {
-            return "WEAKREFERENCE";
-        } else if (type == Type.WEAKREFERENCES) {
-            return "WEAKREFERENCES";
-        } else {
-            return "unknown";
-        }
-    }
-
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionImpl.java?rev=1476167&r1=1476166&r2=1476167&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionImpl.java
Fri Apr 26 12:22:53 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
+import static javax.jcr.PropertyType.UNDEFINED;
+
 import java.util.List;
 
 import javax.jcr.Value;
@@ -26,23 +28,10 @@ import javax.jcr.query.qom.QueryObjectMo
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.namepath.NamePathMapper;
 import org.apache.jackrabbit.oak.plugins.value.ValueFactoryImpl;
 
-import static javax.jcr.PropertyType.BINARY;
-import static javax.jcr.PropertyType.BOOLEAN;
-import static javax.jcr.PropertyType.DATE;
-import static javax.jcr.PropertyType.DECIMAL;
-import static javax.jcr.PropertyType.DOUBLE;
-import static javax.jcr.PropertyType.LONG;
-import static javax.jcr.PropertyType.NAME;
-import static javax.jcr.PropertyType.PATH;
-import static javax.jcr.PropertyType.REFERENCE;
-import static javax.jcr.PropertyType.STRING;
-import static javax.jcr.PropertyType.UNDEFINED;
-import static javax.jcr.PropertyType.URI;
-import static javax.jcr.PropertyType.WEAKREFERENCE;
-
 /**
  * <pre>
  * [nt:propertyDefinition]
@@ -79,35 +68,11 @@ class PropertyDefinitionImpl extends Ite
      * @throws IllegalStateException if {@code name} is not a valid property type name.
      */
     public static int valueFromName(String name) {
-        if ("STRING".equals(name)) {
-            return STRING;
-        } else if ("BINARY".equals(name)) {
-            return BINARY;
-        } else if ("BOOLEAN".equals(name)) {
-            return BOOLEAN;
-        } else if ("LONG".equals(name)) {
-            return LONG;
-        } else if ("DOUBLE".equals(name)) {
-            return DOUBLE;
-        } else if ("DECIMAL".equals(name)) {
-            return DECIMAL;
-        } else if ("DATE".equals(name)) {
-            return DATE;
-        } else if ("NAME".equals(name)) {
-            return NAME;
-        } else if ("PATH".equals(name)) {
-            return PATH;
-        } else if ("REFERENCE".equals(name)) {
-            return REFERENCE;
-        } else if ("WEAKREFERENCE".equals(name)) {
-            return WEAKREFERENCE;
-        } else if ("URI".equals(name)) {
-            return URI;
-        } else if ("UNDEFINED".equals(name)) {
-            return UNDEFINED;
-        } else {
+        Type<?> type = Type.fromString(name);
+        if (type.isArray()) {
             throw new IllegalStateException("unknown property type: " + name);
         }
+        return type.tag();
     }
 
     //-------------------------------------------------< PropertyDefinition >---



Mime
View raw message