Coverage report

  %line %branch
org.apache.jetspeed.page.document.impl.NodeSetImpl
0% 
0% 

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  * 
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.jetspeed.page.document.impl;
 18  
 
 19  
 import java.util.Comparator;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 import java.util.TreeMap;
 24  
 import java.util.regex.Pattern;
 25  
 
 26  
 import org.apache.commons.collections.map.LRUMap;
 27  
 import org.apache.jetspeed.page.document.Node;
 28  
 import org.apache.jetspeed.page.document.NodeSet;
 29  
 
 30  
 /**
 31  
  * NodeSetImpl
 32  
  *
 33  
  * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
 34  
  * @version $Id$
 35  
  */
 36  
 public class NodeSetImpl implements NodeSet
 37  
 {
 38  0
     public static final NodeSetImpl EMPTY_NODE_SET = new NodeSetImpl();
 39  
 
 40  0
     private static final Map patternCache = new LRUMap(128);
 41  
 
 42  
     private Map nodes;
 43  
     private Comparator comparator;
 44  
 
 45  
     public NodeSetImpl(List nodes, Comparator comparator)
 46  0
     {
 47  0
         this.nodes = new TreeMap(comparator);        
 48  0
         Object[] nodeToCopy = nodes.toArray();
 49  0
         for (int ix = 0; ix < nodeToCopy.length; ix++)
 50  
         {
 51  0
             Node node = (Node)nodeToCopy[ix];
 52  0
             if (!this.nodes.containsKey( node.getName()))
 53  
             {
 54  0
                 this.nodes.put(node.getName(), node);
 55  
             }
 56  
         }         
 57  0
         this.comparator = comparator;
 58  0
     }
 59  
 
 60  
     public NodeSetImpl(List nodes)
 61  
     {
 62  0
         this(nodes, null);
 63  0
     }
 64  
 
 65  
     public NodeSetImpl(Comparator comparator)
 66  0
     {
 67  0
         this.comparator = comparator;
 68  0
     }
 69  
 
 70  
     public NodeSetImpl(NodeSet nodeSet)
 71  
     {
 72  0
         this((nodeSet instanceof NodeSetImpl) ? ((NodeSetImpl)nodeSet).comparator : (Comparator)null);
 73  0
     }
 74  
 
 75  
     public NodeSetImpl()
 76  0
     {
 77  0
     }
 78  
 
 79  
     /**
 80  
      * getCachedPattern
 81  
      *
 82  
      * @param regex pattern
 83  
      * @return cached pattern
 84  
      */
 85  
     private Pattern getCachedPattern(String regex)
 86  
     {
 87  0
         synchronized (patternCache)
 88  
         {
 89  0
             if (patternCache.containsKey(regex))
 90  
             {
 91  0
                 return (Pattern)patternCache.get(regex);
 92  
             }
 93  
             else
 94  
             {
 95  0
                 Pattern pattern = Pattern.compile(regex);
 96  0
                 patternCache.put(regex, pattern);
 97  0
                 return pattern;
 98  
             }
 99  0
         }
 100  
     }
 101  
 
 102  
     /* (non-Javadoc)
 103  
      * @see org.apache.jetspeed.page.document.NodeSet#add(org.apache.jetspeed.page.document.Node)
 104  
      */
 105  
     public void add(Node node)
 106  
     {
 107  0
         if (nodes == null)
 108  
         {
 109  0
             nodes = new TreeMap(comparator);
 110  
         }
 111  0
         if (!nodes.containsKey(node.getName()))
 112  
         {
 113  0
             nodes.put(node.getName(), node);
 114  
         }
 115  0
     }
 116  
     
 117  
     /* (non-Javadoc)
 118  
      * @see org.apache.jetspeed.page.document.NodeSet#get(java.lang.String)
 119  
      */
 120  
     public Node get(String name)
 121  
     {
 122  0
         if (nodes != null)
 123  
         {
 124  0
             return (Node)nodes.get(name);
 125  
         }
 126  0
         return null;
 127  
     }
 128  
     
 129  
     /* (non-Javadoc)
 130  
      * @see org.apache.jetspeed.page.document.NodeSet#iterator()
 131  
      */
 132  
     public Iterator iterator()
 133  
     {
 134  0
         if (nodes == null)
 135  
         {
 136  0
             nodes = new TreeMap(comparator);
 137  
         }
 138  0
         return nodes.values().iterator();
 139  
     }
 140  
     
 141  
     /* (non-Javadoc)
 142  
      * @see org.apache.jetspeed.page.document.NodeSet#subset(java.lang.String)
 143  
      */
 144  
     public NodeSet subset(String type)
 145  
     {
 146  0
         NodeSetImpl subset = new NodeSetImpl(comparator);
 147  0
         Iterator nodeItr = iterator();
 148  0
         while (nodeItr.hasNext())
 149  
         {
 150  0
             Node node = (Node) nodeItr.next();
 151  0
             if (node.getType().equals(type))
 152  
             {
 153  0
                 subset.add(node);
 154  
             }
 155  0
         }
 156  0
         return subset;
 157  
     }
 158  
     
 159  
     /* (non-Javadoc)
 160  
      * @see org.apache.jetspeed.page.document.NodeSet#inclusiveSubset(java.lang.String)
 161  
      */
 162  
     public NodeSet inclusiveSubset(String regex)
 163  
     {
 164  0
         Pattern pattern = getCachedPattern(regex);
 165  0
         NodeSetImpl subset = new NodeSetImpl(comparator);
 166  0
         Iterator nodeItr = iterator();
 167  0
         while (nodeItr.hasNext())
 168  
         {
 169  0
             Node node = (Node) nodeItr.next();
 170  0
             if (pattern.matcher(node.getName()).matches())
 171  
             {
 172  0
                 subset.add(node);
 173  
             }
 174  0
         }
 175  0
         return subset;
 176  
     }
 177  
     
 178  
     /* (non-Javadoc)
 179  
      * @see org.apache.jetspeed.page.document.NodeSet#exclusiveSubset(java.lang.String)
 180  
      */
 181  
     public NodeSet exclusiveSubset(String regex)
 182  
     {
 183  0
         Pattern pattern = getCachedPattern(regex);
 184  0
         NodeSetImpl subset = new NodeSetImpl(comparator);
 185  0
         Iterator nodeItr = iterator();
 186  0
         while (nodeItr.hasNext())
 187  
         {
 188  0
             Node node = (Node) nodeItr.next();
 189  0
             if (!pattern.matcher(node.getName()).matches())
 190  
             {
 191  0
                 subset.add(node);
 192  
             }
 193  0
         }
 194  0
         return subset;
 195  
     }
 196  
     
 197  
     /* (non-Javadoc)
 198  
      * @see org.apache.jetspeed.page.document.NodeSet#size()
 199  
      */
 200  
     public int size()
 201  
     {
 202  0
         if (nodes != null)
 203  
         {
 204  0
             return nodes.size();
 205  
         }
 206  0
         return 0;
 207  
     }
 208  
     
 209  
     /* (non-Javadoc)
 210  
      * @see org.apache.jetspeed.page.document.NodeSet#contains(org.apache.jetspeed.page.document.Node)
 211  
      */
 212  
     public boolean contains(Node node)
 213  
     {
 214  0
         if (nodes != null)
 215  
         {
 216  0
             return nodes.containsValue(node);
 217  
         }
 218  0
         return false;
 219  
     }
 220  
     
 221  
     /* (non-Javadoc)
 222  
      * @see org.apache.jetspeed.page.document.NodeSet#isEmpty()
 223  
      */
 224  
     public boolean isEmpty()
 225  
     {
 226  0
         if (nodes != null)
 227  
         {
 228  0
             return nodes.isEmpty();
 229  
         }
 230  0
         return true;
 231  
     }
 232  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.