View Javadoc

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