OrderComparator.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:2k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.core;
  17. import java.util.Comparator;
  18. /**
  19.  * Comparator implementation for Ordered objects,
  20.  * sorting by order value ascending (resp. by priority descending).
  21.  *
  22.  * <p>Non-Ordered objects are treated as greatest order values,
  23.  * thus ending up at the end of the list, in arbitrary order
  24.  * (just like same order values of Ordered objects).
  25.  *
  26.  * @author Juergen Hoeller
  27.  * @since 07.04.2003
  28.  * @see Ordered
  29.  */
  30. public class OrderComparator implements Comparator {
  31.   public int compare(Object o1, Object o2) {
  32.     int i1 = (o1 instanceof Ordered ? ((Ordered) o1).getOrder() : Integer.MAX_VALUE);
  33.   int i2 = (o2 instanceof Ordered ? ((Ordered) o2).getOrder() : Integer.MAX_VALUE);
  34.   // direct evaluation instead of Integer.compareTo to avoid unnecessary object creation
  35.   if (i1 < i2)
  36. return -1;
  37.   else if (i1 > i2)
  38.   return 1;
  39.   else
  40.   return 0;
  41.   }
  42. }