View Javadoc

1   /*
2   
3       dsh-codegen  Source code generation suite.
4       Copyright (c) 2004-2013 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   */
31  public final class CollectionDescription
32  {
33      /** The interface name for this collection description. */
34      private final String interfaceName;
35  
36      /** The interface description for this collection description. */
37      private final String interfaceDescription;
38  
39      /** The interface package name for this collection description. */
40      private final String interfacePackageName;
41  
42      /** The implementation name for this collection description. */
43      private final String implementationName;
44  
45      /** The implementation package name for this collection description. */
46      private final String implementationPackageName;
47  
48  
49      /**
50       * Create a new collection description from the specified parameters.
51       *
52       * @param interfaceName interface name for this collection description
53       * @param interfaceDescription interface description for this collection description
54       * @param interfacePackageName interface package name for this collection description
55       * @param implementationName implementation name for this collection description
56       * @param implementationPackageName implementation package name for this collection description
57       */
58      public CollectionDescription(final String interfaceName,
59                                   final String interfaceDescription,
60                                   final String interfacePackageName,
61                                   final String implementationName,
62                                   final String implementationPackageName)
63      {
64          this.interfaceName = interfaceName;
65          this.interfaceDescription = interfaceDescription;
66          this.interfacePackageName = interfacePackageName;
67          this.implementationName = implementationName;
68          this.implementationPackageName = implementationPackageName;
69      }
70  
71  
72      /**
73       * Return the interface name for this collection description.
74       *
75       * @return the interface name for this collection description
76       */
77      public String getInterfaceName()
78      {
79          return interfaceName;
80      }
81  
82      /**
83       * Return the interface description for this collection description.
84       *
85       * @return the interface description for this collection description
86       */
87      public String getInterfaceDescription()
88      {
89          return interfaceDescription;
90      }
91  
92      /**
93       * Return the interface package name for this collection description.
94       *
95       * @return the interface package name for this collection description
96       */
97      public String getInterfacePackageName()
98      {
99          return interfacePackageName;
100     }
101 
102     /**
103      * Return the implementation name for this collection description.
104      *
105      * @return the implementation name for this collection description
106      */
107     public String getImplementationName()
108     {
109         return implementationName;
110     }
111 
112     /**
113      * Return the implementation package name for this collection description.
114      *
115      * @return the implementation package name for this collection description
116      */
117     public String getImplementationPackageName()
118     {
119         return implementationPackageName;
120     }
121 
122 
123     /** Collection description. */
124     public static final CollectionDescription COLLECTION =
125         new CollectionDescription("Collection", "collection", "java.util", "ArrayList", "java.util");
126 
127     /** LinkedList description. */
128     public static final CollectionDescription LINKED_LIST =
129         new CollectionDescription("List", "list", "java.util", "LinkedList", "java.util");
130 
131     /** ArrayList description. */
132     public static final CollectionDescription ARRAY_LIST =
133         new CollectionDescription("List", "list", "java.util", "ArrayList", "java.util");
134 
135     /** HashSet description. */
136     public static final CollectionDescription HASH_SET =
137         new CollectionDescription("Set", "set", "java.util", "HashSet", "java.util");
138 
139     /** LinkedHashSet description. */
140     public static final CollectionDescription LINKED_HASH_SET =
141         new CollectionDescription("Set", "set", "java.util", "LinkedHashSet", "java.util");
142 
143     /** TreeSet description. */
144     public static final CollectionDescription TREE_SET =
145         new CollectionDescription("SortedSet", "sorted set", "java.util", "TreeSet", "java.util");
146 
147 
148     /**
149      * Choose a collection description that satisfies the specified parameters.
150      *
151      * @param indexed true if the collection should be indexed
152      * @param unique true if the collection should not allow duplicate elements
153      * @param ordered true if the collection should iterate over elements in <i>insertion-order</i>
154      * @param sorted true if the collection should iterate over elements in ascending element order,
155      *    sorted according to the <i>natural ordering</i> of its elements (see Comparable), or by a Comparator
156      *    provided at creation time
157      * @return a collection description that satisfies the specified parameters
158      */
159     public static CollectionDescription choose(final boolean indexed,
160                                                final boolean unique,
161                                                final boolean ordered,
162                                                final boolean sorted)
163     {
164         if (indexed && !ordered)
165         {
166             return ARRAY_LIST;
167         }
168         if (indexed && ordered)
169         {
170             return LINKED_LIST;
171         }
172         if (unique && !ordered && !sorted)
173         {
174             return HASH_SET;
175         }
176         if (unique && ordered && !sorted)
177         {
178             return LINKED_HASH_SET;
179         }
180         if (unique && sorted)
181         {
182             return TREE_SET;
183         }
184         return COLLECTION;
185     }
186 }