Line data Source code
1 : // Copyright (C) 2011 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.restapi.project; 16 : 17 : import com.google.gerrit.entities.Project; 18 : import com.google.gerrit.server.config.AllProjectsName; 19 : import com.google.gerrit.server.util.TreeFormatter.TreeNode; 20 : import com.google.inject.Inject; 21 : import com.google.inject.assistedinject.Assisted; 22 : import java.util.NavigableSet; 23 : import java.util.TreeSet; 24 : 25 : /** Node of a Project in a tree formatted by {@link ListProjects}. */ 26 : public class ProjectNode implements TreeNode, Comparable<ProjectNode> { 27 : public interface Factory { 28 : ProjectNode create(Project project, boolean isVisible); 29 : } 30 : 31 : private final AllProjectsName allProjectsName; 32 : private final Project project; 33 : private final boolean isVisible; 34 : 35 0 : private final NavigableSet<ProjectNode> children = new TreeSet<>(); 36 : 37 : @Inject 38 : protected ProjectNode( 39 : final AllProjectsName allProjectsName, 40 : @Assisted final Project project, 41 0 : @Assisted final boolean isVisible) { 42 0 : this.allProjectsName = allProjectsName; 43 0 : this.project = project; 44 0 : this.isVisible = isVisible; 45 0 : } 46 : 47 : /** 48 : * Returns the project parent name. 49 : * 50 : * @return Project parent name, {@code null} for the 'All-Projects' root project 51 : */ 52 : Project.NameKey getParentName() { 53 0 : return project.getParent(allProjectsName); 54 : } 55 : 56 : boolean isAllProjects() { 57 0 : return allProjectsName.equals(project.getNameKey()); 58 : } 59 : 60 : Project getProject() { 61 0 : return project; 62 : } 63 : 64 : @Override 65 : public String getDisplayName() { 66 0 : return project.getName(); 67 : } 68 : 69 : @Override 70 : public boolean isVisible() { 71 0 : return isVisible; 72 : } 73 : 74 : @Override 75 : public NavigableSet<? extends ProjectNode> getChildren() { 76 0 : return children; 77 : } 78 : 79 : void addChild(ProjectNode child) { 80 0 : children.add(child); 81 0 : } 82 : 83 : @Override 84 : public int compareTo(ProjectNode o) { 85 0 : return project.getNameKey().compareTo(o.project.getNameKey()); 86 : } 87 : }