View Javadoc

1   /*
2   
3       dsh-codegen  Source code generation suite.
4       Copyright (c) 2004-2012 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.codegen;
25  
26  /**
27   * A description of a collection interface and implementation.
28   *
29   * @author  Michael Heuer
30   * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
31   */
32  public final class CollectionDescription
33  {
34      /** The interface name for this collection description. */
35      private final String interfaceName;
36  
37      /** The interface description for this collection description. */
38      private final String interfaceDescription;
39  
40      /** The interface package name for this collection description. */
41      private final String interfacePackageName;
42  
43      /** The implementation name for this collection description. */
44      private final String implementationName;
45  
46      /** The implementation package name for this collection description. */
47      private final String implementationPackageName;
48  
49  
50      /**
51       * Create a new collection description from the specified parameters.
52       *
53       * @param interfaceName interface name for this collection description
54       * @param interfaceDescription interface description for this collection description
55       * @param interfacePackageName interface package name for this collection description
56       * @param implementationName implementation name for this collection description
57       * @param implementationPackageName implementation package name for this collection description
58       */
59      public CollectionDescription(final String interfaceName,
60                                   final String interfaceDescription,
61                                   final String interfacePackageName,
62                                   final String implementationName,
63                                   final String implementationPackageName)
64      {
65          this.interfaceName = interfaceName;
66          this.interfaceDescription = interfaceDescription;
67          this.interfacePackageName = interfacePackageName;
68          this.implementationName = implementationName;
69          this.implementationPackageName = implementationPackageName;
70      }
71  
72  
73      /**
74       * Return the interface name for this collection description.
75       *
76       * @return the interface name for this collection description
77       */
78      public String getInterfaceName()
79      {
80          return interfaceName;
81      }
82  
83      /**
84       * Return the interface description for this collection description.
85       *
86       * @return the interface description for this collection description
87       */
88      public String getInterfaceDescription()
89      {
90          return interfaceDescription;
91      }
92  
93      /**
94       * Return the interface package name for this collection description.
95       *
96       * @return the interface package name for this collection description
97       */
98      public String getInterfacePackageName()
99      {
100         return interfacePackageName;
101     }
102 
103     /**
104      * Return the implementation name for this collection description.
105      *
106      * @return the implementation name for this collection description
107      */
108     public String getImplementationName()
109     {
110         return implementationName;
111     }
112 
113     /**
114      * Return the implementation package name for this collection description.
115      *
116      * @return the implementation package name for this collection description
117      */
118     public String getImplementationPackageName()
119     {
120         return implementationPackageName;
121     }
122 
123 
124     /** Collection description. */
125     public static final CollectionDescription COLLECTION =
126         new CollectionDescription("Collection", "collection", "java.util", "ArrayList", "java.util");
127 
128     /** LinkedList description. */
129     public static final CollectionDescription LINKED_LIST =
130         new CollectionDescription("List", "list", "java.util", "LinkedList", "java.util");
131 
132     /** ArrayList description. */
133     public static final CollectionDescription ARRAY_LIST =
134         new CollectionDescription("List", "list", "java.util", "ArrayList", "java.util");
135 
136     /** HashSet description. */
137     public static final CollectionDescription HASH_SET =
138         new CollectionDescription("Set", "set", "java.util", "HashSet", "java.util");
139 
140     /** LinkedHashSet description. */
141     public static final CollectionDescription LINKED_HASH_SET =
142         new CollectionDescription("Set", "set", "java.util", "LinkedHashSet", "java.util");
143 
144     /** TreeSet description. */
145     public static final CollectionDescription TREE_SET =
146         new CollectionDescription("SortedSet", "sorted set", "java.util", "TreeSet", "java.util");
147 
148 
149     /**
150      * Choose a collection description that satisfies the specified parameters.
151      *
152      * @param indexed true if the collection should be indexed
153      * @param unique true if the collection should not allow duplicate elements
154      * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
155      * @param sorted true if the collection should iterate over elements in ascending element order,
156      *    sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
157      *    provided at creation time
158      * @return a collection description that satisfies the specified parameters
159      */
160     public static CollectionDescription choose(final boolean indexed,
161                                                final boolean unique,
162                                                final boolean ordered,
163                                                final boolean sorted)
164     {
165         if (indexed && !ordered)
166         {
167             return ARRAY_LIST;
168         }
169         if (indexed && ordered)
170         {
171             return LINKED_LIST;
172         }
173         if (unique && !ordered && !sorted)
174         {
175             return HASH_SET;
176         }
177         if (unique && ordered && !sorted)
178         {
179             return LINKED_HASH_SET;
180         }
181         if (unique && sorted)
182         {
183             return TREE_SET;
184         }
185         return COLLECTION;
186     }
187 }