1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 #include "router_item_compare.h"
16 
17 #include "router_map_helper.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
RouterItemCompare(const std::set<std::string> & moduleNameSet)21 RouterItemCompare::RouterItemCompare(const std::set<std::string> &moduleNameSet) : moduleNameSet(moduleNameSet)
22 {}
23 
operator ()(const RouterItem & item1,const RouterItem & item2) const24 bool RouterItemCompare::operator() (const RouterItem &item1, const RouterItem &item2) const
25 {
26     if (item1.name != item2.name) {
27         return item1.name < item2.name;
28     }
29 
30     std::string version1 = RouterMapHelper::ExtractVersionFromOhmurl(item1.ohmurl);
31     std::string version2 = RouterMapHelper::ExtractVersionFromOhmurl(item2.ohmurl);
32     int versionCompare = RouterMapHelper::Compare(version1, version2);
33     if (versionCompare != 0) {
34         return versionCompare > 0;
35     }
36 
37     bool contains1 = moduleNameSet.find(item1.moduleName) != moduleNameSet.end();
38     bool contains2 = moduleNameSet.find(item2.moduleName) != moduleNameSet.end();
39     if (contains1 && contains2) {
40         return item1.moduleName < item2.moduleName;
41     } else if (!contains1 && contains2) {
42         return false;
43     } else if (contains1 && !contains2) {
44         return true;
45     }
46     return item1.moduleName < item2.moduleName;
47 }
48 } // namespace AppExecFwk
49 } // namespace OHOS