sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: Rename Freezer as StateChanger. For now used only for State.FINAL, but future versions may use it for more states.
Date Sat, 30 Jun 2018 16:09:34 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 1708685  Rename Freezer as StateChanger. For now used only for State.FINAL, but future
versions may use it for more states.
1708685 is described below

commit 17086855c54c5ddd04f4b2fa0e74f544661da2cb
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Sat Jun 30 18:08:57 2018 +0200

    Rename Freezer as StateChanger. For now used only for State.FINAL, but future versions
may use it for more states.
---
 .../org/apache/sis/metadata/MetadataStandard.java  | 13 -----
 .../apache/sis/metadata/ModifiableMetadata.java    |  5 +-
 .../metadata/{Freezer.java => StateChanger.java}   | 58 ++++++++++++++--------
 3 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
b/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
index b4d3c40..69ca60e 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
+++ b/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
@@ -917,19 +917,6 @@ public class MetadataStandard implements Serializable {
     }
 
     /**
-     * Replaces every properties in the specified metadata by their
-     * {@linkplain ModifiableMetadata#unmodifiable() unmodifiable variant}.
-     *
-     * @throws ClassCastException if the specified implementation class do
-     *         not implements a metadata interface of the expected package.
-     */
-    final void freeze(final Object metadata) throws ClassCastException {
-        if (metadata != null) {
-            Freezer.getOrCreate().walk(this, null, metadata, true);
-        }
-    }
-
-    /**
      * Compares the two specified metadata objects.
      * The two metadata arguments shall be implementations of a metadata interface defined
by
      * this {@code MetadataStandard}, otherwise an exception will be thrown. However the
two
diff --git a/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
b/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
index 56ce547..039c427 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
+++ b/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
@@ -252,11 +252,10 @@ public abstract class ModifiableMetadata extends AbstractMetadata {
             }
             case FINAL: {
                 if ((state & FINAL) != 0) break;
-                final MetadataStandard standard = getStandard();
                 byte result = state;
                 try {
                     state = FREEZING;
-                    standard.freeze(this);
+                    StateChanger.applyTo(target, this);
                     result = FINAL;
                 } finally {
                     state = result;
@@ -512,7 +511,7 @@ public abstract class ModifiableMetadata extends AbstractMetadata {
             if (state == FREEZING) {
                 /*
                  * apply(State.FINAL) is under progress. The source collection is already
-                 * an unmodifiable instance created by Freezer.clone(Object).
+                 * an unmodifiable instance created by StageChanger.
                  */
                 assert collectionType(elementType).isInstance(source);
                 return (Collection<E>) source;
diff --git a/core/sis-metadata/src/main/java/org/apache/sis/metadata/Freezer.java b/core/sis-metadata/src/main/java/org/apache/sis/metadata/StateChanger.java
similarity index 79%
rename from core/sis-metadata/src/main/java/org/apache/sis/metadata/Freezer.java
rename to core/sis-metadata/src/main/java/org/apache/sis/metadata/StateChanger.java
index 3a38883..18381f5 100644
--- a/core/sis-metadata/src/main/java/org/apache/sis/metadata/Freezer.java
+++ b/core/sis-metadata/src/main/java/org/apache/sis/metadata/StateChanger.java
@@ -31,22 +31,29 @@ import org.apache.sis.metadata.iso.identification.DefaultRepresentativeFraction;
 
 
 /**
- * Returns unmodifiable view of metadata elements of arbitrary type.
- * This class tries to avoid creating new clones as much as possible.
+ * Invokes {@link ModifiableMetadata#apply(ModifiableMetadata.State)} recursively on metadata
elements.
+ *
+ * As of Apache SIS 1.0, this class is used only for {@link ModifiableMetadata.State#FINAL}.
+ * But a future version may use this object for other states too.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @version 1.0
  * @since   0.3
  * @module
  */
