Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CompositeMap |
|
| 2.5;2,5 |
1 | package org.apache.maven.shared.filtering; | |
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.AbstractMap; | |
23 | import java.util.ArrayList; | |
24 | import java.util.Iterator; | |
25 | import java.util.List; | |
26 | import java.util.Map; | |
27 | import java.util.Set; | |
28 | ||
29 | /** | |
30 | * A Map composed with some others (optional adding SystemProperties and envvar). | |
31 | * The get method look in the Map list to return the corresponding value. | |
32 | * | |
33 | * @author <a href="mailto:olamy@apache.org">olamy</a> | |
34 | * @version $Id: CompositeMap.java 1055685 2011-01-05 23:14:08Z dennisl $ | |
35 | */ | |
36 | public class CompositeMap | |
37 | extends AbstractMap | |
38 | { | |
39 | ||
40 | private List /*Map*/maps; | |
41 | ||
42 | private boolean systemPropertiesFirst; | |
43 | ||
44 | /** | |
45 | * | |
46 | * @param maps | |
47 | */ | |
48 | public CompositeMap( List /* Map */maps ) | |
49 | { | |
50 | 0 | this( maps, false, false ); |
51 | 0 | } |
52 | ||
53 | /** | |
54 | * @param maps an ordered {@link List} of {@link Map} | |
55 | * @param useSystemProperties using or not the System Properties | |
56 | * @param systemPropertiesFirst if with get( key ) the sysProps must win (the internal ordered {@link List} | |
57 | * will have in first the System Properties) | |
58 | */ | |
59 | public CompositeMap( List /*Map*/maps, boolean useSystemProperties, boolean systemPropertiesFirst ) | |
60 | 0 | { |
61 | 0 | this.systemPropertiesFirst = systemPropertiesFirst; |
62 | 0 | if ( systemPropertiesFirst && !useSystemProperties ) |
63 | { | |
64 | 0 | throw new IllegalArgumentException( "systemPropertiesFirst can't be true if useSystemProperties is false" ); |
65 | } | |
66 | 0 | this.maps = new ArrayList(); |
67 | 0 | if ( useSystemProperties && !systemPropertiesFirst ) |
68 | { | |
69 | 0 | if ( maps != null ) |
70 | { | |
71 | 0 | this.maps.addAll( maps ); |
72 | } | |
73 | 0 | this.maps.add( System.getProperties() ); |
74 | } | |
75 | 0 | else if ( useSystemProperties && systemPropertiesFirst ) |
76 | { | |
77 | 0 | this.maps.add( System.getProperties() ); |
78 | 0 | if ( maps != null ) |
79 | { | |
80 | 0 | this.maps.addAll( maps ); |
81 | } | |
82 | } | |
83 | else | |
84 | { | |
85 | 0 | if ( maps != null ) |
86 | { | |
87 | 0 | this.maps.addAll( maps ); |
88 | } | |
89 | } | |
90 | 0 | } |
91 | ||
92 | public Object get( Object key ) | |
93 | { | |
94 | 0 | if ( this.maps != null ) |
95 | { | |
96 | 0 | for ( Iterator iterator = this.maps.iterator(); iterator.hasNext(); ) |
97 | { | |
98 | 0 | Map map = (Map) iterator.next(); |
99 | 0 | Object value = map.get( key ); |
100 | 0 | if ( value != null ) |
101 | { | |
102 | 0 | return value; |
103 | } | |
104 | 0 | } |
105 | } | |
106 | 0 | return null; |
107 | } | |
108 | ||
109 | /** | |
110 | * @see java.util.AbstractMap#entrySet() | |
111 | */ | |
112 | public Set entrySet() | |
113 | { | |
114 | 0 | throw new UnsupportedOperationException( "Cannot enumerate properties in a composite map" ); |
115 | } | |
116 | ||
117 | public List getMaps() | |
118 | { | |
119 | 0 | return maps; |
120 | } | |
121 | ||
122 | public void addMap( Map map ) | |
123 | { | |
124 | // see constructors internal Map can't be null | |
125 | 0 | this.maps.add( map ); |
126 | 0 | } |
127 | ||
128 | public boolean isSystemPropertiesFirst() | |
129 | { | |
130 | 0 | return systemPropertiesFirst; |
131 | } | |
132 | ||
133 | public void setSystemPropertiesFirst( boolean systemPropertiesFirst ) | |
134 | { | |
135 | 0 | this.systemPropertiesFirst = systemPropertiesFirst; |
136 | 0 | } |
137 | ||
138 | } |