You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2015/01/15 19:47:50 UTC

svn commit: r1652219 - in /sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper: BooleanConverter.java CalendarConverter.java Converter.java DateConverter.java NumberConverter.java StringConverter.java

Author: cziegeler
Date: Thu Jan 15 18:47:50 2015
New Revision: 1652219

URL: http://svn.apache.org/r1652219
Log:
SLING-4307 : Avoid caching JCR property values

Added:
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/BooleanConverter.java   (with props)
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/CalendarConverter.java   (with props)
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Converter.java   (with props)
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/DateConverter.java   (with props)
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/NumberConverter.java   (with props)
    sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/StringConverter.java   (with props)

Added: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/BooleanConverter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/BooleanConverter.java?rev=1652219&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/BooleanConverter.java (added)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/BooleanConverter.java Thu Jan 15 18:47:50 2015
@@ -0,0 +1,119 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal.helper;
+
+import java.math.BigDecimal;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * A converter for Boolean
+ */
+public class BooleanConverter implements Converter {
+
+    private final Boolean value;
+
+    public BooleanConverter(final Boolean val) {
+        this.value = val;
+    }
+
+    /**
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        return this.value.toString();
+    }
+
+    private Integer getNumber() {
+        return ( value.booleanValue() ? 1 : 0);
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toLong()
+     */
+    public Long toLong() {
+        return this.getNumber().longValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toByte()
+     */
+    public Byte toByte() {
+        return this.getNumber().byteValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toShort()
+     */
+    public Short toShort() {
+        return this.getNumber().shortValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toInteger()
+     */
+    public Integer toInteger() {
+        return this.getNumber().intValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toDouble()
+     */
+    public Double toDouble() {
+        return this.getNumber().doubleValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toFloat()
+     */
+    public Float toFloat() {
+        return this.getNumber().floatValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toCalendar()
+     */
+    public Calendar toCalendar() {
+        final Calendar c = Calendar.getInstance();
+        c.setTimeInMillis(this.toLong());
+        return c;
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toDate()
+     */
+    public Date toDate() {
+        return new Date(this.toLong());
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toBoolean()
+     */
+    public Boolean toBoolean() {
+        return this.value;
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toBigDecimal()
+     */
+    public BigDecimal toBigDecimal() {
+        return new BigDecimal(this.getNumber().toString());
+    }
+}

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/BooleanConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/BooleanConverter.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/CalendarConverter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/CalendarConverter.java?rev=1652219&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/CalendarConverter.java (added)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/CalendarConverter.java Thu Jan 15 18:47:50 2015
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal.helper;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.jackrabbit.util.ISO8601;
+
+/**
+ * A converter for Calendar
+ */
+public class CalendarConverter extends NumberConverter implements Converter {
+
+    private final Calendar value;
+
+    public CalendarConverter(final Calendar val) {
+        super(val.getTimeInMillis());
+        this.value = val;
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toCalendar()
+     */
+    @Override
+    public Calendar toCalendar() {
+        return this.value;
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toDate()
+     */
+    @Override
+    public Date toDate() {
+        return this.value.getTime();
+    }
+
+    @Override
+    public String toString() {
+        return ISO8601.format(this.value);
+    }
+}

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/CalendarConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/CalendarConverter.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Converter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Converter.java?rev=1652219&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Converter.java (added)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Converter.java Thu Jan 15 18:47:50 2015
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal.helper;
+
+
+import java.math.BigDecimal;
+import java.util.Calendar;
+import java.util.Date;
+
+
+/**
+ * A converter converts a value to a specific target type.
+ */
+public interface Converter {
+
+    /**
+     * Convert to Long.
+     * @return Long representation of the converted value
+     * @throws NumberFormatException
+     */
+    Long toLong();
+
+    /**
+     * Convert to Byte.
+     * @return Byte representation of the converted value
+     * @throws NumberFormatException
+     */
+    Byte toByte();
+
+    /**
+     * Convert to Short.
+     * @return Short representation of the converted value
+     * @throws NumberFormatException
+     */
+    Short toShort();
+
+    /**
+     * Convert to Integer.
+     * @return Integer representation of the converted value
+     * @throws NumberFormatException
+     */
+    Integer toInteger();
+
+    /**
+     * Convert to Double.
+     * @return Double representation of the converted value
+     * @throws NumberFormatException
+     */
+    Double toDouble();
+
+    /**
+     * Convert to Float.
+     * @return Float representation of the converted value
+     * @throws NumberFormatException
+     */
+    Float toFloat();
+
+    /**
+     * Convert to Calendar.
+     * @return Calendar representation of the converted value
+     * @throws IllegalArgumentException  if the value cannot be parsed into a calendar
+     */
+    Calendar toCalendar();
+
+    /**
+     * Convert to Date.
+     * @return Date representation of the converted value
+     * @throws IllegalArgumentException  if the value cannot be parsed into a date
+     */
+    Date toDate();
+
+    /**
+     * Convert to boolean.
+     * @return  Boolean representation of the converted value
+     */
+    Boolean toBoolean();
+
+    /**
+     * Convert to BigDecimal.
+     * @return BigDecimal representation of the converted value
+     * @throws NumberFormatException
+     */
+    BigDecimal toBigDecimal();
+
+}

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Converter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Converter.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/DateConverter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/DateConverter.java?rev=1652219&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/DateConverter.java (added)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/DateConverter.java Thu Jan 15 18:47:50 2015
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal.helper;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.jackrabbit.util.ISO8601;
+
+/**
+ * A converter for Date
+ */
+public class DateConverter extends NumberConverter implements Converter {
+
+    private final Date value;
+
+    public DateConverter(final Date val) {
+        super(val.getTime());
+        this.value = val;
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toCalendar()
+     */
+    @Override
+    public Calendar toCalendar() {
+        final Calendar c = Calendar.getInstance();
+        c.setTimeInMillis(this.toLong());
+        return c;
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toDate()
+     */
+    @Override
+    public Date toDate() {
+        return this.value;
+    }
+
+    @Override
+    public String toString() {
+        return ISO8601.format(this.toCalendar());
+    }
+}

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/DateConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/DateConverter.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/NumberConverter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/NumberConverter.java?rev=1652219&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/NumberConverter.java (added)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/NumberConverter.java Thu Jan 15 18:47:50 2015
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal.helper;
+
+import java.math.BigDecimal;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * A converter for Number
+ */
+public class NumberConverter implements Converter {
+
+    private final Number value;
+
+    public NumberConverter(final Number val) {
+        this.value = val;
+    }
+
+    /**
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        return this.value.toString();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toLong()
+     */
+    public Long toLong() {
+        return this.value.longValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toByte()
+     */
+    public Byte toByte() {
+        return this.value.byteValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toShort()
+     */
+    public Short toShort() {
+        return this.value.shortValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toInteger()
+     */
+    public Integer toInteger() {
+        return this.value.intValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toDouble()
+     */
+    public Double toDouble() {
+        return this.value.doubleValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toFloat()
+     */
+    public Float toFloat() {
+        return this.value.floatValue();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toCalendar()
+     */
+    public Calendar toCalendar() {
+        final Calendar c = Calendar.getInstance();
+        c.setTimeInMillis(this.toLong());
+        return c;
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toDate()
+     */
+    public Date toDate() {
+        return new Date(this.toLong());
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toBoolean()
+     */
+    public Boolean toBoolean() {
+        return false;
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toBigDecimal()
+     */
+    public BigDecimal toBigDecimal() {
+        if ( this.value instanceof BigDecimal ) {
+            return (BigDecimal)this.value;
+        }
+        return new BigDecimal(this.value.toString());
+    }
+}

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/NumberConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/NumberConverter.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Added: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/StringConverter.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/StringConverter.java?rev=1652219&view=auto
==============================================================================
--- sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/StringConverter.java (added)
+++ sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/StringConverter.java Thu Jan 15 18:47:50 2015
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jcr.resource.internal.helper;
+
+import java.math.BigDecimal;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.jackrabbit.util.ISO8601;
+
+/**
+ * A converter for any object based on toString()
+ */
+public class StringConverter implements Converter {
+
+    private final Object value;
+
+    public StringConverter(final Object val) {
+        this.value = val;
+    }
+
+    /**
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString() {
+        return this.value.toString();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toLong()
+     */
+    public Long toLong() {
+        return Long.parseLong(this.toString());
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toByte()
+     */
+    public Byte toByte() {
+        return Byte.parseByte(this.toString());
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toShort()
+     */
+    public Short toShort() {
+        return Short.parseShort(this.toString());
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toInteger()
+     */
+    public Integer toInteger() {
+        return Integer.parseInt(this.toString());
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toDouble()
+     */
+    public Double toDouble() {
+        return Double.parseDouble(this.toString());
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toFloat()
+     */
+    public Float toFloat() {
+        return Float.parseFloat(this.toString());
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toCalendar()
+     */
+    public Calendar toCalendar() {
+        final Calendar c = ISO8601.parse(toString());
+        if (c == null) {
+            throw new IllegalArgumentException("Not a date string: " + toString());
+        }
+        return c;
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toDate()
+     */
+    public Date toDate() {
+        final Calendar c = this.toCalendar();
+        return c.getTime();
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toBoolean()
+     */
+    public Boolean toBoolean() {
+        return Boolean.valueOf(this.toString());
+    }
+
+    /**
+     * @see org.apache.sling.jcr.resource.internal.helper.Converter#toBigDecimal()
+     */
+    public BigDecimal toBigDecimal() {
+        return new BigDecimal(this.toString());
+    }
+}

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/StringConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/StringConverter.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url