/** * @param v the vertex to test * @return <code>true</code> if {@code v} has no parent */ public boolean isRoot(V v) { if (!delegate.containsVertex(v)) return false; return getParent(v) == null; }
/** * computes whether the passed node is a root node (has no children) */ public boolean isRoot(V v) { if (!delegate.containsVertex(v)) { return false; } return getParent(v) == null; }
public ClusterVO getParent(Integer tenantId, ClusterVO cluster) { DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); // root has no parent if (tree.isRoot(cluster)) return null; return tree.getParent(cluster); }
/** * Returns an ordered list of the nodes beginning at the root * and ending at {@code vertex}, including all intermediate * nodes. * @param vertex the last node in the path from the root * @return an ordered list of the nodes from root to child */ public List<V> getPath(V vertex) { if (!delegate.containsVertex(vertex)) return null; List<V> vertex_to_root = new ArrayList<V>(); vertex_to_root.add(vertex); V parent = getParent(vertex); while(parent != null) { vertex_to_root.add(parent); parent = getParent(parent); } // reverse list so that it goes from root to child List<V> root_to_vertex = new ArrayList<V>(vertex_to_root.size()); for (int i = vertex_to_root.size() - 1; i >= 0; i--) root_to_vertex.add(vertex_to_root.get(i)); return root_to_vertex; }
/** * Returns an ordered list of the nodes beginning at the root and ending at * {@code vertex}, including all intermediate nodes. * * @param vertex * the last node in the path from the root * @return an ordered list of the nodes from root to child */ public List<V> getPath(V vertex) { if (!delegate.containsVertex(vertex)) { return null; } List<V> vertex_to_root = new ArrayList<V>(); vertex_to_root.add(vertex); V parent = getParent(vertex); while (parent != null) { vertex_to_root.add(parent); parent = getParent(parent); } // reverse list so that it goes from root to child List<V> root_to_vertex = new ArrayList<V>(vertex_to_root.size()); for (int i = vertex_to_root.size() - 1; i >= 0; i--) { root_to_vertex.add(vertex_to_root.get(i)); } return root_to_vertex; }
public List<ClusterVO> getSiblings(Integer tenantId, ClusterVO cluster) { DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); if (tree.isRoot(cluster)) return null; //root has no siblings ClusterVO parent = tree.getParent(cluster); Collection<ClusterVO> sibl = tree.getChildren(parent); List<ClusterVO> siblings = new ArrayList<ClusterVO>(); for (ClusterVO clusterVO : sibl) { if (!clusterVO.equals(cluster)) siblings.add(clusterVO); } return siblings; }
public void renameCluster(Integer tenantId, String clusterName, String newName) throws ClusterException { ClusterVO cluster = loadCluster(tenantId, idMappingDAO.lookup(clusterName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER)); DelegateTree<ClusterVO, ItemAssocVO<Integer,Integer>> tree = clusters.get(tenantId); if (tree.isRoot(cluster)) throw new ClusterException("ROOT cluster cannot be renamed!"); ClusterVO parent = tree.getParent(cluster); ClusterVO newCluster = new ClusterVO(tenantId, idMappingDAO.lookup(newName), itemTypeDAO.getIdOfType(tenantId, ClusterService.ITEMTYPE_CLUSTER), newName, cluster.getDescription()); // add the new cluster addCluster(newCluster, parent); // move all children to the new cluster for (ClusterVO child : tree.getChildren(cluster)) { moveCluster(tenantId, child.getName(), newName); } if (logger.isDebugEnabled()) { logger.debug("removed all children: " + tree.isLeaf(cluster)); } // move all items to the new cluster for (ItemVO<Integer, Integer> item : getItemsOfCluster(cluster)) { addItemToCluster(newCluster, item); } // delete the old cluster removeCluster(tenantId, clusterName); }