View Javadoc
1   package org.codehaus.modello.plugin.java.javasource;
2   
3   /*
4    * Copyright (c) 2013, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import org.codehaus.modello.model.ModelDefault;
26  
27  /**
28   * JType sub-class for maps.
29   *
30   * @author <a href="mailto:simonetripodi@apache.org">Simone Tripodi</a>
31   * @since 1.8
32   */
33  public final class JMapType extends JComponentizedType {
34      // --------------------------------------------------------------------------
35  
36      /** Name of the actual map instance to be used, e.g. java.util.ArrayList. */
37      private String _instanceName;
38  
39      // --------------------------------------------------------------------------
40  
41      /**
42       * Creates an instance of a map type, of type 'mapName'.
43       *
44       * @param typeName Name of the map type interface.
45       * @param componentType Component type.
46       * @param useJava50 True if Java 5.0 should be used.
47       */
48      public JMapType(final String typeName, final JType componentType, final boolean useJava50) {
49          super(typeName, componentType, useJava50);
50      }
51  
52      /**
53       * Creates an instance of a map type, of type 'mapName'.
54       *
55       * @param typeName Name of the map type interface.
56       * @param instanceName Name of the actual map type instance.
57       * @param componentType Component type.
58       * @param useJava50 True if Java 5.0 should be used.
59       */
60      public JMapType(
61              final String typeName, final String instanceName, final JType componentType, final boolean useJava50) {
62          super(typeName, componentType, useJava50);
63          _instanceName = instanceName;
64      }
65  
66      // --------------------------------------------------------------------------
67  
68      /**
69       * Returns the instance name of this map type.
70       *
71       * @return The instance name of this map type.
72       */
73      public String getInstanceName() {
74          if (ModelDefault.PROPERTIES.equals(getName())) {
75              return _instanceName;
76          }
77  
78          if (_instanceName != null) {
79              String instance;
80  
81              int separator = _instanceName.indexOf("()");
82              if (separator != -1) {
83                  instance = _instanceName.substring(0, separator);
84              } else {
85                  instance = _instanceName;
86              }
87  
88              if (isUseJava50()) {
89                  return instance + "<Object, " + getComponentType().toString() + ">()";
90              }
91  
92              return instance + "/*<Object, " + getComponentType().toString() + ">*/()";
93          }
94  
95          return toString();
96      }
97  
98      /**
99       * @return the String representation of this JType.
100      */
101     public String toString() {
102         if (ModelDefault.PROPERTIES.equals(getName())) {
103             return getName();
104         }
105 
106         if (isUseJava50()) {
107             return getName() + "<Object, " + getComponentType().toString() + ">";
108         }
109 
110         return getName() + "/*<Object, " + getComponentType().toString() + ">*/";
111     }
112 
113     // --------------------------------------------------------------------------
114 }