View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.impl;
17  
18  import java.util.Comparator;
19  
20  /**
21   * Weight-based comparator for UriMapping objects.
22   * 
23   * <p>
24   * Used to ensure detailed mapping rules are evaluated before the generic ones.
25   * 
26   * @author Nicolas Richeton
27   * 
28   */
29  public class UriMappingComparator implements Comparator<UriMapping> {
30  
31      @Override
32      public int compare(UriMapping o1, UriMapping o2) {
33          int weightCompare = o2.getWeight() - o1.getWeight();
34  
35          if (weightCompare != 0) {
36              return weightCompare;
37          }
38  
39          // 2 objects with the same weight are usually not the same.
40          // This is required to prevent removal of different rules using the same
41          // weight within a SortedMap.
42          return o2.hashCode() - o1.hashCode();
43      }
44  
45  }