View Javadoc

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          this( maps, false, false );
51      }
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      {
61          this.systemPropertiesFirst = systemPropertiesFirst;
62          if ( systemPropertiesFirst && !useSystemProperties )
63          {
64              throw new IllegalArgumentException( "systemPropertiesFirst can't be true if useSystemProperties is false" );
65          }
66          this.maps = new ArrayList();
67          if ( useSystemProperties && !systemPropertiesFirst )
68          {
69              if ( maps != null )
70              {
71                  this.maps.addAll( maps );
72              }
73              this.maps.add( System.getProperties() );
74          }
75          else if ( useSystemProperties && systemPropertiesFirst )
76          {
77              this.maps.add( System.getProperties() );
78              if ( maps != null )
79              {
80                  this.maps.addAll( maps );
81              }
82          }
83          else
84          {
85              if ( maps != null )
86              {
87                  this.maps.addAll( maps );
88              }
89          }
90      }
91  
92      public Object get( Object key )
93      {
94          if ( this.maps != null )
95          {
96              for ( Iterator iterator = this.maps.iterator(); iterator.hasNext(); )
97              {
98                  Map map = (Map) iterator.next();
99                  Object value = map.get( key );
100                 if ( value != null )
101                 {
102                     return value;
103                 }
104             }
105         }
106         return null;
107     }
108 
109     /** 
110      * @see java.util.AbstractMap#entrySet()
111      */
112     public Set entrySet()
113     {
114         throw new UnsupportedOperationException( "Cannot enumerate properties in a composite map" );
115     }
116 
117     public List getMaps()
118     {
119         return maps;
120     }
121 
122     public void addMap( Map map )
123     {
124         // see constructors internal Map can't be null
125         this.maps.add( map );
126     }
127 
128     public boolean isSystemPropertiesFirst()
129     {
130         return systemPropertiesFirst;
131     }
132 
133     public void setSystemPropertiesFirst( boolean systemPropertiesFirst )
134     {
135         this.systemPropertiesFirst = systemPropertiesFirst;
136     }
137 
138 }