1 | |
package org.apache.onami.configuration.variables; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.Collection; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Map; |
25 | |
import java.util.Properties; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public final class VariablesMap implements Map<String, String> |
32 | |
{ |
33 | |
|
34 | |
|
35 | |
private final Parser parser; |
36 | |
|
37 | 52 | private final Map<String, Resolver> resolvers = new HashMap<String, Resolver>(); |
38 | |
|
39 | 52 | private final Map<String, String> data = new HashMap<String, String>(); |
40 | |
|
41 | |
public VariablesMap() |
42 | |
{ |
43 | 0 | this( new AntStyleParser() ); |
44 | 0 | } |
45 | |
|
46 | |
public VariablesMap( Parser parser ) |
47 | 52 | { |
48 | 52 | if ( parser == null ) |
49 | |
{ |
50 | 0 | throw new IllegalArgumentException( "Parser used to resolve input variables cannot be null." ); |
51 | |
} |
52 | 52 | this.parser = parser; |
53 | 52 | } |
54 | |
|
55 | |
public void clear() |
56 | |
{ |
57 | 2 | resolvers.clear(); |
58 | 2 | data.clear(); |
59 | 2 | } |
60 | |
|
61 | |
public boolean containsKey( Object key ) |
62 | |
{ |
63 | 2 | return data.containsKey( key ); |
64 | |
} |
65 | |
|
66 | |
public boolean containsValue( Object value ) |
67 | |
{ |
68 | 0 | return data.containsValue( value ); |
69 | |
} |
70 | |
|
71 | |
public Set<Entry<String, String>> entrySet() |
72 | |
{ |
73 | 2 | return data.entrySet(); |
74 | |
} |
75 | |
|
76 | |
public String get( Object key ) |
77 | |
{ |
78 | 152 | return data.get( key ); |
79 | |
} |
80 | |
|
81 | |
public boolean isEmpty() |
82 | |
{ |
83 | 0 | return data.isEmpty(); |
84 | |
} |
85 | |
|
86 | |
public Set<String> keySet() |
87 | |
{ |
88 | 0 | return data.keySet(); |
89 | |
} |
90 | |
|
91 | |
public String put( String key, String value ) |
92 | |
{ |
93 | 1868 | putValue( key, value ); |
94 | 1856 | resolveVariables(); |
95 | 1856 | return data.get( key ); |
96 | |
} |
97 | |
|
98 | |
public void putAll( Map<? extends String, ? extends String> t ) |
99 | |
{ |
100 | 0 | for ( Entry<? extends String, ? extends String> entry : t.entrySet() ) |
101 | |
{ |
102 | 0 | putValue( entry.getKey(), entry.getValue() ); |
103 | |
} |
104 | 0 | resolveVariables(); |
105 | 0 | } |
106 | |
|
107 | |
public void putAll( Properties properties ) |
108 | |
{ |
109 | 0 | for ( Entry<Object, Object> entry : properties.entrySet() ) |
110 | |
{ |
111 | 0 | putValue( entry.getKey().toString(), entry.getValue().toString() ); |
112 | |
} |
113 | 0 | resolveVariables(); |
114 | 0 | } |
115 | |
|
116 | |
private void putValue( String key, String value ) |
117 | |
{ |
118 | 1868 | data.put( key, value ); |
119 | |
|
120 | 1868 | Resolver resolver = parser.parse( value ); |
121 | 1856 | if ( resolver.needsResolving() ) |
122 | |
{ |
123 | 1136 | resolvers.put( key, resolver ); |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 720 | if ( resolvers.containsKey( key ) ) |
128 | |
{ |
129 | 0 | resolvers.remove( key ); |
130 | |
} |
131 | |
} |
132 | 1856 | } |
133 | |
|
134 | |
private void resolveVariables() |
135 | |
{ |
136 | 1858 | for ( Entry<String, Resolver> entry : resolvers.entrySet() ) |
137 | |
{ |
138 | 15806 | data.put( entry.getKey(), entry.getValue().resolve( data ) ); |
139 | |
} |
140 | 1858 | } |
141 | |
|
142 | |
public String remove( Object key ) |
143 | |
{ |
144 | 2 | String value = null; |
145 | 2 | if ( containsKey( key ) ) |
146 | |
{ |
147 | 2 | value = data.remove( key ); |
148 | 2 | resolvers.remove( key ); |
149 | 2 | resolveVariables(); |
150 | |
} |
151 | 2 | return value; |
152 | |
} |
153 | |
|
154 | |
public int size() |
155 | |
{ |
156 | 0 | return data.size(); |
157 | |
} |
158 | |
|
159 | |
public Collection<String> values() |
160 | |
{ |
161 | 0 | return data.values(); |
162 | |
} |
163 | |
|
164 | |
@Override |
165 | |
public String toString() |
166 | |
{ |
167 | 0 | return data.toString(); |
168 | |
} |
169 | |
|
170 | |
} |