org.openmdx.state2.spi.Order Maven / Gradle / Ivy
/*
* ====================================================================
* Project: openMDX, http://www.openmdx.org/
* Description: ValidTimes
* Owner: OMEX AG, Switzerland, http://www.omex.ch
* ====================================================================
*
* This software is published under the BSD license as listed below.
*
* Copyright (c) 2008, OMEX AG, Switzerland
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the openMDX team nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ------------------
*
* This product includes software developed by other organizations as
* listed in the NOTICE file.
*/
package org.openmdx.state2.spi;
import java.util.Date;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import org.openmdx.kernel.exception.BasicException;
import org.w3c.cci2.ImmutableDatatype;
import org.w3c.spi.DatatypeFactories;
/**
* Valid Times
*/
public class Order {
/**
* Constructor
*/
private Order() {
// Avoid instantiation
}
/**
* Plus one day
*/
public static final Duration ONE_DAY = DatatypeFactories.xmlDatatypeFactory(
).newDurationDayTime(
true, // isPositive
1, // day
0, // hour
0, // minute
0 // second
);
/**
* Minus one day
*/
public static final Duration MINUS_ONE_DAY = DatatypeFactories.xmlDatatypeFactory(
).newDurationDayTime(
false, // isPositive
1, // day
0, // hour
0, // minute
0 // second
);
//------------------------------------------------------------------------
// Date States
//------------------------------------------------------------------------
/**
* Tests whether validTo is greater than or equal to validFrom
*
* @param validFrom
* @param validTo
*
* @throws IllegalArgumentException if validTo is less than validFrom
*/
public static void assertTimeRange(
XMLGregorianCalendar validFrom,
XMLGregorianCalendar validTo
){
if(validFrom != null && validTo != null) {
if(validTo.compare(validFrom) == DatatypeConstants.LESSER) {
throw BasicException.initHolder(
new IllegalArgumentException(
"validTo must be greater than or equal to validFrom",
BasicException.newEmbeddedExceptionStack(
BasicException.Code.DEFAULT_DOMAIN,
BasicException.Code.BAD_PARAMETER,
new BasicException.Parameter("validFrom", validFrom),
new BasicException.Parameter("validTo", validTo)
)
)
);
}
}
}
/**
* Compare two XMLGregorianCalendar values where null
is
* considered to be smaller than every other value.
*
* @param d1 the first value
* @param d2 the second value
*
* @return a negative integer, zero, or a positive integer as d1 is less
* than, equal to, or greater than d2.
*/
public static int compareValidFrom(
XMLGregorianCalendar d1,
XMLGregorianCalendar d2
){
return d1 == null ? (
d2 == null ? 0 : -1
) : (
d2 == null ? 1 : compare(d1,d2)
);
}
/**
* Compare two XMLGregorianCalendar values where null
is
* considered to be greater than every other value.
*
* @param d1
* @param d2
*
* @return a negative integer, zero, or a positive integer as d1 is less
* than, equal to, or greater than d2.
*/
public static int compareValidTo(
XMLGregorianCalendar d1,
XMLGregorianCalendar d2
){
return d1 == null ? (
d2 == null ? 0 : 1
) : (
d2 == null ? -1 : compare(d1,d2)
);
}
/**
* Compare two XMLGregorianCalendar values where null
is
* considered to be lesser than every other value for from
* and greater than every other value for to
.
*
* @param from
* @param to
*
* @return a negative integer, zero, or a positive integer as from
* is less than, equal to, or greater than to
.
*/
public static int compareValidFromToValidTo(
XMLGregorianCalendar from,
XMLGregorianCalendar to
){
return from == null || to == null ? -1 : compare(from,to);
}
/**
* Compare two (mutable or immutable) XMLGregorianCalendar
values
*
* @param d1
* @param d2
*
* @return a negative integer, zero, or a positive integer as from
* is less than, equal to, or greater than to
.
*/
public static int compare(
XMLGregorianCalendar d1,
XMLGregorianCalendar d2
){
boolean i1 = d1 instanceof ImmutableDatatype>;
boolean i2 = d2 instanceof ImmutableDatatype>;
return
i1 == i2 ? d1.compare(d2) :
i1 ? ((XMLGregorianCalendar)d1.clone()).compare(d2) :
d1.compare((XMLGregorianCalendar) d2.clone());
}
/**
* Compare two (mutable or immutable) XMLGregorianCalendar
values
*
* @param d1
* @param d2
*
* @return true if the two values are either equal or both null
*/
public static boolean equal(
XMLGregorianCalendar d1,
XMLGregorianCalendar d2
){
boolean i1 = d1 instanceof ImmutableDatatype>;
boolean i2 = d2 instanceof ImmutableDatatype>;
return
d1 == d2 ? true :
d1 == null || d2 == null ? false :
i1 == i2 ? d1.equals(d2) :
i1 ? d1.clone().equals(d2) :
d1.equals(d2.clone());
}
//------------------------------------------------------------------------
// Date-Time States
//------------------------------------------------------------------------
/**
* Tests whether invalidFrom is greater than validFrom
*
* @param validFrom
* @param invalidFrom
*
* @throws IllegalArgumentException if invalidFrom is less than or equal to validFrom
*/
public static void assertTimeRange(
Date validFrom,
Date invalidFrom
){
if(validFrom != null && invalidFrom != null) {
if(invalidFrom.compareTo(validFrom) <= 0) {
throw BasicException.initHolder(
new IllegalArgumentException(
"invalidFrom must be greater than validFrom",
BasicException.newEmbeddedExceptionStack(
BasicException.Code.DEFAULT_DOMAIN,
BasicException.Code.BAD_PARAMETER,
new BasicException.Parameter("validFrom", validFrom),
new BasicException.Parameter("invalidFrom", invalidFrom)
)
)
);
}
}
}
/**
* Compare two Date values where null
is
* considered to be smaller than every other value.
*
* @param d1
* @param d2
*
* @return a negative integer, zero, or a positive integer as d1 is less
* than, equal to, or greater than d2.
*/
public static int compareValidFrom(
Date d1,
Date d2
){
return d1 == null ? (
d2 == null ? 0 : -1
) : (
d2 == null ? 1 : d1.compareTo(d2)
);
}
/**
* Compare two Date values where null
is
* considered to be greater than every other value.
*
* @param d1
* @param d2
*
* @return a negative integer, zero, or a positive integer as d1 is less
* than, equal to, or greater than d2.
*/
public static int compareInvalidFrom(
Date d1,
Date d2
){
return d1 == null ? (
d2 == null ? 0 : 1
) : (
d2 == null ? -1 : d1.compareTo(d2)
);
}
/**
* Compare two Date values where null
is
* considered to be lesser than every other value for from
* and greater than every other value for to
.
*
* @param from
* @param to
*
* @return a negative integer, zero, or a positive integer as from
* is less than, equal to, or greater than to
.
*/
public static int compareValidFromToValidTo(
Date from,
Date to
){
return from == null || to == null ? -1 : from.compareTo(to);
}
//------------------------------------------------------------------------
// Existence
//------------------------------------------------------------------------
/**
* Compare two Date values where null
is
* considered to be greater than every other value.
*
* @param d1
* @param d2
*
* @return a negative integer, zero, or a positive integer as d1 is less
* than, equal to, or greater than d2.
*/
public static int compareRemovedAt(
Date d1,
Date d2
){
return d1 == null ? (
d2 == null ? 0 : 1
) : (
d2 == null ? -1 : d1.compareTo(d2)
);
}
//------------------------------------------------------------------------
// Adjacence
//------------------------------------------------------------------------
/**
* Retrieve the previous day
*
* @param date
*
* @return the previous day
*/
public static XMLGregorianCalendar predecessor(
XMLGregorianCalendar date
){
XMLGregorianCalendar predecessor = (XMLGregorianCalendar) date.clone();
predecessor.add(MINUS_ONE_DAY);
return predecessor;
}
/**
* Retrieve the next day
*
* @param date
*
* @return the next day
*/
public static XMLGregorianCalendar successor(
XMLGregorianCalendar date
){
XMLGregorianCalendar successor = (XMLGregorianCalendar) date.clone();
successor.add(ONE_DAY);
return successor;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy