LCOV - code coverage report
Current view: top level - entities - LabelTypes.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 39 41 95.1 %
Date: 2022-11-19 15:00:39 Functions: 10 12 83.3 %

          Line data    Source code
       1             : // Copyright (C) 2009 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.entities;
      16             : 
      17             : import java.util.ArrayList;
      18             : import java.util.Collections;
      19             : import java.util.Comparator;
      20             : import java.util.HashMap;
      21             : import java.util.List;
      22             : import java.util.Map;
      23             : import java.util.Optional;
      24             : 
      25             : public class LabelTypes {
      26             :   protected List<LabelType> labelTypes;
      27             :   private transient volatile Map<String, LabelType> byLabel;
      28             :   private transient volatile Map<String, Integer> positions;
      29             : 
      30           0 :   protected LabelTypes() {}
      31             : 
      32         146 :   public LabelTypes(List<? extends LabelType> approvals) {
      33         146 :     labelTypes = Collections.unmodifiableList(new ArrayList<>(approvals));
      34         146 :   }
      35             : 
      36             :   public List<LabelType> getLabelTypes() {
      37         146 :     return labelTypes;
      38             :   }
      39             : 
      40             :   public Optional<LabelType> byLabel(LabelId labelId) {
      41          73 :     return Optional.ofNullable(byLabel().get(labelId.get().toLowerCase()));
      42             :   }
      43             : 
      44             :   public Optional<LabelType> byLabel(String labelName) {
      45         103 :     return Optional.ofNullable(byLabel().get(labelName.toLowerCase()));
      46             :   }
      47             : 
      48             :   private Map<String, LabelType> byLabel() {
      49         103 :     if (byLabel == null) {
      50         103 :       synchronized (this) {
      51         103 :         if (byLabel == null) {
      52         103 :           Map<String, LabelType> l = new HashMap<>();
      53         103 :           if (labelTypes != null) {
      54         103 :             for (LabelType t : labelTypes) {
      55         103 :               l.put(t.getName().toLowerCase(), t);
      56         103 :             }
      57             :           }
      58         103 :           byLabel = l;
      59             :         }
      60         103 :       }
      61             :     }
      62         103 :     return byLabel;
      63             :   }
      64             : 
      65             :   @Override
      66             :   public String toString() {
      67           0 :     return labelTypes.toString();
      68             :   }
      69             : 
      70             :   public Comparator<String> nameComparator() {
      71         103 :     final Map<String, Integer> positions = positions();
      72         103 :     return new Comparator<>() {
      73             :       @Override
      74             :       public int compare(String left, String right) {
      75         103 :         int lp = position(left);
      76         103 :         int rp = position(right);
      77         103 :         int cmp = lp - rp;
      78         103 :         if (cmp == 0) {
      79         103 :           cmp = left.compareTo(right);
      80             :         }
      81         103 :         return cmp;
      82             :       }
      83             : 
      84             :       private int position(String name) {
      85         103 :         Integer p = positions.get(name);
      86         103 :         return p != null ? p : positions.size();
      87             :       }
      88             :     };
      89             :   }
      90             : 
      91             :   private Map<String, Integer> positions() {
      92         103 :     if (positions == null) {
      93         103 :       synchronized (this) {
      94         103 :         if (positions == null) {
      95         103 :           Map<String, Integer> p = new HashMap<>();
      96         103 :           if (labelTypes != null) {
      97         103 :             int i = 0;
      98         103 :             for (LabelType t : labelTypes) {
      99         103 :               p.put(t.getName(), i++);
     100         103 :             }
     101             :           }
     102         103 :           positions = p;
     103             :         }
     104         103 :       }
     105             :     }
     106         103 :     return positions;
     107             :   }
     108             : }

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