View Javadoc

1   /*
2      Copyright 2007 Ivo Limmen
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.limmen.crs;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.limmen.crs.model.Configuration;
24  
25  public class BaseConfigurationIndex {
26  
27  	private static List<String> allConfigurations =
28  		new ArrayList<String>();
29  
30  	private static Map<String, List<String>> baseConfigurations =
31  		new HashMap<String, List<String>>();
32  
33  	public static void addToIndex(Configuration configuration) {
34  
35  		if (configuration.getBaseConfiguration() != null) {
36  
37  			List<String> derivatives = baseConfigurations.get(configuration.getBaseConfiguration().getId());
38  
39  			if (derivatives == null) {
40  
41  				derivatives = new ArrayList<String>();
42  			}
43  
44  			if (!derivatives.contains(configuration.getId())) {
45  
46  				// add this configuration as a derivative
47  				derivatives.add(configuration.getId());
48  			}
49  
50  			baseConfigurations.put(configuration.getBaseConfiguration().getId(), derivatives);
51  		}
52  
53  		if (!allConfigurations.contains(configuration.getId())) {
54  
55  			allConfigurations.add(configuration.getId());
56  		}
57  	}
58  
59  	public static String getBaseConfiguration(String configurationId) {
60  
61  		for (String baseConfiguration : baseConfigurations.keySet()) {
62  
63  			if (baseConfigurations.get(baseConfiguration).contains(configurationId)) {
64  
65  				return baseConfiguration;
66  			}
67  		}
68  
69  		return null;
70  	}
71  
72  	public static List<String> getConfigurationList() {
73  
74  		return allConfigurations;
75  	}
76  
77  	public static List<String> getDerivativesOfBase(String aConfigurationId) {
78  
79  		return baseConfigurations.get(aConfigurationId);
80  	}
81  
82  	public static Map<String, List<String>> getHierarchialList() {
83  
84  		return baseConfigurations;
85  	}
86  
87  	public static void removeFromIndex(Configuration configuration) {
88  
89  		if (configuration.getBaseConfiguration() != null) {
90  
91  			List<String> derivatives = baseConfigurations.get(configuration.getBaseConfiguration().getId());
92  
93  			if (derivatives != null) {
94  
95  				derivatives.remove(configuration.getId());
96  			}
97  		}
98  
99  		if (allConfigurations.contains(configuration.getId())) {
100 
101 			allConfigurations.remove(configuration.getId());
102 		}
103 	}
104 
105 	private BaseConfigurationIndex() {
106 
107 		super();
108    }
109 }