001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.util; 029 030import java.util.ArrayList; 031import java.util.List; 032 033/** 034 * Data structure which stores contents indexed by path. 035 * 036 * @param <V> the element type of the path map 037 */ 038public class CmsPathMap<V> { 039 040 /** The tree used for storing the contents. */ 041 private CmsPathTree<String, V> m_tree = new CmsPathTree<String, V>(); 042 043 /** 044 * Adds an element for the given path, overwriting the previous element for that path if there is one.<p> 045 * 046 * @param path the path 047 * @param value the element to add 048 */ 049 public void add(String path, V value) { 050 051 m_tree.setValue(splitPath(path), value); 052 } 053 054 /** 055 * Gets the values for the direct children of the given path.<p> 056 * 057 * @param path the path 058 * @return the child values 059 */ 060 public List<V> getChildValues(String path) { 061 062 return m_tree.getChildValues(splitPath(path)); 063 } 064 065 /** 066 * Gets the values for the descendants of the path, including the path itself.<p> 067 * 068 * @param path the path 069 * @return the descendant values for the path 070 */ 071 public List<V> getDescendantValues(String path) { 072 073 return m_tree.getDescendantValues(splitPath(path)); 074 } 075 076 /** 077 * Converts a path into list form.<p> 078 * 079 * @param path the path to convert 080 * @return the list of the path elements 081 */ 082 private List<String> splitPath(String path) { 083 084 List<String> result = new ArrayList<String>(); 085 for (String token : path.split("/")) { 086 if ("".equals(token)) { 087 continue; 088 } 089 result.add(token); 090 } 091 return result; 092 } 093 094}