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

com.metamx.common.guava.Comparators Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2011,2012 Metamarkets Group Inc.
 *
 * 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 com.metamx.common.guava;

import org.joda.time.DateTimeComparator;
import org.joda.time.Interval;

import java.util.Comparator;

/**
 */
public class Comparators
{
  /**
   * This is a "reverse" comparator.  Positive becomes negative, negative becomes positive and 0 (equal) stays the same.
   * This was poorly named as "inverse" as it's not really inverting a true/false relationship
   * 
   * @param baseComp
   * @param 
   * @return
   */
  public static  Comparator inverse(final Comparator baseComp)
  {
    return new Comparator()
    {
      @Override
      public int compare(T t, T t1)
      {
        return - baseComp.compare(t, t1);
      }
    };
  }

  /**
   * Use Guava Ordering.natural() instead
   *
   * @param 
   * @return
   */
  @Deprecated
  public static  Comparator comparable()
  {
    return new Comparator()
    {
      @Override
      public int compare(T t, T t1)
      {
        return t.compareTo(t1);
      }
    };
  }

  private static final Comparator INTERVAL_COMPARATOR = new Comparator()
  {
    private DateTimeComparator dateTimeComp = DateTimeComparator.getInstance();

    @Override
    public int compare(Interval lhs, Interval rhs)
    {
      int retVal = dateTimeComp.compare(lhs.getStart(), rhs.getStart());
      if (retVal == 0) {
        retVal = dateTimeComp.compare(lhs.getEnd(), rhs.getEnd());
      }
      return retVal;
    }
  };

  @Deprecated
  public static Comparator intervals()
  {
    return intervalsByStartThenEnd();
  }

  public static Comparator intervalsByStartThenEnd()
  {
    return INTERVAL_COMPARATOR;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy