LCOV - code coverage report
Current view: top level - common/data - FilenameComparator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 25 25 100.0 %
Date: 2022-11-19 15:00:39 Functions: 2 2 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2016 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.common.data;
      16             : 
      17             : import com.google.gerrit.entities.Patch;
      18             : import java.util.Arrays;
      19             : import java.util.Comparator;
      20             : import java.util.HashSet;
      21             : import java.util.Set;
      22             : 
      23             : public class FilenameComparator implements Comparator<String> {
      24          66 :   public static final FilenameComparator INSTANCE = new FilenameComparator();
      25             : 
      26          66 :   private static final Set<String> cppHeaderSuffixes =
      27          66 :       new HashSet<>(Arrays.asList(".h", ".hh", ".hxx", ".hpp"));
      28             : 
      29             :   private FilenameComparator() {}
      30             : 
      31             :   @Override
      32             :   public int compare(String path1, String path2) {
      33           7 :     if (Patch.COMMIT_MSG.equals(path1) && Patch.COMMIT_MSG.equals(path2)) {
      34           1 :       return 0;
      35           7 :     } else if (Patch.COMMIT_MSG.equals(path1)) {
      36           1 :       return -1;
      37           7 :     } else if (Patch.COMMIT_MSG.equals(path2)) {
      38           1 :       return 1;
      39             :     }
      40           7 :     if (Patch.MERGE_LIST.equals(path1) && Patch.MERGE_LIST.equals(path2)) {
      41           1 :       return 0;
      42           7 :     } else if (Patch.MERGE_LIST.equals(path1)) {
      43           1 :       return -1;
      44           7 :     } else if (Patch.MERGE_LIST.equals(path2)) {
      45           1 :       return 1;
      46             :     }
      47             : 
      48           7 :     int s1 = path1.lastIndexOf('.');
      49           7 :     int s2 = path2.lastIndexOf('.');
      50           7 :     if (s1 > 0 && s2 > 0 && path1.substring(0, s1).equals(path2.substring(0, s2))) {
      51           5 :       String suffixA = path1.substring(s1);
      52           5 :       String suffixB = path2.substring(s2);
      53             :       // C++ and C: give priority to header files (.h/.hpp/...)
      54           5 :       if (cppHeaderSuffixes.contains(suffixA)) {
      55           1 :         return -1;
      56           5 :       } else if (cppHeaderSuffixes.contains(suffixB)) {
      57           1 :         return 1;
      58             :       }
      59             :     }
      60           7 :     return path1.compareTo(path2);
      61             :   }
      62             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750