View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
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 de.smartics.properties.spi.config.support;
17  
18  import java.util.Properties;
19  
20  import javax.annotation.concurrent.NotThreadSafe;
21  
22  import de.smartics.properties.api.config.domain.Property;
23  import de.smartics.properties.api.config.domain.PropertyCollection;
24  import de.smartics.properties.api.config.domain.PropertyLocation;
25  import de.smartics.properties.api.config.domain.PropertyProvider;
26  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
27  import de.smartics.util.lang.Arg;
28  
29  /**
30   * Provides an adapter for {@link Properties} instances to match the
31   * {@link PropertyProvider} interface.
32   */
33  @NotThreadSafe
34  public final class PropertiesPropertyProvider implements PropertyProvider
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    /**
41     * The class version identifier.
42     * <p>
43     * The value of this constant is {@value}.
44     */
45    private static final long serialVersionUID = 1L;
46  
47    // --- members --------------------------------------------------------------
48  
49    /**
50     * The key of the configuration whose properties are provided.
51     *
52     * @serial
53     */
54    private final ConfigurationKey<?> configurationKey;
55  
56    /**
57     * The unique identifier of the physical source of the properties.
58     *
59     * @serial
60     */
61    private final PropertyLocation sourceId;
62  
63    /**
64     * The wrapped properties.
65     *
66     * @serial
67     */
68    private final Properties properties;
69  
70    /**
71     * Default constructor.
72     *
73     * @param configurationKey the key of the configuration whose properties are
74     *          provided.
75     * @param sourceId the unique identifier of the physical source of the
76     *          properties.
77     * @param properties the wrapped properties.
78     * @throws NullPointerException if either {@code configurationKey} ,
79     *           {@code sourceId} or {@code properties} is <code>null</code>.
80     */
81    public PropertiesPropertyProvider(final ConfigurationKey<?> configurationKey,
82        final PropertyLocation sourceId, final Properties properties)
83      throws NullPointerException
84    {
85      this.configurationKey =
86          Arg.checkNotNull("configurationKey", configurationKey);
87      this.sourceId = Arg.checkNotNull("sourceId", sourceId);
88      this.properties = Arg.checkNotNull("properties", properties);
89    }
90  
91    // ****************************** Initializer *******************************
92  
93    // ****************************** Constructors ******************************
94  
95    // ****************************** Inner Classes *****************************
96  
97    // ********************************* Methods ********************************
98  
99    // --- init -----------------------------------------------------------------
100 
101   // --- get&set --------------------------------------------------------------
102 
103   @Override
104   public ConfigurationKey<?> getConfigurationKey()
105   {
106     return configurationKey;
107   }
108 
109   @Override
110   public PropertyLocation getSourceId()
111   {
112     return sourceId;
113   }
114 
115   @Override
116   public boolean isLazy()
117   {
118     return false;
119   }
120 
121   // --- business -------------------------------------------------------------
122 
123   @Override
124   public Property getProperty(final String name) throws NullPointerException,
125     IllegalArgumentException
126   {
127     Arg.checkNotNull("name", name);
128     final Property property = ensureProperty(name, properties.get(name));
129     return property;
130   }
131 
132   private Property ensureProperty(final String name, final Object value)
133   {
134     if (value instanceof Property)
135     {
136       return (Property) value;
137     }
138     final Property property = new Property(sourceId, name, value);
139     return property;
140   }
141 
142   @Override
143   public PropertyCollection getProperties()
144   {
145     return new PropertiesPropertyCollection(properties);
146   }
147 
148   @Override
149   public boolean containsKey(final String name) throws NullPointerException
150   {
151     Arg.checkNotNull("name", name);
152     return properties.containsKey(name);
153   }
154 
155   // --- object basics --------------------------------------------------------
156 
157   /**
158    * Returns the string representation of the object.
159    *
160    * @return the string representation of the object.
161    */
162   @Override
163   public String toString()
164   {
165     final StringBuilder buffer = new StringBuilder();
166 
167     buffer.append(sourceId).append('=').append(properties);
168 
169     return buffer.toString();
170   }
171 }