-final class Freezer extends MetadataVisitor<Boolean> {
+final class StateChanger extends MetadataVisitor<Boolean> {
     /**
-     * The {@code Freezer} instance in current use. The clean way would have been to pass
the
-     * instance in argument to all {@code apply(State.FINAL)} methods in metadata packages.
-     * But above-cited methods are public, and we do not want to expose {@code Freezer} in
public API for now.
-     * This thread-local is a workaround for that situation.
+     * The {@code StateChanger} instance in current use. The clean way would have been to
pass
+     * the instance in argument to all {@code apply(State.FINAL)} methods in metadata packages.
+     * But above-cited methods are public, and we do not want to expose {@code StateChanger}
+     * in public API. This thread-local is a workaround for that situation.
      */
-    private static final ThreadLocal<Freezer> VISITORS = ThreadLocal.withInitial(Freezer::new);
+    private static final ThreadLocal<StateChanger> VISITORS = ThreadLocal.withInitial(StateChanger::new);
+
+    /**
+     * The state to apply on all metadata objects.
+     */
+    private ModifiableMetadata.State target;
 
     /**
      * All objects made immutable during iteration over children properties.
@@ -60,24 +67,28 @@ final class Freezer extends MetadataVisitor<Boolean> {
     private Cloner cloner;
 
     /**
-     * Creates a new {@code Freezer} instance.
+     * Creates a new {@code StateChanger} instance.
      */
-    private Freezer() {
+    private StateChanger() {
         existings = new HashMap<>(32);
     }
 
     /**
-     * Returns the visitor for the current thread if it already exists, or creates a new
one otherwise.
+     * Applies a state change on the given metadata object.
      */
-    static Freezer getOrCreate() {
-        return VISITORS.get();
+    static void applyTo(final ModifiableMetadata.State target, final ModifiableMetadata metadata)
{
+        final StateChanger changer = VISITORS.get();
+        final ModifiableMetadata.State previous = changer.target;
+        changer.target = target;
+        changer.walk(metadata.getStandard(), null, metadata, true);
+        changer.target = previous;
     }
 
     /**
-     * Returns the thread-local variable that created this {@code Freezer} instance.
+     * Returns the thread-local variable that created this {@code StateChanger} instance.
      */
     @Override
-    final ThreadLocal<Freezer> creator() {
+    final ThreadLocal<StateChanger> creator() {
         return VISITORS;
     }
 
@@ -106,9 +117,9 @@ final class Freezer extends MetadataVisitor<Boolean> {
     }
 
     /**
-     * Recursively freezes all elements in the given array.
+     * Recursively change the state of all elements in the given array.
      */
-    private void freezeAll(final Object[] array) throws CloneNotSupportedException {
+    private void applyTo(final Object[] array) throws CloneNotSupportedException {
         for (int i=0; i < array.length; i++) {
             array[i] = visit(null, array[i]);
         }
@@ -119,8 +130,8 @@ final class Freezer extends MetadataVisitor<Boolean> {
      * This method performs the following heuristic tests:
      *
      * <ul>
-     *   <li>If the specified object is an instance of {@code ModifiableMetadata},
-     *       then {@link ModifiableMetadata#unmodifiable()} is invoked on that object.</li>
+     *   <li>If the specified object is an instance of {@code ModifiableMetadata},
then
+     *       {@link ModifiableMetadata#apply(ModifiableMetadata.State)} is invoked on that
object.</li>
      *   <li>Otherwise, if the object is a {@linkplain Collection collection}, then
the
      *       content is copied into a new collection of similar type, with values replaced
      *       by their unmodifiable variant.</li>
@@ -140,9 +151,12 @@ final class Freezer extends MetadataVisitor<Boolean> {
          *          It may have its own algorithm for freezing itself.
          */
         if (object instanceof ModifiableMetadata) {
-            ((ModifiableMetadata) object).freeze();
+            ((ModifiableMetadata) object).apply(target);
             return unique(object);
         }
+        if (target != ModifiableMetadata.State.FINAL) {
+            return object;
+        }
         if (object instanceof DefaultRepresentativeFraction) {
             ((DefaultRepresentativeFraction) object).freeze();
             return unique(object);
@@ -175,7 +189,7 @@ final class Freezer extends MetadataVisitor<Boolean> {
                         } else if (collection instanceof CodeListSet<?>) {
                             collection = Collections.unmodifiableSet(((CodeListSet<?>)
collection).clone());
                         } else {
-                            freezeAll(array);
+                            applyTo(array);
                             collection = CollectionsExt.immutableSet(false, array);
                         }
                     } else {
@@ -184,7 +198,7 @@ final class Freezer extends MetadataVisitor<Boolean> {
                          * Conservatively assumes a List if we are not sure to have a Set
since the list
                          * is less destructive (no removal of duplicated values).
                          */
-                        freezeAll(array);
+                        applyTo(array);
                         collection = UnmodifiableArrayList.wrap(array);
                     }
                     break;


Mime
View raw message