sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1454602 - in /sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis: internal/converter/CharSequenceConverter.java internal/converter/IdentityConverter.java util/IdentityConverter.java util/ObjectConverters.java
Date Fri, 08 Mar 2013 22:15:04 GMT
Author: desruisseaux
Date: Fri Mar  8 22:15:03 2013
New Revision: 1454602

URL: http://svn.apache.org/r1454602
Log:
Added CharSequenceConverter (internal), to be needed for the support of InternationalString.

Added:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/CharSequenceConverter.java
  (with props)
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/IdentityConverter.java
      - copied, changed from r1454596, sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/IdentityConverter.java
Removed:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/IdentityConverter.java
Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java

Added: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/CharSequenceConverter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/CharSequenceConverter.java?rev=1454602&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/CharSequenceConverter.java
(added)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/CharSequenceConverter.java
[UTF-8] Fri Mar  8 22:15:03 2013
@@ -0,0 +1,117 @@
+/*
+ * 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.sis.internal.converter;
+
+import java.io.Serializable;
+import net.jcip.annotations.Immutable;
+import org.apache.sis.util.ObjectConverter;
+import org.apache.sis.util.UnconvertibleObjectException;
+
+
+/**
+ * Handles conversions from {@link CharSequence} to {@link String}, then forward
+ * to an other converter from {@link String} to various objects. Instance of this
+ * converter are not registered in {@link ConverterRegistry} like other converters
+ * because we avoid registering converter expecting interfaces as their source.
+ *
+ * <p>The main purpose of this class is to support the conversion of
+ * {@link org.opengis.util.InternationalString}.</p>
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.02)
+ * @version 0.3
+ * @module
+ */
+@Immutable
+final class CharSequenceConverter<T> extends SurjectiveConverter<CharSequence,T>
implements Serializable {
+    /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = 2591675151163578878L;
+
+    /**
+     * A converter from {@link CharSequence} to {@link String}.
+     */
+    static final CharSequenceConverter<String> STRING =
+            new CharSequenceConverter<>(String.class, IdentityConverter.create(String.class));
+
+    /**
+     * The target type requested by the user. We retain this type explicitly instead
+     * than querying {@code next.getTargetType()} because it may be a super-class of
+     * the later.
+     */
+    private final Class<T> targetType;
+
+    /**
+     * The converter to apply after this one.
+     */
+    private final ObjectConverter<? super String, ? extends T> next;
+
+    /**
+     * Creates a new converter from {@link CharSequence} to the given target type.
+     *
+     * @param targetType The target type requested by the user.
+     * @param next The converter to apply after this one.
+     */
+    private CharSequenceConverter(final Class<T> targetType, final ObjectConverter<?
super String, ? extends T> next) {
+        this.targetType = targetType;
+        this.next = next;
+    }
+
+    /**
+     * Creates a new converter from {@link CharSequence} to the given target type.
+     *
+     * @param targetType The target type requested by the user.
+     * @param next The converter to apply after this one.
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> ObjectConverter<? super CharSequence, ? extends T> create(
+            final Class<T> targetType, final ObjectConverter<? super String, ? extends
T> next)
+    {
+        if (next.getSourceClass().isAssignableFrom(CharSequence.class)) {
+            return (ObjectConverter<? super CharSequence, ? extends T>) next;
+        }
+        return new CharSequenceConverter<>(targetType, next);
+    }
+
+    /**
+     * Returns the source class, which is always {@link CharSequence}.
+     */
+    @Override
+    public final Class<CharSequence> getSourceClass() {
+        return CharSequence.class;
+    }
+
+    /**
+     * Returns the target class.
+     */
+    @Override
+    public final Class<T> getTargetClass() {
+        return targetType;
+    }
+
+    /**
+     * Converts an object to an object of the target type.
+     */
+    @Override
+    public T convert(final CharSequence source) throws UnconvertibleObjectException {
+        if (targetType.isInstance(source)) {
+            return targetType.cast(source);
+        }
+        return next.convert(source != null ? source.toString() : null);
+    }
+}

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/CharSequenceConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/CharSequenceConverter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Copied: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/IdentityConverter.java
(from r1454596, sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/IdentityConverter.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/IdentityConverter.java?p2=sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/IdentityConverter.java&p1=sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/IdentityConverter.java&r1=1454596&r2=1454602&rev=1454602&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/IdentityConverter.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/converter/IdentityConverter.java
[UTF-8] Fri Mar  8 22:15:03 2013
@@ -14,16 +14,18 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sis.util;
+package org.apache.sis.internal.converter;
 
 import java.util.Set;
 import java.util.EnumSet;
 import java.util.Map;
-import java.util.WeakHashMap;
+import java.util.IdentityHashMap;
 import java.io.ObjectStreamException;
 import java.io.Serializable;
 import net.jcip.annotations.Immutable;
+import org.apache.sis.internal.util.SystemListener;
 import org.apache.sis.math.FunctionProperty;
+import org.apache.sis.util.ObjectConverter;
 
 
 /**
@@ -37,7 +39,7 @@ import org.apache.sis.math.FunctionPrope
  * @module
  */
 @Immutable
-final class IdentityConverter<T> implements ObjectConverter<T,T>, Serializable
{
+public final class IdentityConverter<T> implements ObjectConverter<T,T>, Serializable
{
     /**
      * For cross-version compatibility.
      */
@@ -45,21 +47,44 @@ final class IdentityConverter<T> impleme
 
     /**
      * Identity converters created in the JVM, for sharing unique instances.
-     * Use weak keys in order to allow class unloading.
      */
-    private static final Map<Class<?>, IdentityConverter<?>> CACHE = new
WeakHashMap<>();
+    private static final Map<Class<?>, IdentityConverter<?>> CACHE = new
IdentityHashMap<>();
+    static {
+        SystemListener.add(new SystemListener() {
+            @Override protected void classpathChanged() {
+                clearCache();
+            }
+        });
+    }
 
     /**
      * Returns an identity converter for the given type.
+     *
+     * @param  <T>  The compile-time type.
+     * @param  type The type of the desired converter.
+     * @return The identity converter for the given type.
      */
-    public static synchronized <T> IdentityConverter<T> create(final Class<T>
type) {
-        @SuppressWarnings("unchecked")
-        IdentityConverter<T> converter = (IdentityConverter<T>) CACHE.get(type);
-        if (converter == null) {
-            converter = new IdentityConverter<>(type);
-            CACHE.put(type, converter);
+    public static <T> IdentityConverter<T> create(final Class<T> type)
{
+        synchronized (CACHE) {
+            @SuppressWarnings("unchecked")
+            IdentityConverter<T> converter = (IdentityConverter<T>) CACHE.get(type);
+            if (converter == null) {
+                converter = new IdentityConverter<>(type);
+                CACHE.put(type, converter);
+            }
+            return converter;
+        }
+    }
+
+    /**
+     * Invoked when the cache needs to be cleared because the classpath changed.
+     * Some cached type may no longer be on the classpath, so we need to release
+     * references in order to allow the garbage collector to unload them.
+     */
+    static void clearCache() {
+        synchronized (CACHE) {
+            CACHE.clear();
         }
-        return converter;
     }
 
     /**
@@ -107,6 +132,8 @@ final class IdentityConverter<T> impleme
 
     /**
      * Returns the given object unchanged.
+     *
+     * @param source The value to convert.
      */
     @Override
     public T convert(final T source) {
@@ -125,7 +152,7 @@ final class IdentityConverter<T> impleme
      * Invoked on deserialization for resolving to a unique instance.
      */
     private Object readResolve() throws ObjectStreamException {
-        synchronized (IdentityConverter.class) {
+        synchronized (CACHE) {
             @SuppressWarnings("unchecked")
             final IdentityConverter<T> converter = (IdentityConverter<T>) CACHE.get(type);
             if (converter != null) {

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java?rev=1454602&r1=1454601&r2=1454602&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/ObjectConverters.java
[UTF-8] Fri Mar  8 22:15:03 2013
@@ -18,6 +18,7 @@ package org.apache.sis.util;
 
 import java.util.Map;
 import java.util.Set;
+import org.apache.sis.internal.converter.IdentityConverter;
 import org.apache.sis.util.collection.CollectionsExt;
 import org.apache.sis.util.resources.Errors;
 



Mime
View raw message