View Javadoc

1   /*
2    * Copyright 2007-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.exceptions.report.message;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.TreeMap;
25  
26  import de.smartics.exceptions.i18n.message.MessageType;
27  import de.smartics.util.lang.Arg;
28  
29  /**
30   * Contains the information about a set of place holders. The placeholder
31   * information is specified via
32   * {@link de.smartics.exceptions.i18n.message.MessageParam} annotations. The
33   * placeholder is used to create exception messages like <code>details</code> or
34   * <code>todo</code>.
35   */
36  public class PlaceHolderInfo implements Serializable
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    /**
43     * The class version identifier.
44     */
45    private static final long serialVersionUID = 1L;
46  
47    /**
48     * The singleton to represent the immutable empty place holder info.
49     */
50    public static final PlaceHolderInfo EMPTY = new PlaceHolderInfo()
51    {
52      /**
53       * The class version identifier.
54       */
55      private static final long serialVersionUID = 1L;
56  
57      /**
58       * Not allowed to add place holder descriptions to the empty singleton.
59       *
60       * @param desc the description to add.
61       */
62      public void putPlaceHolderDesc(final PlaceHolderDesc desc)
63      {
64        throw new UnsupportedOperationException(
65            "Adding place holder descriptions to the empty place holder info singleton is not allowed.");
66      }
67    };
68  
69    // --- members --------------------------------------------------------------
70  
71    /**
72     * The descriptions of place holders.
73     */
74    private final Map<PlaceHolderDescId, PlaceHolderDesc> placeHolderDescs =
75        new LinkedHashMap<PlaceHolderDescId, PlaceHolderDesc>();
76  
77    // ****************************** Initializer *******************************
78  
79    // ****************************** Constructors ******************************
80  
81    /**
82     * Default constructor.
83     */
84    public PlaceHolderInfo()
85    {
86    }
87  
88    // ****************************** Inner Classes *****************************
89  
90    // ********************************* Methods ********************************
91  
92    // --- init -----------------------------------------------------------------
93  
94    // --- get&set --------------------------------------------------------------
95  
96    // --- business -------------------------------------------------------------
97  
98    /**
99     * Returns the place holder description identifier by the given
100    * <code>id</code>.
101    *
102    * @param id the identifier of the place holder description requested.
103    * @return the place holder description or <code>null</code>, if there is no
104    *         place holder description for the given <code>id</code>.
105    */
106   public PlaceHolderDesc getPlaceHolderDesc(final PlaceHolderDescId id)
107   {
108     return placeHolderDescs.get(id);
109   }
110 
111   /**
112    * Returns the place holder description identifier by the given
113    * <code>id</code> or the default if no explicit value is given.
114    *
115    * @param id the identifier of the place holder description requested.
116    * @return the place holder description or the default, if there is no place
117    *         holder description for the given <code>id</code>.
118    */
119   public PlaceHolderDesc getPlaceHolderDescOrDefault(final PlaceHolderDescId id)
120   {
121     PlaceHolderDesc desc = placeHolderDescs.get(id);
122     if (desc == null)
123     {
124       final PlaceHolderDesc defaultDescription =
125           getPlaceHolderDesc(new PlaceHolderDescId(id.getPlaceHolderIndex(),
126               null));
127       desc = new PlaceHolderDesc(defaultDescription, id.getMessageType());
128     }
129     return desc;
130   }
131 
132   /**
133    * Returns an iteration over the place holder descriptions in their natural
134    * order.
135    *
136    * @param messageType the message type for which descriptors are requested.
137    * @return the descriptions in their natural order.
138    * @throws NullPointerException if {@code messageType} is <code>null</code>.
139    */
140   public List<PlaceHolderDesc> getPlaceHolderDescs(final MessageType messageType)
141     throws NullPointerException
142   {
143     Arg.checkNotNull("messageType", messageType);
144 
145     final Map<String, PlaceHolderDesc> map =
146         new TreeMap<String, PlaceHolderDesc>();
147     for (PlaceHolderDesc desc : this.placeHolderDescs.values())
148     {
149       final String index = String.valueOf(desc.getPlaceHolderIndex());
150       final MessageType descMessageType = desc.getPlaceHolderMessageType();
151       if (messageType.equals(descMessageType)
152           || (descMessageType == null && !map.containsKey(index)))
153       {
154         map.put(index, desc);
155       }
156     }
157 
158     final List<PlaceHolderDesc> values = new ArrayList<PlaceHolderDesc>(map.values());
159     Collections.sort(values);
160     return values;
161   }
162 
163   /**
164    * Sets the description with its identifier to the map of place holder
165    * descriptions. Any previously added descriptions with the same ID is
166    * replaced.
167    *
168    * @param desc the description to add.
169    */
170   public void putPlaceHolderDesc(final PlaceHolderDesc desc)
171   {
172     this.placeHolderDescs.put(desc.getPlaceHolderId(), desc);
173   }
174 
175   /**
176    * Checks if the place holder info is empty.
177    *
178    * @return <code>true</code> if no place holder descriptions are provided,
179    *         <code>false</code> otherwise.
180    */
181   public boolean isEmpty()
182   {
183     return placeHolderDescs.isEmpty();
184   }
185 
186   // --- object basics --------------------------------------------------------
187 
188 }