Author: desruisseaux
Date: Sun Dec  2 07:21:38 2012
New Revision: 1416124

URL: http://svn.apache.org/viewvc?rev=1416124&view=rev
Log:
Added a central place where to keep trace of objects having UUID.

Added:
    
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/UUIDs.java
   (with props)
Modified:
    
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/WeakValueHashMap.java

Added: 
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/UUIDs.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/UUIDs.java?rev=1416124&view=auto
==============================================================================
--- 
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/UUIDs.java
 (added)
+++ 
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/UUIDs.java
 Sun Dec  2 07:21:38 2012
@@ -0,0 +1,73 @@
+/*
+ * 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.jaxb;
+
+import java.util.UUID;
+import org.apache.sis.util.Static;
+import org.apache.sis.util.collection.WeakValueHashMap;
+
+
+/**
+ * Weak references to objects associated to a UUID in the current JVM.
+ * The objects are typically instances of ISO 19115 (metadata) and ISO 19111 
(referencing) types.
+ * This class is convenient at XML marshalling and unmarshalling time for 
handling the {@code uuid}
+ * and {@code uuidref} attributes. The {@code uuidref} attribute is used to 
refer to an XML element
+ * that has a corresponding {@code uuid} attribute.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.13)
+ * @version 0.3
+ * @module
+ *
+ * @see <a 
href="https://www.seegrid.csiro.au/wiki/bin/view/AppSchemas/GmlIdentifiers";>GML 
identifiers</a>
+ */
+public final class UUIDs extends Static {
+    /**
+     * The objects for which a UUID has been created.
+     */
+    private static final WeakValueHashMap<UUID,Object> OBJECTS = new 
WeakValueHashMap<>(UUID.class);
+
+    /**
+     * Do not allow instantiation of this class.
+     */
+    private UUIDs() {
+    }
+
+    /**
+     * Returns the object associated to the given UUID, or {@code null} if 
none.
+     *
+     * @param  uuid The UUID for which to look for an object (can be {@code 
null}).
+     * @return The object associated to the given UUID, or {@code null} if 
none.
+     */
+    public static Object lookup(final UUID uuid) {
+        return OBJECTS.get(uuid);
+    }
+
+    /**
+     * Keep a weak references to the given object for the given UUID.
+     * If an object is already mapped to the given UUID, then the mapping is 
<strong>not</strong>
+     * modified and the currently mapped object is returned. The returned 
object may or may not be
+     * equals to the object given in argument to this method.
+     *
+     * @param  uuid   The UUID to associate to the object.
+     * @param  object The object to associate to the UUID.
+     * @return If an object is already mapped to the given UUID, that object. 
Otherwise {@code null}.
+     */
+    public static Object store(final UUID uuid, final Object object) {
+        return OBJECTS.putIfAbsent(uuid, object);
+    }
+}

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

Propchange: 
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/UUIDs.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/WeakValueHashMap.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/WeakValueHashMap.java?rev=1416124&r1=1416123&r2=1416124&view=diff
==============================================================================
--- 
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/WeakValueHashMap.java
 (original)
+++ 
sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/collection/WeakValueHashMap.java
 Sun Dec  2 07:21:38 2012
@@ -339,13 +339,16 @@ public class WeakValueHashMap<K,V> exten
     }
 
     /**
-     * Implementation of {@link #put(Object, Object)} and {@link 
#remove(Object)} operations.
+     * Implementation of {@link #put(Object, Object)} and {@link 
#remove(Object)} operations
+     *
+     * @param putIfAbsent If {@code true} and a value is found for the given 
key,
+     *        returns the old value without modifying the map.
      */
     @SuppressWarnings("unchecked")
-    private synchronized V intern(final Object key, final V value) {
+    private synchronized V intern(final Object key, final V value, final 
boolean putIfAbsent) {
         assert isValid();
         /*
-         * If 'obj' is already contained in this WeakValueHashMap, we need to 
clear it.
+         * If 'value' is already contained in this WeakValueHashMap, we need 
to clear it.
          */
         V oldValue = null;
         Entry[] table = this.table;
@@ -354,6 +357,9 @@ public class WeakValueHashMap<K,V> exten
         for (Entry e = table[index]; e != null; e = (Entry) e.next) {
             if (keyEquals(key, e.key)) {
                 oldValue = e.get();
+                if (putIfAbsent) {
+                    return oldValue;
+                }
                 e.dispose();
                 table = this.table; // May have changed.
                 index = hash % table.length;
@@ -379,7 +385,7 @@ public class WeakValueHashMap<K,V> exten
      *
      * @param  key key with which the specified value is to be associated.
      * @param  value value to be associated with the specified key.
-     * @return previous value associated with specified key, or {@code null}
+     * @return The previous value associated with specified key, or {@code 
null}
      *         if there was no mapping for key.
      *
      * @throws NullArgumentException if the key or the value is {@code null}.
@@ -388,7 +394,25 @@ public class WeakValueHashMap<K,V> exten
     public V put(final K key, final V value) throws NullArgumentException {
         ArgumentChecks.ensureNonNull("key",   key);
         ArgumentChecks.ensureNonNull("value", value);
-        return intern(key, value);
+        return intern(key, value, false);
+    }
+
+    /**
+     * Associates the specified value with the specified key only if no value 
is currently
+     * associated to that key. If a value already exists for the given key, 
then this method
+     * returns the current value without changing the map.
+     *
+     * @param  key key with which the specified value is to be associated.
+     * @param  value value to be associated with the specified key.
+     * @return {@code null} if the given value has been associated to the 
given key,
+     *         or the current (unchanged) value if a mapping already exists 
for that key.
+     *
+     * @throws NullArgumentException if the key or the value is {@code null}.
+     */
+    public V putIfAbsent(final K key, final V value) throws 
NullArgumentException {
+        ArgumentChecks.ensureNonNull("key",   key);
+        ArgumentChecks.ensureNonNull("value", value);
+        return intern(key, value, true);
     }
 
     /**
@@ -400,7 +424,7 @@ public class WeakValueHashMap<K,V> exten
      */
     @Override
     public V remove(final Object key) {
-        return intern(key, null);
+        return intern(key, null, false);
     }
 
     /**


Reply via email to