View Javadoc

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