All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.cache.query.internal.types.TemporalComparator Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */

package com.gemstone.gemfire.cache.query.internal.types;

import java.util.*;


/**
 * Comparator for mixed comparisons between instances of
 * java.util.Date, java.sql.Date, java.sql.Time, and java.sql.Timestamp.
 *
 * @version     $Revision: 1.1 $
 * @author      ericz
 */


class TemporalComparator implements Comparator
{
        // all temporal comparators are created equal
    @Override
    public boolean equals(Object obj)
    {
        return obj instanceof TemporalComparator;
    }

        // throws ClassCastExcepton if obj1 or obj2 is not a java.util.Date or subclass
    public int compare(Object obj1, Object obj2)
    {
        java.util.Date date1 = (java.util.Date)obj1;
        java.util.Date date2 = (java.util.Date)obj2;
        long ms1 = date1.getTime();
        long ms2 = date2.getTime();

            // if we're dealing with Timestamps, then we need to extract milliseconds
            // out of the nanos and then do a compare with the "extra" nanos
        int extraNanos1 = 0;
        int extraNanos2 = 0;
        if (date1 instanceof java.sql.Timestamp)
        {
            int nanos = ((java.sql.Timestamp)date1).getNanos();
            int ms = nanos / 1000000;
            ms1 += ms;
            extraNanos1 = nanos - (ms * 1000000);
        }

        if (date2 instanceof java.sql.Timestamp)
        {
            int nanos = ((java.sql.Timestamp)date2).getNanos();
            int ms = nanos / 1000000;
            ms2 += ms;
            extraNanos2 = nanos - (ms * 1000000);
        }

        if (ms1 != ms2)
            return ms1 < ms2 ? -1 : 1;
        return extraNanos1 == extraNanos2 ? 0 : (extraNanos1 < extraNanos2 ? -1 : 1);
    }
}

            
                                                             
    




© 2015 - 2024 Weber Informatics LLC | Privacy Policy