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.core.metadata;
17  
18  import java.util.HashMap;
19  import java.util.Locale;
20  import java.util.Map;
21  
22  import javax.annotation.concurrent.NotThreadSafe;
23  
24  import org.apache.commons.lang.builder.ToStringBuilder;
25  
26  import de.smartics.properties.api.core.domain.PropertiesContext;
27  import de.smartics.properties.api.core.domain.PropertyComment;
28  import de.smartics.properties.api.core.domain.PropertyDescriptor;
29  import de.smartics.properties.spi.core.metadata.comment.PropertyCommentParser;
30  
31  /**
32   * Proxy to load property comments on demand.
33   */
34  @NotThreadSafe
35  public final class PropertyCommentProxy implements PropertyCommentProvider
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    /**
42     * The class version identifier.
43     * <p>
44     * The value of this constant is {@value}.
45     * </p>
46     */
47    private static final long serialVersionUID = 1L;
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * The properties context to fetch information from the META-INF folder. May
53     * be <code>null</code>.
54     *
55     * @serial
56     */
57    private final PropertiesContext context;
58  
59    /**
60     * The cached comments.
61     *
62     * @serial
63     */
64    private final Map<Locale, PropertyComment> comments;
65  
66    // ****************************** Initializer *******************************
67  
68    // ****************************** Constructors ******************************
69  
70    /**
71     * Default constructor. If the {@code context} is <code>null</code>, the proxy
72     * may only server {@link PropertyComment#EMPTY_COMMENT empty property
73     * comment} instances.
74     *
75     * @param context the properties context to fetch information from the
76     *          <code>META-INF</code> folder.
77     */
78    public PropertyCommentProxy(final PropertiesContext context)
79    {
80      this.context = context;
81      this.comments =
82          (context != null) ? new HashMap<Locale, PropertyComment>() : null;
83    }
84  
85    // ****************************** Inner Classes *****************************
86  
87    // ********************************* Methods ********************************
88  
89    // --- init -----------------------------------------------------------------
90  
91    // --- get&set --------------------------------------------------------------
92  
93    // --- business -------------------------------------------------------------
94  
95    /**
96     * {@inheritDoc}
97     *
98     * @impl Due to the very close relationship between the proxy and the property
99     *       descriptor, the proxy cannot get access to its descriptor on creation
100    *       time. The reason for the requirement that each invocation provides
101    *       the same property descriptor is caching: the comments are cached with
102    *       the locale as key. The property descriptor is not taken into account.
103    *       This is okay since this implementation is private and only used
104    *       internally by the PropertyMetaData class.
105    */
106   @Override
107   public PropertyComment getComment(
108       final PropertyDescriptor propertyDescriptor, final Locale locale)
109   {
110     if (context != null)
111     {
112       final PropertyCommentParser parser = new PropertyCommentParser(context);
113       PropertyComment comment = comments.get(locale);
114       if (comment == null)
115       {
116         comment = parser.parse(propertyDescriptor, locale);
117         comments.put(locale, comment);
118       }
119 
120       return comment;
121     }
122 
123     return PropertyComment.EMPTY_COMMENT;
124   }
125 
126   // --- object basics --------------------------------------------------------
127 
128   /**
129    * Returns the string representation of the object.
130    *
131    * @return the string representation of the object.
132    */
133   @Override
134   public String toString()
135   {
136     return ToStringBuilder.reflectionToString(this);
137   }
138 }