Coverage Report - org.apache.maven.archiva.database.browsing.GroupIdFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupIdFilter
0%
0/25
0%
0/10
0
GroupIdFilter$GroupTreeNode
0%
0/14
0%
0/2
0
 
 1  
 package org.apache.maven.archiva.database.browsing;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 import java.util.StringTokenizer;
 26  
 import java.util.TreeMap;
 27  
 
 28  
 /**
 29  
  * GroupIdFilter - utility methods for filtering groupIds. 
 30  
  *
 31  
  * @version $Id: GroupIdFilter.java 718864 2008-11-19 06:33:35Z brett $
 32  
  */
 33  0
 public class GroupIdFilter
 34  
 {
 35  
     private static final String GROUP_SEPARATOR = ".";
 36  
 
 37  
     /**
 38  
      * <p>
 39  
      * Filter out excessive groupId naming. (to provide a tree-ish view of the list of groupIds).
 40  
      * </p>
 41  
      * 
 42  
      * <pre>
 43  
      *  // Input List
 44  
      *  commons-lang
 45  
      *  com.jsch
 46  
      *  org.apache.apache
 47  
      *  org.apache.maven
 48  
      *  org.codehaus.modello
 49  
      *  // Filtered List
 50  
      *  commons-lang
 51  
      *  com.jsch
 52  
      *  org
 53  
      * </pre>
 54  
      * 
 55  
      * <pre>
 56  
      *  // Input List
 57  
      *  commons-lang
 58  
      *  commons-io
 59  
      *  commons-pool
 60  
      *  com.jsch
 61  
      *  com.jsch.lib
 62  
      *  com.jsch.providers
 63  
      *  org.apache.apache
 64  
      *  org.apache.maven
 65  
      *  org.apache.maven.archiva
 66  
      *  org.apache.maven.shared
 67  
      *  // Filtered List
 68  
      *  commons-lang
 69  
      *  commons-io
 70  
      *  commons-pool
 71  
      *  com.jsch
 72  
      *  org.apache
 73  
      * </pre>
 74  
      * 
 75  
      * @param groups the list of groupIds.
 76  
      * @return
 77  
      */
 78  
     public static List<String> filterGroups( List<String> groups )
 79  
     {
 80  0
         GroupTreeNode tree = buildGroupTree( groups );
 81  0
         return collateGroups( tree );
 82  
     }
 83  
 
 84  
     public static GroupTreeNode buildGroupTree( List<String> groups )
 85  
     {
 86  0
         GroupTreeNode rootNode = new GroupTreeNode();
 87  
 
 88  
         // build a tree structure
 89  0
         for ( String groupId : groups )
 90  
         {
 91  0
             StringTokenizer tok = new StringTokenizer( groupId, GROUP_SEPARATOR );
 92  
 
 93  0
             GroupTreeNode node = rootNode;
 94  
 
 95  0
             while ( tok.hasMoreTokens() )
 96  
             {
 97  0
                 String part = tok.nextToken();
 98  
 
 99  0
                 if ( !node.getChildren().containsKey( part ) )
 100  
                 {
 101  0
                     GroupTreeNode newNode = new GroupTreeNode( part, node );
 102  0
                     node.addChild( newNode );
 103  0
                     node = newNode;
 104  0
                 }
 105  
                 else
 106  
                 {
 107  0
                     node = node.getChildren().get( part );
 108  
                 }
 109  0
             }
 110  0
         }
 111  
 
 112  0
         return rootNode;
 113  
     }
 114  
 
 115  
     private static List<String> collateGroups( GroupTreeNode rootNode )
 116  
     {
 117  0
         List<String> groups = new ArrayList<String>();
 118  0
         for ( GroupTreeNode node : rootNode.getChildren().values() )
 119  
         {
 120  0
             while ( node.getChildren().size() == 1 )
 121  
             {
 122  0
                 node = node.getChildren().values().iterator().next();
 123  
             }
 124  
 
 125  0
             groups.add( node.getFullName() );
 126  
         }
 127  0
         return groups;
 128  
     }
 129  
 
 130  0
     private static class GroupTreeNode
 131  
     {
 132  
         private final String name;
 133  
 
 134  
         private final String fullName;
 135  
 
 136  0
         private final Map<String, GroupTreeNode> children = new TreeMap<String, GroupTreeNode>();
 137  
 
 138  
         GroupTreeNode()
 139  0
         {
 140  0
             name = null;
 141  0
             fullName = null;
 142  0
         }
 143  
 
 144  
         GroupTreeNode( String name, GroupTreeNode parent )
 145  0
         {
 146  0
             this.name = name;
 147  0
             this.fullName = parent.fullName != null ? parent.fullName + GROUP_SEPARATOR + name : name;
 148  0
         }
 149  
 
 150  
         public String getName()
 151  
         {
 152  0
             return name;
 153  
         }
 154  
 
 155  
         public String getFullName()
 156  
         {
 157  0
             return fullName;
 158  
         }
 159  
 
 160  
         public Map<String, GroupTreeNode> getChildren()
 161  
         {
 162  0
             return children;
 163  
         }
 164  
 
 165  
         public void addChild( GroupTreeNode newNode )
 166  
         {
 167  0
             children.put( newNode.name, newNode );
 168  0
         }
 169  
     }
 170  
 }