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 dfc7aff69ff1c417cdf6018eaccca8ff53c22b92 Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Sun Nov 19 20:04:05 2023 +0100 Remove `NilDate`. It was not working because this class is mutable, so metadata make copies of it and those copies were not `NilObject`. --- .../main/org/apache/sis/xml/NilDate.java | 69 ---------------------- .../main/org/apache/sis/xml/NilReason.java | 34 ++++++++--- .../test/org/apache/sis/xml/NilReasonTest.java | 13 ---- 3 files changed, 27 insertions(+), 89 deletions(-) diff --git a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/xml/NilDate.java b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/xml/NilDate.java deleted file mode 100644 index 047603685f..0000000000 --- a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/xml/NilDate.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.xml; - -import java.util.Date; -import java.io.ObjectStreamException; - - -/** - * An empty {@code Date} which is nil for the given reason. - * - * @author Martin Desruisseaux (Geomatys) - */ -final class NilDate extends Date implements NilObject { - /** - * For cross-version compatibility. - */ - private static final long serialVersionUID = 4374532826187673813L; - - /** - * The reason why the object is nil. - */ - private final NilReason reason; - - /** - * Creates a new international string which is nil for the given reason. - */ - NilDate(final NilReason reason) { - super(0); - this.reason = reason; - } - - /** - * Returns the reason why this object is nil. - */ - @Override - public NilReason getNilReason() { - return reason; - } - - /** - * Unconditionally returns en empty string. - */ - @Override - public String toString() { - return ""; - } - - /** - * Invoked on deserialization for replacing the deserialized instance by the unique instance. - */ - private Object readResolve() throws ObjectStreamException { - return reason.createNilObject(Date.class); - } -} diff --git a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/xml/NilReason.java b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/xml/NilReason.java index 25310a03be..39bc587a38 100644 --- a/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/xml/NilReason.java +++ b/endorsed/src/org.apache.sis.metadata/main/org/apache/sis/xml/NilReason.java @@ -16,8 +16,8 @@ */ package org.apache.sis.xml; -import java.util.Date; import java.util.Map; +import java.util.Set; import java.net.URI; import java.net.URISyntaxException; import java.io.Serializable; @@ -413,6 +413,21 @@ public final class NilReason implements Serializable { return false; } + /** + * Returns whether the given class is accepted by {@code createNilObject(Class)}. + * The set of accepted types is implementation-dependent and may change in any future Apache SIS version. + * + * @param type the class or interface to test, or {@code null}. + * @return {@code true} if the given type is non-null and is accepted by {@link #createNilObject(Class)}. + * + * @since 1.5 + */ + public static boolean isSupported(final Class<?> type) { + if (type == null) return false; + if (type.isInterface()) return !NilObjectHandler.isIgnoredInterface(type); + return ACCEPTED_CLASSES.contains(type); + } + /** * Returns an object of the given type which is nil for the reason represented by this instance. * The {@code type} argument can be one of the following cases: @@ -428,7 +443,7 @@ public final class NilReason implements Serializable { * {@code 0} or {@code false}, in this preference order, depending on the method return type.</li> * </ul> * </li> - * <li>One of {@link Float}, {@link Double}, {@link String}, {@link URI} or {@link Date} types. + * <li>One of {@link Float}, {@link Double}, {@link String} or {@link URI} types. * In such case, this method returns an instance which will be recognized as "nil" by the XML marshaller.</li> * </ul> * @@ -493,11 +508,18 @@ public final class NilReason implements Serializable { return type.cast(object); } + /** + * Classes that are handled in a special way by {@link #createNilInstance(Class)}. + */ + private static final Set<Class<?>> ACCEPTED_CLASSES = Set.of( + Double.class, Float.class, String.class, URI.class); + /** * Returns a new {@code Float}, {@code Double} or {@code String} instance to be considered as a nil value. * - * <p><b>Reminder:</b> If more special cases are added, do not forget to update the {@link #forObject(Object)} - * method and to update the {@link #createNilObject(Class)} and {@link #forObject(Object)} javadoc.</p> + * <p><b>Reminder:</b> If more special cases are added, + * do not forget to update the {@link #forObject(Object)} method and the {@link #ACCEPTED_CLASSES} set, + * then to update the {@link #createNilObject(Class)} and {@link #forObject(Object)} documentation.</p> * * @throws IllegalArgumentException if the given type is not a supported type. * @@ -512,8 +534,6 @@ public final class NilReason implements Serializable { object = new String(""); // REALLY need a new instance. } else if (type == URI.class) { object = URI.create(""); // Really need a new instance. - } else if (type == Date.class) { - object = new NilDate(this); } else { throw new IllegalArgumentException(Errors.format(Errors.Keys.IllegalArgumentValue_2, "type", type)); } @@ -529,7 +549,7 @@ public final class NilReason implements Serializable { * <li>If the given object implements the {@link NilObject} interface, then this method delegates * to the {@link NilObject#getNilReason()} method.</li> * <li>Otherwise if the given object is one of the {@link Float}, {@link Double}, {@link String}, - * {@link URI} or {@link Date} instances + * or {@link URI} instances * returned by {@link #createNilObject(Class)}, then this method returns the associated reason.</li> * <li>Otherwise this method returns {@code null}.</li> * </ul> diff --git a/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/NilReasonTest.java b/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/NilReasonTest.java index 513ded6721..1d86c8b4dc 100644 --- a/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/NilReasonTest.java +++ b/endorsed/src/org.apache.sis.metadata/test/org/apache/sis/xml/NilReasonTest.java @@ -17,7 +17,6 @@ package org.apache.sis.xml; import java.net.URI; -import java.util.Date; import java.net.URISyntaxException; import org.opengis.util.InternationalString; import org.opengis.metadata.citation.Citation; @@ -175,18 +174,6 @@ public final class NilReasonTest extends TestCase { assertSame(value, NilReason.MISSING.createNilObject(InternationalString.class), "Expected cached value."); } - /** - * Tests {@link NilReason#createNilObject(Class)} for a date. - */ - @Test - public void testCreateNilDate() { - final Date value = NilReason.TEMPLATE.createNilObject(Date.class); - assertEquals("", value.toString()); - assertInstanceOf(NilObject.class, value); - assertSame(NilReason.TEMPLATE, NilReason.forObject(value)); - assertSame(value, NilReason.TEMPLATE.createNilObject(Date.class), "Expected cached value."); - } - /** * Tests {@link NilReason#createNilObject(Class)} for an URI. */