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
34      extends JComponentizedType
35  {
36      // --------------------------------------------------------------------------
37  
38      /** Name of the actual map instance to be used, e.g. java.util.ArrayList. */
39      private String _instanceName;
40  
41      // --------------------------------------------------------------------------
42  
43      /**
44       * Creates an instance of a map type, of type 'mapName'.
45       *
46       * @param typeName Name of the map type interface.
47       * @param componentType Component type.
48       * @param useJava50 True if Java 5.0 should be used.
49       */
50      public JMapType( final String typeName, final JType componentType, final boolean useJava50 )
51      {
52          super( typeName, componentType, useJava50 );
53      }
54  
55      /**
56       * Creates an instance of a map type, of type 'mapName'.
57       *
58       * @param typeName Name of the map type interface.
59       * @param instanceName Name of the actual map type instance.
60       * @param componentType Component type.
61       * @param useJava50 True if Java 5.0 should be used.
62       */
63      public JMapType( final String typeName, final String instanceName, final JType componentType,
64                              final boolean useJava50 )
65      {
66          super( typeName, componentType, useJava50 );
67          _instanceName = instanceName;
68      }
69  
70      // --------------------------------------------------------------------------
71  
72      /**
73       * Returns the instance name of this map type.
74       *
75       * @return The instance name of this map type.
76       */
77      public String getInstanceName()
78      {
79          if ( ModelDefault.PROPERTIES.equals( getName() ) )
80          {
81              return _instanceName;
82          }
83  
84          if ( _instanceName != null )
85          {
86              String instance;
87  
88              int separator = _instanceName.indexOf( "()" );
89              if ( separator != -1 )
90              {
91                  instance = _instanceName.substring( 0, separator );
92              }
93              else
94              {
95                  instance = _instanceName;
96              }
97  
98              if ( isUseJava50() )
99              {
100                 return instance + "<Object, " + getComponentType().toString() + ">()";
101             }
102 
103             return instance + "/*<Object, " + getComponentType().toString() + ">*/()";
104         }
105 
106         return toString();
107     }
108 
109     /**
110      * {@inheritDoc} <br/>
111      * Returns the String representation of this JType.
112      */
113     public String toString()
114     {
115         if ( ModelDefault.PROPERTIES.equals( getName() ) )
116         {
117             return getName();
118         }
119 
120         if ( isUseJava50() )
121         {
122             return getName() + "<Object, " + getComponentType().toString() + ">";
123         }
124 
125         return getName() + "/*<Object, " + getComponentType().toString() + ">*/";
126     }
127 
128     // --------------------------------------------------------------------------
129 }