Example usage for java.sql Time compareTo

List of usage examples for java.sql Time compareTo

Introduction

In this page you can find the example usage for java.sql Time compareTo.

Prototype

public int compareTo(Date anotherDate) 

Source Link

Document

Compares two Dates for ordering.

Usage

From source file:org.ofbiz.base.util.ObjectType.java

public static Boolean doRealCompare(Object value1, Object value2, String operator, String type, String format,
        List<Object> messages, Locale locale, ClassLoader loader, boolean value2InlineConstant) {
    boolean verboseOn = Debug.verboseOn();

    if (verboseOn)
        Debug.logVerbose("Comparing value1: \"" + value1 + "\" " + operator + " value2:\"" + value2 + "\"",
                module);// w w w .ja  v a2s .c o  m

    try {
        if (!"PlainString".equals(type)) {
            Class<?> clz = ObjectType.loadClass(type, loader);
            type = clz.getName();
        }
    } catch (ClassNotFoundException e) {
        Debug.logWarning("The specified type [" + type
                + "] is not a valid class or a known special type, may see more errors later because of this: "
                + e.getMessage(), module);
    }

    if (value1 == null) {
        // some default behavior for null values, results in a bit cleaner operation
        if ("is-null".equals(operator)) {
            return Boolean.TRUE;
        } else if ("is-not-null".equals(operator)) {
            return Boolean.FALSE;
        } else if ("is-empty".equals(operator)) {
            return Boolean.TRUE;
        } else if ("is-not-empty".equals(operator)) {
            return Boolean.FALSE;
        } else if ("contains".equals(operator)) {
            return Boolean.FALSE;
        }
    }

    int result = 0;

    Object convertedValue2 = null;
    if (value2 != null) {
        Locale value2Locale = locale;
        if (value2InlineConstant) {
            value2Locale = UtilMisc.parseLocale("en");
        }
        try {
            convertedValue2 = ObjectType.simpleTypeConvert(value2, type, format, value2Locale);
        } catch (GeneralException e) {
            Debug.logError(e, module);
            messages.add("Could not convert value2 for comparison: " + e.getMessage());
            return null;
        }
    }

    // have converted value 2, now before converting value 1 see if it is a Collection and we are doing a contains comparison
    if ("contains".equals(operator) && value1 instanceof Collection<?>) {
        Collection<?> col1 = (Collection<?>) value1;
        return col1.contains(convertedValue2) ? Boolean.TRUE : Boolean.FALSE;
    }

    Object convertedValue1 = null;
    try {
        convertedValue1 = ObjectType.simpleTypeConvert(value1, type, format, locale);
    } catch (GeneralException e) {
        Debug.logError(e, module);
        messages.add("Could not convert value1 for comparison: " + e.getMessage());
        return null;
    }

    // handle null values...
    if (convertedValue1 == null || convertedValue2 == null) {
        if ("equals".equals(operator)) {
            return convertedValue1 == null && convertedValue2 == null ? Boolean.TRUE : Boolean.FALSE;
        } else if ("not-equals".equals(operator)) {
            return convertedValue1 == null && convertedValue2 == null ? Boolean.FALSE : Boolean.TRUE;
        } else if ("is-not-empty".equals(operator) || "is-empty".equals(operator)) {
            // do nothing, handled later...
        } else {
            if (convertedValue1 == null) {
                messages.add("Left value is null, cannot complete compare for the operator " + operator);
                return null;
            }
            if (convertedValue2 == null) {
                messages.add("Right value is null, cannot complete compare for the operator " + operator);
                return null;
            }
        }
    }

    if ("contains".equals(operator)) {
        if ("java.lang.String".equals(type) || "PlainString".equals(type)) {
            String str1 = (String) convertedValue1;
            String str2 = (String) convertedValue2;

            return str1.indexOf(str2) < 0 ? Boolean.FALSE : Boolean.TRUE;
        } else {
            messages.add(
                    "Error in XML file: cannot do a contains compare between a String and a non-String type");
            return null;
        }
    } else if ("is-empty".equals(operator)) {
        if (convertedValue1 == null)
            return Boolean.TRUE;
        if (convertedValue1 instanceof String && ((String) convertedValue1).length() == 0)
            return Boolean.TRUE;
        if (convertedValue1 instanceof List<?> && ((List<?>) convertedValue1).size() == 0)
            return Boolean.TRUE;
        if (convertedValue1 instanceof Map<?, ?> && ((Map<?, ?>) convertedValue1).size() == 0)
            return Boolean.TRUE;
        return Boolean.FALSE;
    } else if ("is-not-empty".equals(operator)) {
        if (convertedValue1 == null)
            return Boolean.FALSE;
        if (convertedValue1 instanceof String && ((String) convertedValue1).length() == 0)
            return Boolean.FALSE;
        if (convertedValue1 instanceof List<?> && ((List<?>) convertedValue1).size() == 0)
            return Boolean.FALSE;
        if (convertedValue1 instanceof Map<?, ?> && ((Map<?, ?>) convertedValue1).size() == 0)
            return Boolean.FALSE;
        return Boolean.TRUE;
    }

    if ("java.lang.String".equals(type) || "PlainString".equals(type)) {
        String str1 = (String) convertedValue1;
        String str2 = (String) convertedValue2;

        if (str1.length() == 0 || str2.length() == 0) {
            if ("equals".equals(operator)) {
                return str1.length() == 0 && str2.length() == 0 ? Boolean.TRUE : Boolean.FALSE;
            } else if ("not-equals".equals(operator)) {
                return str1.length() == 0 && str2.length() == 0 ? Boolean.FALSE : Boolean.TRUE;
            } else {
                messages.add(
                        "ERROR: Could not do a compare between strings with one empty string for the operator "
                                + operator);
                return null;
            }
        }
        result = str1.compareTo(str2);
    } else if ("java.lang.Double".equals(type) || "java.lang.Float".equals(type)
            || "java.lang.Long".equals(type) || "java.lang.Integer".equals(type)
            || "java.math.BigDecimal".equals(type)) {
        Number tempNum = (Number) convertedValue1;
        double value1Double = tempNum.doubleValue();

        tempNum = (Number) convertedValue2;
        double value2Double = tempNum.doubleValue();

        if (value1Double < value2Double)
            result = -1;
        else if (value1Double > value2Double)
            result = 1;
        else
            result = 0;
    } else if ("java.sql.Date".equals(type)) {
        java.sql.Date value1Date = (java.sql.Date) convertedValue1;
        java.sql.Date value2Date = (java.sql.Date) convertedValue2;
        result = value1Date.compareTo(value2Date);
    } else if ("java.sql.Time".equals(type)) {
        java.sql.Time value1Time = (java.sql.Time) convertedValue1;
        java.sql.Time value2Time = (java.sql.Time) convertedValue2;
        result = value1Time.compareTo(value2Time);
    } else if ("java.sql.Timestamp".equals(type)) {
        java.sql.Timestamp value1Timestamp = (java.sql.Timestamp) convertedValue1;
        java.sql.Timestamp value2Timestamp = (java.sql.Timestamp) convertedValue2;
        result = value1Timestamp.compareTo(value2Timestamp);
    } else if ("java.lang.Boolean".equals(type)) {
        Boolean value1Boolean = (Boolean) convertedValue1;
        Boolean value2Boolean = (Boolean) convertedValue2;
        if ("equals".equals(operator)) {
            if ((value1Boolean.booleanValue() && value2Boolean.booleanValue())
                    || (!value1Boolean.booleanValue() && !value2Boolean.booleanValue()))
                result = 0;
            else
                result = 1;
        } else if ("not-equals".equals(operator)) {
            if ((!value1Boolean.booleanValue() && value2Boolean.booleanValue())
                    || (value1Boolean.booleanValue() && !value2Boolean.booleanValue()))
                result = 0;
            else
                result = 1;
        } else {
            messages.add("Can only compare Booleans using the operators 'equals' or 'not-equals'");
            return null;
        }
    } else if ("java.lang.Object".equals(type)) {
        if (convertedValue1.equals(convertedValue2)) {
            result = 0;
        } else {
            result = 1;
        }
    } else {
        messages.add("Type \"" + type + "\" specified for compare not supported.");
        return null;
    }

    if (verboseOn)
        Debug.logVerbose("Got Compare result: " + result + ", operator: " + operator, module);
    if ("less".equals(operator)) {
        if (result >= 0)
            return Boolean.FALSE;
    } else if ("greater".equals(operator)) {
        if (result <= 0)
            return Boolean.FALSE;
    } else if ("less-equals".equals(operator)) {
        if (result > 0)
            return Boolean.FALSE;
    } else if ("greater-equals".equals(operator)) {
        if (result < 0)
            return Boolean.FALSE;
    } else if ("equals".equals(operator)) {
        if (result != 0)
            return Boolean.FALSE;
    } else if ("not-equals".equals(operator)) {
        if (result == 0)
            return Boolean.FALSE;
    } else {
        messages.add("Specified compare operator \"" + operator + "\" not known.");
        return null;
    }

    if (verboseOn)
        Debug.logVerbose("Returning true", module);
    return Boolean.TRUE;
}

