Line data Source code
1 : // Copyright (C) 2017 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.index.project; 16 : 17 : import static com.google.common.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.common.base.MoreObjects; 20 : import com.google.common.collect.ImmutableList; 21 : import com.google.gerrit.entities.Project; 22 : import java.util.ArrayList; 23 : import java.util.List; 24 : import java.util.Optional; 25 : 26 : /** 27 : * Representation of a Gerrit project in the project index. 28 : * 29 : * <p>Includes information about all parent projects. 30 : */ 31 : public class ProjectData { 32 : private final Project project; 33 : private final Optional<ProjectData> parent; 34 : 35 147 : public ProjectData(Project project, Optional<ProjectData> parent) { 36 147 : this.project = project; 37 147 : this.parent = parent; 38 147 : } 39 : 40 : public Project getProject() { 41 147 : return project; 42 : } 43 : 44 : public Optional<ProjectData> getParent() { 45 0 : return parent; 46 : } 47 : 48 : /** Returns all {@link ProjectData} in the hierarchy starting with the current one. */ 49 : public ImmutableList<ProjectData> tree() { 50 7 : List<ProjectData> parents = new ArrayList<>(); 51 7 : Optional<ProjectData> curr = Optional.of(this); 52 7 : while (curr.isPresent()) { 53 7 : parents.add(curr.get()); 54 7 : curr = curr.get().parent; 55 : } 56 7 : return ImmutableList.copyOf(parents); 57 : } 58 : 59 : public ImmutableList<String> getParentNames() { 60 7 : return tree().stream().skip(1).map(p -> p.getProject().getName()).collect(toImmutableList()); 61 : } 62 : 63 : @Override 64 : public String toString() { 65 2 : MoreObjects.ToStringHelper h = MoreObjects.toStringHelper(this); 66 2 : h.addValue(project.getName()); 67 2 : return h.toString(); 68 : } 69 : }