You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2014/11/01 11:51:27 UTC
svn commit: r1635919 - in /sis/branches/JDK8/core:
sis-metadata/src/main/java/org/apache/sis/metadata/
sis-metadata/src/main/java/org/apache/sis/metadata/iso/maintenance/
sis-metadata/src/main/java/org/apache/sis/metadata/iso/quality/
sis-referencing/s...
Author: desruisseaux
Date: Sat Nov 1 10:51:26 2014
New Revision: 1635919
URL: http://svn.apache.org/r1635919
Log:
Reduce memory usage by avoiding the creation of unused empty collection
in equals, hashCode, toString, freeze, isEmpty and prune methods.
Added:
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/Semaphores.java
- copied, changed from r1635480, sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Semaphores.java
Removed:
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Semaphores.java
Modified:
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/maintenance/DefaultScopeDescription.java
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/quality/AbstractElement.java
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/ConcatenatedTransform.java
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java
sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java
Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java?rev=1635919&r1=1635918&r2=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/AbstractMetadata.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -19,6 +19,7 @@ package org.apache.sis.metadata;
import java.util.Map;
import java.util.logging.Logger;
import javax.xml.bind.annotation.XmlTransient;
+import org.apache.sis.internal.system.Semaphores;
import org.apache.sis.util.Emptiable;
import org.apache.sis.util.ComparisonMode;
import org.apache.sis.util.LenientComparable;
@@ -72,7 +73,7 @@ import org.apache.sis.util.logging.Loggi
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-2.4)
- * @version 0.3
+ * @version 0.5
* @module
*
* @see MetadataStandard
@@ -144,7 +145,21 @@ public abstract class AbstractMetadata i
*/
@Override
public boolean isEmpty() {
- return Pruner.isEmpty(this, true, false);
+ /*
+ * The NULL_COLLECTION semaphore prevents creation of new empty collections by getter methods
+ * (a consequence of lazy instantiation). The intend is to avoid creation of unnecessary objects
+ * for all unused properties. Users should not see behavioral difference, except if they override
+ * some getters with an implementation invoking other getters. However in such cases, users would
+ * have been exposed to null values at XML marshalling time anyway.
+ */
+ final boolean allowNull = Semaphores.queryAndSet(Semaphores.NULL_COLLECTION);
+ try {
+ return Pruner.isEmpty(this, true, false);
+ } finally {
+ if (!allowNull) {
+ Semaphores.clear(Semaphores.NULL_COLLECTION);
+ }
+ }
}
/**
@@ -155,7 +170,15 @@ public abstract class AbstractMetadata i
* @throws UnmodifiableMetadataException If this metadata is not modifiable.
*/
public void prune() {
- Pruner.isEmpty(this, true, true);
+ // See comment in 'isEmpty()' about NULL_COLLECTION semaphore purpose.
+ final boolean allowNull = Semaphores.queryAndSet(Semaphores.NULL_COLLECTION);
+ try {
+ Pruner.isEmpty(this, true, true);
+ } finally {
+ if (!allowNull) {
+ Semaphores.clear(Semaphores.NULL_COLLECTION);
+ }
+ }
}
/**
Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java?rev=1635919&r1=1635918&r2=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -37,6 +37,7 @@ import org.apache.sis.util.resources.Err
import org.apache.sis.util.collection.TreeTable;
import org.apache.sis.util.collection.CheckedContainer;
import org.apache.sis.internal.system.Modules;
+import org.apache.sis.internal.system.Semaphores;
import org.apache.sis.internal.system.SystemListener;
import org.apache.sis.internal.simple.SimpleCitation;
@@ -896,10 +897,23 @@ public class MetadataStandard implements
*/
final ObjectPair pair = new ObjectPair(metadata1, metadata2);
final Set<ObjectPair> inProgress = ObjectPair.CURRENT.get();
- if (inProgress.add(pair)) try {
- return accessor.equals(metadata1, metadata2, mode);
- } finally {
- inProgress.remove(pair);
+ if (inProgress.add(pair)) {
+ /*
+ * The NULL_COLLECTION semaphore prevents creation of new empty collections by getter methods
+ * (a consequence of lazy instantiation). The intend is to avoid creation of unnecessary objects
+ * for all unused properties. Users should not see behavioral difference, except if they override
+ * some getters with an implementation invoking other getters. However in such cases, users would
+ * have been exposed to null values at XML marshalling time anyway.
+ */
+ final boolean allowNull = Semaphores.queryAndSet(Semaphores.NULL_COLLECTION);
+ try {
+ return accessor.equals(metadata1, metadata2, mode);
+ } finally {
+ inProgress.remove(pair);
+ if (!allowNull) {
+ Semaphores.clear(Semaphores.NULL_COLLECTION);
+ }
+ }
} else {
/*
* If we get here, a cycle has been found. Returns 'true' in order to allow the caller to continue
@@ -926,16 +940,23 @@ public class MetadataStandard implements
public int hashCode(final Object metadata) throws ClassCastException {
if (metadata != null) {
final Map<Object,Object> inProgress = RecursivityGuard.HASH_CODES.get();
- if (inProgress.put(metadata, Boolean.TRUE) == null) try {
- return getAccessor(metadata.getClass(), true).hashCode(metadata);
- } finally {
- inProgress.remove(metadata);
+ if (inProgress.put(metadata, Boolean.TRUE) == null) {
+ // See comment in 'equals(…) about NULL_COLLECTION semaphore purpose.
+ final boolean allowNull = Semaphores.queryAndSet(Semaphores.NULL_COLLECTION);
+ try {
+ return getAccessor(metadata.getClass(), true).hashCode(metadata);
+ } finally {
+ inProgress.remove(metadata);
+ if (!allowNull) {
+ Semaphores.clear(Semaphores.NULL_COLLECTION);
+ }
+ }
}
/*
* If we get there, a cycle has been found. We can not compute a hash code value for that metadata.
* However it should not be a problem since this metadata is part of a bigger metadata object, and
* that enclosing object has other properties for computing its hash code. We just need the result
- * to be consistent, we should be the case if properties ordering is always the same.
+ * to be consistent, wich should be the case if properties ordering is always the same.
*/
}
return 0;
Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java?rev=1635919&r1=1635918&r2=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/ModifiableMetadata.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -33,9 +33,9 @@ import org.apache.sis.util.resources.Err
import org.apache.sis.util.collection.CodeListSet;
import org.apache.sis.internal.util.CheckedHashSet;
import org.apache.sis.internal.util.CheckedArrayList;
+import org.apache.sis.internal.system.Semaphores;
import static org.apache.sis.util.collection.Containers.isNullOrEmpty;
-import static org.apache.sis.internal.jaxb.Context.isMarshalling;
/**
@@ -83,7 +83,7 @@ import static org.apache.sis.internal.ja
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3 (derived from geotk-2.1)
- * @version 0.3
+ * @version 0.5
* @module
*/
@XmlTransient
@@ -221,12 +221,23 @@ public abstract class ModifiableMetadata
public void freeze() {
if (isModifiable()) {
ModifiableMetadata success = null;
+ /*
+ * The NULL_COLLECTION semaphore prevents creation of new empty collections by getter methods
+ * (a consequence of lazy instantiation). The intend is to avoid creation of unnecessary objects
+ * for all unused properties. Users should not see behavioral difference, except if they override
+ * some getters with an implementation invoking other getters. However in such cases, users would
+ * have been exposed to null values at XML marshalling time anyway.
+ */
+ final boolean allowNull = Semaphores.queryAndSet(Semaphores.NULL_COLLECTION);
try {
unmodifiable = FREEZING;
getStandard().freeze(this);
success = this;
} finally {
unmodifiable = success;
+ if (!allowNull) {
+ Semaphores.clear(Semaphores.NULL_COLLECTION);
+ }
}
}
}
@@ -510,6 +521,17 @@ public abstract class ModifiableMetadata
}
/**
+ * Returns {@code true} if empty collection should be returned as {@code null} value.
+ * This is usually not a behavior that we allow in public API. However this behavior
+ * is sometime desired internally, for example when marshalling with JAXB or when
+ * performing a {@code equals}, {@code isEmpty} or {@code prune} operation
+ * (for avoiding creating unnecessary collections).
+ */
+ private static boolean emptyCollectionAsNull() {
+ return Semaphores.query(Semaphores.NULL_COLLECTION);
+ }
+
+ /**
* Returns the specified list, or a new one if {@code c} is null.
* This is a convenience method for implementation of {@code getFoo()} methods.
*
@@ -520,9 +542,9 @@ public abstract class ModifiableMetadata
*/
protected final <E> List<E> nonNullList(final List<E> c, final Class<E> elementType) {
if (c != null) {
- return c.isEmpty() && isMarshalling() ? null : c;
+ return c.isEmpty() && emptyCollectionAsNull() ? null : c;
}
- if (isMarshalling()) {
+ if (emptyCollectionAsNull()) {
return null;
}
if (isModifiable()) {
@@ -549,9 +571,9 @@ public abstract class ModifiableMetadata
*/
protected final <E> Set<E> nonNullSet(final Set<E> c, final Class<E> elementType) {
if (c != null) {
- return c.isEmpty() && isMarshalling() ? null : c;
+ return c.isEmpty() && emptyCollectionAsNull() ? null : c;
}
- if (isMarshalling()) {
+ if (emptyCollectionAsNull()) {
return null;
}
if (isModifiable()) {
@@ -580,9 +602,9 @@ public abstract class ModifiableMetadata
protected final <E> Collection<E> nonNullCollection(final Collection<E> c, final Class<E> elementType) {
if (c != null) {
assert collectionType(elementType).isInstance(c);
- return c.isEmpty() && isMarshalling() ? null : c;
+ return c.isEmpty() && emptyCollectionAsNull() ? null : c;
}
- if (isMarshalling()) {
+ if (emptyCollectionAsNull()) {
return null;
}
final boolean isModifiable = isModifiable();
Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java?rev=1635919&r1=1635918&r2=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -29,6 +29,7 @@ import org.apache.sis.util.collection.Ta
import org.apache.sis.util.collection.TreeTableFormat;
import org.apache.sis.internal.util.UnmodifiableArrayList;
import org.apache.sis.internal.system.LocalizedStaticObject;
+import org.apache.sis.internal.system.Semaphores;
/**
@@ -45,7 +46,7 @@ import org.apache.sis.internal.system.Lo
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3
- * @version 0.3
+ * @version 0.5
* @module
*/
final class TreeTableView implements TreeTable, Serializable {
@@ -137,7 +138,21 @@ final class TreeTableView implements Tre
f.setColumns(TableColumn.NAME, TableColumn.VALUE);
format = f;
}
- return format.format(this);
+ /*
+ * The NULL_COLLECTION semaphore prevents creation of new empty collections by getter methods
+ * (a consequence of lazy instantiation). The intend is to avoid creation of unnecessary objects
+ * for all unused properties. Users should not see behavioral difference, except if they override
+ * some getters with an implementation invoking other getters. However in such cases, users would
+ * have been exposed to null values at XML marshalling time anyway.
+ */
+ final boolean allowNull = Semaphores.queryAndSet(Semaphores.NULL_COLLECTION);
+ try {
+ return format.format(this);
+ } finally {
+ if (!allowNull) {
+ Semaphores.clear(Semaphores.NULL_COLLECTION);
+ }
+ }
}
}
Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/maintenance/DefaultScopeDescription.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/maintenance/DefaultScopeDescription.java?rev=1635919&r1=1635918&r2=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/maintenance/DefaultScopeDescription.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/maintenance/DefaultScopeDescription.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -26,10 +26,10 @@ import org.opengis.metadata.maintenance.
import org.apache.sis.metadata.iso.ISOMetadata;
import org.apache.sis.internal.metadata.ExcludedSet;
import org.apache.sis.internal.jaxb.Context;
+import org.apache.sis.internal.system.Semaphores;
import org.apache.sis.util.collection.CheckedContainer;
import org.apache.sis.util.resources.Messages;
-import static org.apache.sis.internal.jaxb.Context.isMarshalling;
import static org.apache.sis.util.collection.Containers.isNullOrEmpty;
@@ -210,7 +210,8 @@ public class DefaultScopeDescription ext
if (property == code) {
return cast(value);
} else if (!(value instanceof Set) || !((Set<?>) value).isEmpty()) {
- return isMarshalling() ? null : new ExcludedSet<>(NAMES[code-1], NAMES[property-1]);
+ return Semaphores.query(Semaphores.NULL_COLLECTION)
+ ? null : new ExcludedSet<>(NAMES[code-1], NAMES[property-1]);
}
}
// Unconditionally create a new set, because the
Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/quality/AbstractElement.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/quality/AbstractElement.java?rev=1635919&r1=1635918&r2=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/quality/AbstractElement.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/quality/AbstractElement.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -38,11 +38,11 @@ import org.opengis.metadata.quality.Logi
import org.opengis.metadata.quality.EvaluationMethodType;
import org.opengis.util.InternationalString;
import org.apache.sis.metadata.iso.ISOMetadata;
+import org.apache.sis.internal.system.Semaphores;
import org.apache.sis.util.collection.CheckedContainer;
import org.apache.sis.util.resources.Errors;
import static org.apache.sis.util.collection.Containers.isNullOrEmpty;
-import static org.apache.sis.internal.jaxb.Context.isMarshalling;
/**
@@ -517,7 +517,7 @@ public class AbstractElement extends ISO
@Override
@XmlElement(name = "dateTime")
public Collection<Date> getDates() {
- if (isMarshalling()) {
+ if (Semaphores.query(Semaphores.NULL_COLLECTION)) {
return isNullOrEmpty(dates) ? null : dates;
}
if (dates == null) {
Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/ConcatenatedTransform.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/ConcatenatedTransform.java?rev=1635919&r1=1635918&r2=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/ConcatenatedTransform.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/operation/transform/ConcatenatedTransform.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -32,7 +32,7 @@ import org.opengis.referencing.operation
import org.apache.sis.parameter.Parameterized;
import org.apache.sis.referencing.operation.matrix.Matrices;
import org.apache.sis.referencing.operation.matrix.MatrixSIS;
-import org.apache.sis.internal.referencing.Semaphores;
+import org.apache.sis.internal.system.Semaphores;
import org.apache.sis.util.Classes;
import org.apache.sis.util.LenientComparable;
import org.apache.sis.util.ComparisonMode;
Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java?rev=1635919&r1=1635918&r2=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -29,6 +29,7 @@ import org.apache.sis.util.logging.Warni
import org.apache.sis.util.resources.Errors;
import org.apache.sis.util.resources.Messages;
import org.apache.sis.util.resources.IndexedResourceBundle;
+import org.apache.sis.internal.system.Semaphores;
import org.apache.sis.xml.MarshalContext;
import org.apache.sis.xml.ValueConverter;
import org.apache.sis.xml.ReferenceResolver;
@@ -86,6 +87,11 @@ public final class Context extends Marsh
public static final int SUBSTITUTE_MIMETYPE = 16;
/**
+ * Bit where to store whether {@link #finish()} shall invoke {@code Semaphores.clear(Semaphores.NULL_COLLECTION)}.
+ */
+ private static final int CLEAR_SEMAPHORE = 32;
+
+ /**
* The thread-local context. Elements are created in the constructor, and removed in a
* {@code finally} block by the {@link #finish()} method. This {@code ThreadLocal} shall
* not contain any value when no (un)marshalling is in progress.
@@ -186,6 +192,11 @@ public final class Context extends Marsh
this.warningListener = warningListener;
previous = current();
CURRENT.set(this);
+ if ((bitMasks & MARSHALLING) != 0) {
+ if (!Semaphores.queryAndSet(Semaphores.NULL_COLLECTION)) {
+ this.bitMasks |= CLEAR_SEMAPHORE;
+ }
+ }
}
/**
@@ -263,23 +274,6 @@ public final class Context extends Marsh
}
/**
- * Returns {@code true} if XML marshalling is under progress.
- * This convenience method is implemented by:
- *
- * {@preformat java
- * return isFlagSet(current(), MARSHALLING);
- * }
- *
- * Callers should use the {@link #isFlagSet(Context, int)} method instead if the
- * {@code Context} instance is known, for avoiding a call to {@link #current()}.
- *
- * @return {@code true} if XML marshalling is under progress.
- */
- public static boolean isMarshalling() {
- return isFlagSet(current(), MARSHALLING);
- }
-
- /**
* Returns {@code true} if the given flag is set.
*
* @param context The current context, or {@code null} if none.
@@ -524,6 +518,9 @@ public final class Context extends Marsh
* Invoked in a {@code finally} block when a unmarshalling process is finished.
*/
public final void finish() {
+ if ((bitMasks & CLEAR_SEMAPHORE) != 0) {
+ Semaphores.clear(Semaphores.NULL_COLLECTION);
+ }
if (previous != null) {
CURRENT.set(previous);
} else {
Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java?rev=1635919&r1=1635918&r2=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/NonMarshalledAuthority.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -185,7 +185,7 @@ public final class NonMarshalledAuthorit
* @return The identifiers to marshal, or {@code null} if none.
*/
public static Collection<Identifier> excludeOnMarshalling(Collection<Identifier> identifiers) {
- if (identifiers != null && Context.isMarshalling()) {
+ if (identifiers != null && Context.isFlagSet(Context.current(), Context.MARSHALLING)) {
int count = identifiers.size();
if (count != 0) {
final Identifier[] copy = identifiers.toArray(new Identifier[count]);
Copied: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/Semaphores.java (from r1635480, sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Semaphores.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/Semaphores.java?p2=sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/Semaphores.java&p1=sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Semaphores.java&r1=1635480&r2=1635919&rev=1635919&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/Semaphores.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/Semaphores.java [UTF-8] Sat Nov 1 10:51:26 2014
@@ -14,11 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.sis.internal.referencing;
+package org.apache.sis.internal.system;
/**
- * Semaphores that need to be shared across different referencing packages. Each thread has its own set of semaphores.
+ * Thread-local booleans that need to be shared across different packages. Each thread has its own set of booleans.
* The {@link #clear(int)} method <strong>must</strong> be invoked after the {@link #queryAndSet(int)} method in a
* {@code try ... finally} block.
*
@@ -36,14 +36,21 @@ public final class Semaphores {
* {@code AbstractDerivedCRS} objects contain a {@code conversionFromBase} field, which contains a
* {@code DefaultConversion.targetCRS} field referencing back the {@code AbstractDerivedCRS} object.
*/
- public static final int COMPARING = 1;
+ public static final byte COMPARING = 1;
/**
* A flag to indicate that {@link org.apache.sis.referencing.operation.DefaultSingleOperation}
* is querying {@code org.apache.sis.referencing.operation.transform.ConcatenatedTransform} in
* the intend to format WKT (normally a {@code "PROJCS"} element).
*/
- public static final int PROJCS = 2;
+ public static final byte PROJCS = 2;
+
+ /**
+ * A flag to indicate that empty collections should be returned as {@code null}. Returning null
+ * collections is not a recommended practice, but is useful in some situations like marshalling
+ * a XML document with JAXB, when we want to omit empty XML blocks.
+ */
+ public static final byte NULL_COLLECTION = 4;
/**
* The flags per running thread.
@@ -53,7 +60,7 @@ public final class Semaphores {
/**
* The bit flags.
*/
- private int flags;
+ private byte flags;
/**
* For internal use only.
@@ -64,10 +71,10 @@ public final class Semaphores {
/**
* Returns {@code true} if the given flag is set.
*
- * @param flag One of {@link #COMPARING} or {@link #PROJCS} constants.
+ * @param flag One of {@link #COMPARING}, {@link #PROJCS} or other constants.
* @return {@code true} if the given flag is set.
*/
- public static boolean query(final int flag) {
+ public static boolean query(final byte flag) {
final Semaphores s = FLAGS.get();
return (s != null) && (s.flags & flag) != 0;
}
@@ -75,10 +82,10 @@ public final class Semaphores {
/**
* Sets the given flag.
*
- * @param flag One of {@link #COMPARING} or {@link #PROJCS} constants.
+ * @param flag One of {@link #COMPARING}, {@link #PROJCS} or other constants.
* @return {@code true} if the given flag was already set.
*/
- public static boolean queryAndSet(final int flag) {
+ public static boolean queryAndSet(final byte flag) {
Semaphores s = FLAGS.get();
if (s == null) {
s = new Semaphores();
@@ -92,9 +99,9 @@ public final class Semaphores {
/**
* Clears the given flag.
*
- * @param flag One of {@link #COMPARING} or {@link #PROJCS} constants.
+ * @param flag One of {@link #COMPARING}, {@link #PROJCS} or other constants.
*/
- public static void clear(final int flag) {
+ public static void clear(final byte flag) {
final Semaphores s = FLAGS.get();
if (s != null) {
s.flags &= ~flag;