View Javadoc

1   /*
2    * Copyright 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.admin.domain.model;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.List;
24  import java.util.Set;
25  import java.util.TreeSet;
26  
27  import com.google.common.collect.LinkedListMultimap;
28  import com.google.common.collect.Multimap;
29  
30  import de.smartics.properties.api.core.domain.PropertyDescriptor;
31  import de.smartics.properties.api.core.domain.PropertyDescriptorRegistry;
32  import de.smartics.properties.api.core.domain.PropertyKey;
33  
34  /**
35   * A set of properties descriptors.
36   */
37  public class PropertyDescriptors implements Serializable
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    /**
44     * The class version identifier.
45     */
46    private static final long serialVersionUID = 1L;
47  
48    // --- members --------------------------------------------------------------
49  
50    /**
51     * The registry of known descriptors.
52     */
53    private final PropertyDescriptorRegistry registry;
54  
55    /**
56     * Helper to sort descriptors by property set name.
57     */
58    private final Multimap<String, PropertyDescriptor> sets = LinkedListMultimap
59        .create();
60  
61    // ****************************** Initializer *******************************
62  
63    // ****************************** Constructors ******************************
64  
65    /**
66     * Default constructor.
67     *
68     * @param registry the registry of known descriptors.
69     */
70    public PropertyDescriptors(final PropertyDescriptorRegistry registry)
71    {
72      this.registry = registry;
73      init();
74    }
75  
76    // ****************************** Inner Classes *****************************
77  
78    // ********************************* Methods ********************************
79  
80    // --- init -----------------------------------------------------------------
81  
82    private void init()
83    {
84      final Set<Class<?>> propertySets = registry.getPropertySetTypes();
85      for (final Class<?> propertySet : propertySets)
86      {
87        final List<? extends PropertyDescriptor> descriptors =
88            registry.get(propertySet);
89        if (descriptors.isEmpty())
90        {
91          continue;
92        }
93  
94        final String propertySetName = getPropertySetName(descriptors);
95  
96        for (final PropertyDescriptor descriptor : descriptors)
97        {
98          sets.put(propertySetName, descriptor);
99        }
100     }
101   }
102 
103   private String getPropertySetName(
104       final List<? extends PropertyDescriptor> descriptors)
105   {
106     final PropertyDescriptor firstDescriptor = descriptors.get(0);
107     final PropertyKey key = firstDescriptor.getKey();
108     return key.getPropertySet();
109   }
110 
111   // --- get&set --------------------------------------------------------------
112 
113   /**
114    * Returns the registry of known descriptors.
115    *
116    * @return the registry of known descriptors.
117    */
118   public PropertyDescriptorRegistry getRegistry()
119   {
120     return registry;
121   }
122 
123   /**
124    * Returns the sorted collection of property keys.
125    *
126    * @return the sorted collection of property keys.
127    */
128   public Collection<String> getPropertyKeys()
129   {
130     return new TreeSet<String>(sets.keySet());
131   }
132 
133   /**
134    * Returns the sorted list of property descriptors.
135    *
136    * @param propertySet the set whose descritors are requested.
137    * @return the sorted list of property descriptors.
138    */
139   public List<PropertyDescriptor> get(final String propertySet)
140   {
141     final List<PropertyDescriptor> descriptors =
142         new ArrayList<PropertyDescriptor>(sets.get(propertySet));
143     Collections.sort(descriptors, new Comparator<PropertyDescriptor>()
144     {
145       @Override
146       public int compare(final PropertyDescriptor o1,
147           final PropertyDescriptor o2)
148       {
149         return o1.getKey().compareTo(o2.getKey());
150       }
151     });
152     return descriptors;
153   }
154 
155   // --- business -------------------------------------------------------------
156 
157   // --- object basics --------------------------------------------------------
158 
159 }