Carsten Ziegeler | 7390390 | 2013-01-30 21:09:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.scrplugin.helper; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.List; |
| 24 | |
| 25 | public abstract class ComponentContainerUtil { |
| 26 | |
| 27 | public static class ComponentContainerContainer { |
| 28 | public List<ComponentContainer> components; |
| 29 | public String className; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Split the list of components into separate lists depending |
| 34 | * on the configuration. |
| 35 | */ |
Carsten Ziegeler | e5fef1c | 2013-07-17 12:32:32 +0000 | [diff] [blame] | 36 | public static List<ComponentContainerContainer> split(final List<ComponentContainer> components) { |
Carsten Ziegeler | 7390390 | 2013-01-30 21:09:50 +0000 | [diff] [blame] | 37 | final List<ComponentContainerContainer> result = new ArrayList<ComponentContainerContainer>(); |
| 38 | |
Carsten Ziegeler | e5fef1c | 2013-07-17 12:32:32 +0000 | [diff] [blame] | 39 | while ( !components.isEmpty() ) { |
| 40 | // get the first component |
| 41 | final List<ComponentContainer> innerList = new ArrayList<ComponentContainer>(); |
| 42 | final ComponentContainer component = components.remove(0); |
| 43 | innerList.add(component); |
| 44 | final int pos = component.getClassDescription().getDescribedClass().getName().indexOf('$'); |
| 45 | final String baseClassName; |
| 46 | if ( pos == -1 ) { |
| 47 | baseClassName = component.getClassDescription().getDescribedClass().getName(); |
| 48 | } else { |
| 49 | baseClassName = component.getClassDescription().getDescribedClass().getName().substring(0, pos); |
Carsten Ziegeler | 7390390 | 2013-01-30 21:09:50 +0000 | [diff] [blame] | 50 | } |
Carsten Ziegeler | e5fef1c | 2013-07-17 12:32:32 +0000 | [diff] [blame] | 51 | final String baseClassPrefix = baseClassName + '$'; |
| 52 | |
| 53 | // check for inner classes |
| 54 | final Iterator<ComponentContainer> i = components.iterator(); |
| 55 | while ( i.hasNext() ) { |
| 56 | final ComponentContainer cc = i.next(); |
| 57 | if ( cc.getClassDescription().getDescribedClass().getName().startsWith(baseClassPrefix) ) { |
| 58 | innerList.add(cc); |
| 59 | i.remove(); |
| 60 | } |
Carsten Ziegeler | 7390390 | 2013-01-30 21:09:50 +0000 | [diff] [blame] | 61 | } |
Carsten Ziegeler | e5fef1c | 2013-07-17 12:32:32 +0000 | [diff] [blame] | 62 | |
| 63 | final ComponentContainerContainer ccc = new ComponentContainerContainer(); |
| 64 | ccc.components = innerList; |
| 65 | ccc.className = baseClassName; |
| 66 | result.add(ccc); |
Carsten Ziegeler | 7390390 | 2013-01-30 21:09:50 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return result; |
| 70 | } |
| 71 | } |