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.Arguments;
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      Arguments.checkNotNull("configurationKey", configurationKey);
86      Arguments.checkNotNull("sourceId", sourceId);
87      Arguments.checkNotNull("properties", properties);
88  
89      this.configurationKey = configurationKey;
90      this.sourceId = sourceId;
91      this.properties = properties;
92    }
93  
94    // ****************************** Initializer *******************************
95  
96    // ****************************** Constructors ******************************
97  
98    // ****************************** Inner Classes *****************************
99  
100   // ********************************* Methods ********************************
101 
102   // --- init -----------------------------------------------------------------
103 
104   // --- get&set --------------------------------------------------------------
105 
106   @Override
107   public ConfigurationKey getConfigurationKey()
108   {
109     return configurationKey;
110   }
111 
112   @Override
113   public PropertyLocation getSourceId()
114   {
115     return sourceId;
116   }
117 
118   // --- business -------------------------------------------------------------
119 
120   @Override
121   public Property getProperty(final String name) throws NullPointerException,
122     IllegalArgumentException
123   {
124     Arguments.checkNotNull("name", name);
125     final String value = properties.getProperty(name);
126     final Property property = new Property(sourceId, name, value);
127     return property;
128   }
129 
130   @Override
131   public PropertyCollection getProperties()
132   {
133     return new PropertiesPropertyCollection(properties);
134   }
135 
136   @Override
137   public boolean containsKey(final String name) throws NullPointerException
138   {
139     Arguments.checkNotNull("name", name);
140     return properties.containsKey(name);
141   }
142 
143   // --- object basics --------------------------------------------------------
144 
145   /**
146    * Returns the string representation of the object.
147    *
148    * @return the string representation of the object.
149    */
150   @Override
151   public String toString()
152   {
153     final StringBuilder buffer = new StringBuilder();
154 
155     buffer.append(sourceId).append('=').append(properties);
156 
157     return buffer.toString();
158   }
159 }