sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 01/02: Get synchronization lock only once for a whole WKT string to format.
Date Mon, 09 Sep 2019 15:21:03 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

commit 9e454858db01357d2ec39987abdf07cb47ac44e0
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Sep 9 15:46:16 2019 +0200

    Get synchronization lock only once for a whole WKT string to format.
---
 .../main/java/org/apache/sis/io/wkt/WKTFormat.java | 43 +++++++++++++---------
 1 file changed, 25 insertions(+), 18 deletions(-)

diff --git a/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java b/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
index e3ac8bd..d738ae7 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
@@ -837,24 +837,31 @@ public class WKTFormat extends CompoundFormat<Object> {
             updateFormatter(formatter);
             this.formatter = formatter;
         }
-        final boolean valid;
-        try {
-            formatter.setBuffer(buffer);
-            valid = formatter.appendElement(object) || formatter.appendValue(object);
-        } finally {
-            warnings = formatter.getWarnings();     // Must be saved before formatter.clear()
is invoked.
-            formatter.setBuffer(null);
-            formatter.clear();
-        }
-        if (warnings != null) {
-            warnings.setRoot(object);
-        }
-        if (!valid) {
-            throw new ClassCastException(Errors.getResources(getLocale()).getString(
-                    Errors.Keys.IllegalArgumentClass_2, "object", object.getClass()));
-        }
-        if (buffer != toAppendTo) {
-            toAppendTo.append(buffer);
+        /*
+         * Since each operation on `buffer` is synchronized, this synchronization block allow
the lock
+         * to be obtained only one time instead than many times for each `StringBuffer` method
invoked.
+         * As a bonus, it ensures a consistent result if the given `StringBuffer` is used
concurrently.
+         */
+        synchronized (buffer) {
+            final boolean valid;
+            try {
+                formatter.setBuffer(buffer);
+                valid = formatter.appendElement(object) || formatter.appendValue(object);
+            } finally {
+                warnings = formatter.getWarnings();     // Must be saved before formatter.clear()
is invoked.
+                formatter.setBuffer(null);
+                formatter.clear();
+            }
+            if (warnings != null) {
+                warnings.setRoot(object);
+            }
+            if (!valid) {
+                throw new ClassCastException(Errors.getResources(getLocale()).getString(
+                        Errors.Keys.IllegalArgumentClass_2, "object", object.getClass()));
+            }
+            if (buffer != toAppendTo) {
+                toAppendTo.append(buffer);
+            }
         }
     }
 


Mime
View raw message