All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.common.data.LabelTypes Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.common.data;

import com.google.gerrit.entities.LabelId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LabelTypes {
  protected List labelTypes;
  private transient volatile Map byLabel;
  private transient volatile Map positions;

  protected LabelTypes() {}

  public LabelTypes(List approvals) {
    labelTypes = Collections.unmodifiableList(new ArrayList<>(approvals));
  }

  public List getLabelTypes() {
    return labelTypes;
  }

  public LabelType byLabel(LabelId labelId) {
    return byLabel().get(labelId.get().toLowerCase());
  }

  public LabelType byLabel(String labelName) {
    return byLabel().get(labelName.toLowerCase());
  }

  private Map byLabel() {
    if (byLabel == null) {
      synchronized (this) {
        if (byLabel == null) {
          Map l = new HashMap<>();
          if (labelTypes != null) {
            for (LabelType t : labelTypes) {
              l.put(t.getName().toLowerCase(), t);
            }
          }
          byLabel = l;
        }
      }
    }
    return byLabel;
  }

  @Override
  public String toString() {
    return labelTypes.toString();
  }

  public Comparator nameComparator() {
    final Map positions = positions();
    return new Comparator() {
      @Override
      public int compare(String left, String right) {
        int lp = position(left);
        int rp = position(right);
        int cmp = lp - rp;
        if (cmp == 0) {
          cmp = left.compareTo(right);
        }
        return cmp;
      }

      private int position(String name) {
        Integer p = positions.get(name);
        return p != null ? p : positions.size();
      }
    };
  }

  private Map positions() {
    if (positions == null) {
      synchronized (this) {
        if (positions == null) {
          Map p = new HashMap<>();
          if (labelTypes != null) {
            int i = 0;
            for (LabelType t : labelTypes) {
              p.put(t.getName(), i++);
            }
          }
          positions = p;
        }
      }
    }
    return positions;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy