Line data Source code
1 : // Copyright (C) 2015 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.server.account; 16 : 17 : import com.google.common.collect.Lists; 18 : import com.google.common.collect.MultimapBuilder; 19 : import com.google.common.collect.SetMultimap; 20 : import com.google.common.collect.Sets; 21 : import com.google.gerrit.common.Nullable; 22 : import com.google.gerrit.entities.BranchNameKey; 23 : import com.google.gerrit.entities.Project; 24 : import com.google.gerrit.server.git.ValidationError; 25 : import com.google.gerrit.server.git.meta.TabFile; 26 : import java.io.IOException; 27 : import java.util.List; 28 : import java.util.Set; 29 : 30 5 : public class DestinationList extends TabFile { 31 : public static final String DIR_NAME = "destinations"; 32 5 : private SetMultimap<String, BranchNameKey> destinations = 33 5 : MultimapBuilder.hashKeys().hashSetValues().build(); 34 : 35 : public Set<BranchNameKey> getDestinations(String label) { 36 5 : return destinations.get(label); 37 : } 38 : 39 : void parseLabel(String label, String text, ValidationError.Sink errors) throws IOException { 40 5 : destinations.replaceValues(label, toSet(parse(text, DIR_NAME + label, TRIM, null, errors))); 41 5 : } 42 : 43 : @Nullable 44 : String asText(String label) { 45 1 : Set<BranchNameKey> dests = destinations.get(label); 46 1 : if (dests == null) { 47 0 : return null; 48 : } 49 1 : List<Row> rows = Lists.newArrayListWithCapacity(dests.size()); 50 1 : for (BranchNameKey dest : sort(dests)) { 51 1 : rows.add(new Row(dest.branch(), dest.project().get())); 52 1 : } 53 1 : return asText("Ref", "Project", rows); 54 : } 55 : 56 : private static Set<BranchNameKey> toSet(List<Row> destRows) { 57 5 : Set<BranchNameKey> dests = Sets.newHashSetWithExpectedSize(destRows.size()); 58 5 : for (Row row : destRows) { 59 5 : dests.add(BranchNameKey.create(Project.nameKey(row.right), row.left)); 60 5 : } 61 5 : return dests; 62 : } 63 : }