Search in sources :

Example 76 with Duration

use of javax.xml.datatype.Duration in project jena by apache.

the class DurationImpl method add.

/**
 * <p>Computes a new duration whose value is <code>this+rhs</code>.</p>
 *
 * <p>For example,</p>
 * <pre>
 * "1 day" + "-3 days" = "-2 days"
 * "1 year" + "1 day" = "1 year and 1 day"
 * "-(1 hour,50 minutes)" + "-20 minutes" = "-(1 hours,70 minutes)"
 * "15 hours" + "-3 days" = "-(2 days,9 hours)"
 * "1 year" + "-1 day" = IllegalStateException
 * </pre>
 *
 * <p>Since there's no way to meaningfully subtract 1 day from 1 month,
 * there are cases where the operation fails in
 * {@link IllegalStateException}.</p>
 *
 * <p>
 * Formally, the computation is defined as follows.</p>
 * <p>
 * Firstly, we can assume that two {@link Duration}s to be added
 * are both positive without losing generality (i.e.,
 * <code>(-X)+Y=Y-X</code>, <code>X+(-Y)=X-Y</code>,
 * <code>(-X)+(-Y)=-(X+Y)</code>)
 *
 * <p>
 * Addition of two positive {@link Duration}s are simply defined as
 * field by field addition where missing fields are treated as 0.
 * <p>
 * A field of the resulting {@link Duration} will be unset if and
 * only if respective fields of two input {@link Duration}s are unset.
 * <p>
 * Note that <code>lhs.add(rhs)</code> will be always successful if
 * <code>lhs.signum()*rhs.signum()!=-1</code> or both of them are
 * normalized.</p>
 *
 * @param rhs <code>Duration</code> to add to this <code>Duration</code>
 *
 * @return
 *      non-null valid Duration object.
 *
 * @throws NullPointerException
 *      If the rhs parameter is null.
 * @throws IllegalStateException
 *      If two durations cannot be meaningfully added. For
 *      example, adding negative one day to one month causes
 *      this exception.
 *
 * @see #subtract(Duration)
 */
@Override
public Duration add(final Duration rhs) {
    Duration lhs = this;
    BigDecimal[] buf = new BigDecimal[6];
    buf[0] = sanitize((BigInteger) lhs.getField(DatatypeConstants.YEARS), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.YEARS), rhs.getSign()));
    buf[1] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MONTHS), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MONTHS), rhs.getSign()));
    buf[2] = sanitize((BigInteger) lhs.getField(DatatypeConstants.DAYS), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.DAYS), rhs.getSign()));
    buf[3] = sanitize((BigInteger) lhs.getField(DatatypeConstants.HOURS), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.HOURS), rhs.getSign()));
    buf[4] = sanitize((BigInteger) lhs.getField(DatatypeConstants.MINUTES), lhs.getSign()).add(sanitize((BigInteger) rhs.getField(DatatypeConstants.MINUTES), rhs.getSign()));
    buf[5] = sanitize((BigDecimal) lhs.getField(DatatypeConstants.SECONDS), lhs.getSign()).add(sanitize((BigDecimal) rhs.getField(DatatypeConstants.SECONDS), rhs.getSign()));
    // align sign
    // Y,M
    alignSigns(buf, 0, 2);
    // D,h,m,s
    alignSigns(buf, 2, 6);
    // make sure that the sign bit is consistent across all 6 fields.
    int s = 0;
    for (int i = 0; i < 6; i++) {
        if (s * buf[i].signum() < 0) {
            throw new IllegalStateException();
        }
        if (s == 0) {
            s = buf[i].signum();
        }
    }
    return new DurationImpl(s >= 0, toBigInteger(sanitize(buf[0], s), lhs.getField(DatatypeConstants.YEARS) == null && rhs.getField(DatatypeConstants.YEARS) == null), toBigInteger(sanitize(buf[1], s), lhs.getField(DatatypeConstants.MONTHS) == null && rhs.getField(DatatypeConstants.MONTHS) == null), toBigInteger(sanitize(buf[2], s), lhs.getField(DatatypeConstants.DAYS) == null && rhs.getField(DatatypeConstants.DAYS) == null), toBigInteger(sanitize(buf[3], s), lhs.getField(DatatypeConstants.HOURS) == null && rhs.getField(DatatypeConstants.HOURS) == null), toBigInteger(sanitize(buf[4], s), lhs.getField(DatatypeConstants.MINUTES) == null && rhs.getField(DatatypeConstants.MINUTES) == null), (buf[5].signum() == 0 && lhs.getField(DatatypeConstants.SECONDS) == null && rhs.getField(DatatypeConstants.SECONDS) == null) ? null : sanitize(buf[5], s));
}
Also used : Duration(javax.xml.datatype.Duration) BigDecimal(java.math.BigDecimal)

Example 77 with Duration

use of javax.xml.datatype.Duration in project podam by devopsfolks.

the class XmlTypesExternalFactory method manufacturePojo.

@Override
public <T> T manufacturePojo(Class<T> pojoClass, Type... genericTypeArgs) {
    try {
        if (pojoClass.isAssignableFrom(XMLGregorianCalendar.class)) {
            DatatypeFactory factory = DatatypeFactory.newInstance();
            @SuppressWarnings("unchecked") T calendar = (T) factory.newXMLGregorianCalendar(new GregorianCalendar());
            LOG.info("Externally created XMLGregorianCalendar");
            return calendar;
        } else if (pojoClass.isAssignableFrom(Duration.class)) {
            DatatypeFactory factory = DatatypeFactory.newInstance();
            @SuppressWarnings("unchecked") T duration = (T) factory.newDuration(0L);
            LOG.info("Externally created Duration");
            return duration;
        }
    } catch (Exception e) {
        throw new IllegalStateException("Manufacturing failed", e);
    }
    return null;
}
Also used : DatatypeFactory(javax.xml.datatype.DatatypeFactory) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Duration(javax.xml.datatype.Duration)

Example 78 with Duration

use of javax.xml.datatype.Duration in project cxf by apache.

the class ProxyTest method testCreateSequence.