From source file:org.sakaiproject.tool.section.decorator.SectionDecorator.java

/**
 * Compares SectionDecorators by the section's first meeting times.
 *
 * @param sortAscending// www. j  a  v a 2 s .c om
 * @return
 */
public static final Comparator<SectionDecorator> getTimeComparator(final boolean sortAscending) {
    return new Comparator<SectionDecorator>() {
        public int compare(SectionDecorator section1, SectionDecorator section2) {

            // First compare the category name, then compare the time
            int categoryNameComparison = section1.getCategory().compareTo(section2.getCategory());
            if (categoryNameComparison == 0) {
                // These are in the same category, so compare by the first meeting time
                List meetings1 = section1.getDecoratedMeetings();
                List meetings2 = section2.getDecoratedMeetings();

                MeetingDecorator meeting1 = (MeetingDecorator) meetings1.get(0);
                MeetingDecorator meeting2 = (MeetingDecorator) meetings2.get(0);

                Time startTime1 = meeting1.getStartTime();
                Time startTime2 = meeting2.getStartTime();

                if (startTime1 == null && startTime2 != null) {
                    return sortAscending ? -1 : 1;
                }
                if (startTime2 == null && startTime1 != null) {
                    return sortAscending ? 1 : -1;
                }

                if (startTime1 == null && startTime2 == null || startTime1.equals(startTime2)) {
                    return getTitleComparator(sortAscending).compare(section1, section2);
                }
                return sortAscending ? startTime1.compareTo(startTime2) : startTime2.compareTo(startTime1);
            } else {
                return categoryNameComparison;
            }
        }
    };
}