• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
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 
17 package com.android.cts.apicommon;
18 
19 import java.util.Comparator;
20 
21 public class CoverageComparator implements Comparator<HasCoverage> {
22 
23     /** Compares whether two coverage entities are same. */
compare(HasCoverage entity, HasCoverage otherEntity)24     public int compare(HasCoverage entity, HasCoverage otherEntity) {
25         int lhsPct = Math.round(entity.getCoveragePercentage());
26         int rhsPct = Math.round(otherEntity.getCoveragePercentage());
27         int diff = Integer.compare(getCoveragePercentageSegment(lhsPct),
28                 getCoveragePercentageSegment(rhsPct));
29         if (diff != 0) {
30             return diff;
31         }
32         diff = Integer.compare(otherEntity.getMemberSize(), entity.getMemberSize());
33         if (diff != 0) {
34             return diff;
35         }
36         return entity.getName().compareTo(otherEntity.getName());
37     }
38 
39     /**
40      * Distill coverage percentage down to 3 major segments
41      * @param coveragePercentage
42      * @return
43      */
getCoveragePercentageSegment(int coveragePercentage)44     private int getCoveragePercentageSegment(int coveragePercentage) {
45         // note that this segmentation logic is duplicated in api-coverage.xsl
46         if (coveragePercentage <= 50) {
47             return 0;
48         } else if (coveragePercentage <= 80) {
49             return 1;
50         } else {
51             return 2;
52         }
53     }
54 }
55