@SuppressWarnings("unchecked")
private void testCreateSequence(boolean isServer) throws NoSuchMethodException, RMException {
    Method m = Proxy.class.getDeclaredMethod("invoke", new Class[] { OperationInfo.class, ProtocolVariation.class, Object[].class, Map.class, Exchange.class });
    Proxy proxy = EasyMock.createMockBuilder(Proxy.class).addMockedMethod(m).createMock(control);
    proxy.setReliableEndpoint(rme);
    RMManager manager = control.createMock(RMManager.class);
    EasyMock.expect(rme.getManager()).andReturn(manager).anyTimes();
    SourcePolicyType sp = control.createMock(SourcePolicyType.class);
    EasyMock.expect(manager.getSourcePolicy()).andReturn(sp).anyTimes();
    EasyMock.expect(sp.getAcksTo()).andReturn(null).anyTimes();
    Duration d = DatatypeFactory.createDuration("PT12H");
    EasyMock.expect(sp.getSequenceExpiration()).andReturn(d).anyTimes();
    EasyMock.expect(sp.isIncludeOffer()).andReturn(Boolean.TRUE).anyTimes();
    Duration dOffered = DatatypeFactory.createDuration("PT24H");
    EasyMock.expect(sp.getOfferedSequenceExpiration()).andReturn(dOffered).anyTimes();
    Source source = control.createMock(Source.class);
    EasyMock.expect(rme.getSource()).andReturn(source).anyTimes();
    Identifier offeredId = control.createMock(Identifier.class);
    EasyMock.expect(source.generateSequenceIdentifier()).andReturn(offeredId).anyTimes();
    Endpoint endpoint = control.createMock(Endpoint.class);
    EasyMock.expect(rme.getEndpoint(ProtocolVariation.RM10WSA200408)).andReturn(endpoint).anyTimes();
    EndpointInfo epi = control.createMock(EndpointInfo.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(epi).anyTimes();
    ServiceInfo si = control.createMock(ServiceInfo.class);
    EasyMock.expect(epi.getService()).andReturn(si).anyTimes();
    InterfaceInfo ii = control.createMock(InterfaceInfo.class);
    EasyMock.expect(si.getInterface()).andReturn(ii).anyTimes();
    OperationInfo oi = control.createMock(OperationInfo.class);
    org.apache.cxf.ws.rm.v200502.CreateSequenceResponseType csr = null;
    if (isServer) {
        EasyMock.expect(ii.getOperation(RM10Constants.CREATE_SEQUENCE_ONEWAY_QNAME)).andReturn(oi).anyTimes();
        Endpoint ae = control.createMock(Endpoint.class);
        EasyMock.expect(rme.getApplicationEndpoint()).andReturn(ae).anyTimes();
        EasyMock.expect(ae.getExecutor()).andReturn(SynchronousExecutor.getInstance()).anyTimes();
    } else {
        EasyMock.expect(ii.getOperation(RM10Constants.CREATE_SEQUENCE_QNAME)).andReturn(oi).anyTimes();
        csr = new org.apache.cxf.ws.rm.v200502.CreateSequenceResponseType();
    }
    ExchangeImpl exchange = new ExchangeImpl();
    EasyMock.expect(proxy.invoke(EasyMock.same(oi), EasyMock.isA(ProtocolVariation.class), EasyMock.isA(Object[].class), EasyMock.isA(Map.class), EasyMock.isA(Exchange.class))).andReturn(csr).anyTimes();
    EndpointReferenceType defaultAcksTo = control.createMock(EndpointReferenceType.class);
    AttributedURIType aut = control.createMock(AttributedURIType.class);
    EasyMock.expect(aut.getValue()).andReturn("here").anyTimes();
    EasyMock.expect(defaultAcksTo.getAddress()).andReturn(aut).anyTimes();
    RelatesToType relatesTo = control.createMock(RelatesToType.class);
    control.replay();
    Map<String, Object> context = new HashMap<>();
    if (isServer) {
        assertNull(proxy.createSequence(defaultAcksTo, relatesTo, isServer, ProtocolVariation.RM10WSA200408, exchange, context));
    } else {
        assertNotNull(proxy.createSequence(defaultAcksTo, relatesTo, isServer, ProtocolVariation.RM10WSA200408, exchange, context));
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) HashMap(java.util.HashMap) AttributedURIType(org.apache.cxf.ws.addressing.AttributedURIType) Duration(javax.xml.datatype.Duration) Method(java.lang.reflect.Method) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) RelatesToType(org.apache.cxf.ws.addressing.RelatesToType) Identifier(org.apache.cxf.ws.rm.v200702.Identifier) Endpoint(org.apache.cxf.endpoint.Endpoint) SourcePolicyType(org.apache.cxf.ws.rm.manager.SourcePolicyType) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 79 with Duration

use of javax.xml.datatype.Duration in project cxf by apache.

the class SourceSequenceTest method testSetExpires.

@Test
public void testSetExpires() {
    SourceSequence seq = new SourceSequence(id, ProtocolVariation.RM10WSA200408);
    Expires expires = factory.createExpires();
    seq.setExpires(expires);
    assertFalse(seq.isExpired());
    Duration d = DatatypeFactory.PT0S;
    expires.setValue(d);
    seq.setExpires(expires);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        assertFalse(seq.isExpired());
    }
    d = DatatypeFactory.createDuration("PT1S");
    expires.setValue(d);
    seq.setExpires(expires);
    assertFalse(seq.isExpired());
    d = DatatypeFactory.createDuration("-PT1S");
    expires.setValue(d);
    seq.setExpires(expires);
    assertTrue(seq.isExpired());
}
Also used : Duration(javax.xml.datatype.Duration) Expires(org.apache.cxf.ws.rm.v200702.Expires) Test(org.junit.Test)

Example 80 with Duration

use of javax.xml.datatype.Duration in project cxf by apache.

the class FiqlSearchConditionBuilderTest method testGreaterOrEqualToDuration.

@Test
public void testGreaterOrEqualToDuration() throws DatatypeConfigurationException {
    Duration d = DatatypeFactory.newInstance().newDuration(false, 0, 0, 1, 12, 0, 0);
    String ret = b.is("foo").notBefore(d).query();
    assertEquals("foo=ge=-P0Y0M1DT12H0M0S", ret);
}
Also used : Duration(javax.xml.datatype.Duration) Test(org.junit.Test)

Aggregations

Duration (javax.xml.datatype.Duration)137 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)57 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)12 Date (java.util.Date)12 BigDecimal (java.math.BigDecimal)9 Calendar (java.util.Calendar)9 GregorianCalendar (java.util.GregorianCalendar)9 ObjectDeltaType (com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType)8 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)7 CleanupPolicyType (com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType)6 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)5 IOException (java.io.IOException)5 XSDayTimeDuration (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)5 FileNotFoundException (java.io.FileNotFoundException)4 Collection (java.util.Collection)4 DatatypeFactory (javax.xml.datatype.DatatypeFactory)4 NotNull (org.jetbrains.annotations.NotNull)4 PrismObject (com.evolveum.midpoint.prism.PrismObject)3