This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi-xmlbeans.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 5dfcae0e compare xs:duration values by value in 
JavaGDurationHolderEx.equal_to (#153)
5dfcae0e is described below

commit 5dfcae0e986b144d567296fe53ca0140efddf792
Author: aizu-m <[email protected]>
AuthorDate: Tue Nov 3 15:22:03 2026 +0530

    compare xs:duration values by value in JavaGDurationHolderEx.equal_to (#153)
---
 .../impl/values/JavaGDurationHolderEx.java         |  16 ++-
 .../DurationIdentityConstraintValidateTest.java    | 112 +++++++++++++++++++++
 2 files changed, 126 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/apache/xmlbeans/impl/values/JavaGDurationHolderEx.java 
b/src/main/java/org/apache/xmlbeans/impl/values/JavaGDurationHolderEx.java
index 875fc3dc..91e0b275 100644
--- a/src/main/java/org/apache/xmlbeans/impl/values/JavaGDurationHolderEx.java
+++ b/src/main/java/org/apache/xmlbeans/impl/values/JavaGDurationHolderEx.java
@@ -19,6 +19,8 @@ import org.apache.xmlbeans.*;
 import org.apache.xmlbeans.impl.common.QNameHelper;
 import org.apache.xmlbeans.impl.common.ValidationContext;
 
+import java.math.BigDecimal;
+
 public abstract class JavaGDurationHolderEx extends XmlObjectBase {
     public JavaGDurationHolderEx(SchemaType type, boolean complex) {
         _schemaType = type;
@@ -152,11 +154,21 @@ public abstract class JavaGDurationHolderEx extends 
XmlObjectBase {
     }
 
     protected boolean equal_to(XmlObject d) {
-        return _value.equals(((XmlObjectBase) d).getGDurationValue());
+        // equal in the value space, so P1D equals PT24H
+        return _value.compareToGDuration(((XmlObjectBase) 
d).getGDurationValue()) == 0;
     }
 
     protected int value_hash_code() {
-        return _value.hashCode();
+        // hash the month and second totals that equal_to compares
+        long months = (long) _value.getYear() * 12 + _value.getMonth();
+        BigDecimal seconds = BigDecimal.valueOf(
+            (((long) _value.getDay() * 24 + _value.getHour()) * 60 + 
_value.getMinute()) * 60 + _value.getSecond())
+            .add(_value.getFraction()).stripTrailingZeros();
+        if (_value.getSign() < 0) {
+            months = -months;
+            seconds = seconds.negate();
+        }
+        return Long.hashCode(months) * 31 + seconds.hashCode();
     }
 
     protected void validate_simpleval(String lexical, ValidationContext ctx) {
diff --git 
a/src/test/java/misc/checkin/DurationIdentityConstraintValidateTest.java 
b/src/test/java/misc/checkin/DurationIdentityConstraintValidateTest.java
new file mode 100644
index 00000000..d0070c7e
--- /dev/null
+++ b/src/test/java/misc/checkin/DurationIdentityConstraintValidateTest.java
@@ -0,0 +1,112 @@
+/*   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed 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 misc.checkin;
+
+import org.apache.xmlbeans.SchemaTypeLoader;
+import org.apache.xmlbeans.XmlBeans;
+import org.apache.xmlbeans.XmlDuration;
+import org.apache.xmlbeans.XmlError;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
+import org.apache.xmlbeans.impl.xb.xsdschema.SchemaDocument;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class DurationIdentityConstraintValidateTest {
+
+    private static final String XSD =
+        "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' " +
+        "xmlns:t='urn:t' targetNamespace='urn:t' 
elementFormDefault='qualified'>" +
+        "  <xs:element name='u'>" +
+        "    <xs:complexType><xs:sequence>" +
+        "      <xs:element name='d' type='xs:duration' 
maxOccurs='unbounded'/>" +
+        "    </xs:sequence></xs:complexType>" +
+        "    <xs:unique name='uniq'><xs:selector xpath='t:d'/><xs:field 
xpath='.'/></xs:unique>" +
+        "  </xs:element>" +
+        "  <xs:element name='k'>" +
+        "    <xs:complexType><xs:sequence>" +
+        "      <xs:element name='d' type='xs:duration' 
maxOccurs='unbounded'/>" +
+        "      <xs:element name='r' type='xs:duration' 
maxOccurs='unbounded'/>" +
+        "    </xs:sequence></xs:complexType>" +
+        "    <xs:key name='key'><xs:selector xpath='t:d'/><xs:field 
xpath='.'/></xs:key>" +
+        "    <xs:keyref name='ref' refer='t:key'><xs:selector 
xpath='t:r'/><xs:field xpath='.'/></xs:keyref>" +
+        "  </xs:element>" +
+        "</xs:schema>";
+
+    private static SchemaTypeLoader loader;
+
+    @BeforeAll
+    static void compileSchema() throws Exception {
+        loader = XmlBeans.loadXsd(new 
XmlObject[]{SchemaDocument.Factory.parse(XSD)});
+    }
+
+    private static List<String> validate(String xml) throws Exception {
+        XmlObject doc = loader.parse(xml, null, null);
+        List<XmlError> errors = new ArrayList<>();
+        doc.validate(new XmlOptions().setErrorListener(errors));
+        return 
errors.stream().map(XmlError::getErrorCode).collect(Collectors.toList());
+    }
+
+    private static List<String> unique(String a, String b) throws Exception {
+        return validate("<t:u xmlns:t='urn:t'><t:d>" + a + "</t:d><t:d>" + b + 
"</t:d></t:u>");
+    }
+
+    private static List<String> keyref(String key, String ref) throws 
Exception {
+        return validate("<t:k xmlns:t='urn:t'><t:d>" + key + "</t:d><t:r>" + 
ref + "</t:r></t:k>");
+    }
+
+    @Test
+    void equalDurationsWrittenDifferentlyAreDuplicates() throws Exception {
+        String[][] same = {
+            {"P1D", "PT24H"}, {"PT1M", "PT60S"}, {"P1Y", "P12M"},
+            {"PT1.5S", "PT1.50S"}, {"PT90S", "PT1M30S"}, {"P0D", "-PT0S"}
+        };
+        for (String[] pair : same) {
+            
assertEquals(Collections.singletonList("cvc-identity-constraint.4.1"),
+                unique(pair[0], pair[1]), pair[0] + " " + pair[1]);
+            assertEquals(Collections.emptyList(), keyref(pair[0], pair[1]), 
pair[0] + " " + pair[1]);
+        }
+    }
+
+    @Test
+    void differentDurationsAreNotDuplicates() throws Exception {
+        // P1M is 28 to 31 days, so it is not equal to P30D
+        String[][] different = {{"P1D", "PT23H"}, {"P1M", "P30D"}, {"PT1S", 
"-PT1S"}, {"PT1.5S", "PT1.25S"}};
+        for (String[] pair : different) {
+            assertEquals(Collections.emptyList(), unique(pair[0], pair[1]), 
pair[0] + " " + pair[1]);
+            
assertEquals(Collections.singletonList("cvc-identity-constraint.4.3"),
+                keyref(pair[0], pair[1]), pair[0] + " " + pair[1]);
+        }
+    }
+
+    @Test
+    void valueEqualsAgreesWithValueHashCode() {
+        XmlDuration a = XmlDuration.Factory.newValue("P1DT1H");
+        XmlDuration b = XmlDuration.Factory.newValue("PT25H");
+        assertTrue(a.valueEquals(b));
+        assertEquals(a.valueHashCode(), b.valueHashCode());
+        assertFalse(a.valueEquals(XmlDuration.Factory.newValue("PT26H")));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to