1   package com.atlassian.plugins.codegen;
2   
3   import java.util.Map;
4   
5   import com.atlassian.fugue.Option;
6   
7   import com.google.common.collect.ImmutableMap;
8   import com.google.common.collect.Maps;
9   
10  import static com.atlassian.fugue.Option.none;
11  import static com.google.common.base.Preconditions.checkNotNull;
12  
13  /**
14   * Describes a <component> element that should be added to the plugin XML file.
15   * This is provided in addition to {@link ModuleDescriptor} because some other types of
16   * modules may also need to create component declarations.
17   * <p>
18   * Unlike other classes in this package, this class uses a builder pattern due to the
19   * large number of optional properties. 
20   */
21  public final class ComponentDeclaration implements PluginProjectChange
22  {
23      public enum Visibility
24      {
25          PUBLIC,
26          PRIVATE
27      };
28  
29      private final ClassId classId;
30      private final String key;
31      private final Visibility visibility;
32      private final Option<String> name;
33      private final Option<String> nameI18nKey;
34      private final Option<String> description;
35      private final Option<String> descriptionI18nKey;
36      private final Option<ClassId> interfaceId;
37      private final Option<String> alias;
38      private final ImmutableMap<String, String> serviceProperties;
39      
40      public static Builder builder(ClassId classId, String key)
41      {
42          return new Builder(classId, key);
43      }
44      
45      public static ComponentDeclaration componentDeclaration(ClassId classId, String key)
46      {
47          return builder(classId, key).build();
48      }
49      
50      private ComponentDeclaration(Builder builder)
51      {
52          this.classId = builder.classId;
53          this.visibility = builder.visibility;
54          this.key = builder.key;
55          this.name = builder.name;
56          this.nameI18nKey = builder.nameI18nKey;
57          this.description = builder.description;
58          this.descriptionI18nKey = builder.descriptionI18nKey;
59          this.interfaceId = builder.interfaceId;
60          this.alias = builder.alias;
61          this.serviceProperties = ImmutableMap.copyOf(builder.serviceProperties);
62      }
63  
64      public ClassId getClassId()
65      {
66          return classId;
67      }
68      
69      public String getKey()
70      {
71          return key;
72      }
73  
74      public Option<ClassId> getInterfaceId()
75      {
76          return interfaceId;
77      }
78      
79      public Visibility getVisibility()
80      {
81          return visibility;
82      }
83  
84      public Option<String> getName()
85      {
86          return name;
87      }
88  
89      public Option<String> getNameI18nKey()
90      {
91          return nameI18nKey;
92      }
93      
94      public Option<String> getDescription()
95      {
96          return description;
97      }
98      
99      public Option<String> getDescriptionI18nKey()
100     {
101         return descriptionI18nKey;
102     }
103     
104     public Option<String> getAlias()
105     {
106         return alias;
107     }
108     
109     public ImmutableMap<String, String> getServiceProperties()
110     {
111         return serviceProperties;
112     }
113     
114     @Override
115     public String toString()
116     {
117         return "[component: " + classId + "]";
118     }
119     
120     public static class Builder
121     {
122         private final ClassId classId;
123         private final String key;
124         private Visibility visibility = Visibility.PRIVATE;
125         private Option<String> name = none();
126         private Option<String> nameI18nKey = none();
127         private Option<String> description = none();
128         private Option<String> descriptionI18nKey = none();
129         private Option<ClassId> interfaceId = none();
130         private Option<String> alias = none();
131         private Map<String, String> serviceProperties = Maps.newHashMap();
132     
133         public Builder(ClassId classId, String key)
134         {
135             this.classId = checkNotNull(classId, "classId");
136             this.key = checkNotNull(key, "key");
137         }
138         
139         public ComponentDeclaration build()
140         {
141             return new ComponentDeclaration(this);
142         }
143         
144         public Builder interfaceId(Option<ClassId> interfaceId)
145         {
146             this.interfaceId = checkNotNull(interfaceId, "interfaceId");
147             return this;
148         }
149         
150         public Builder visibility(Visibility visibility)
151         {
152             this.visibility = checkNotNull(visibility, "visibility");
153             return this;
154         }
155 
156         public Builder name(Option<String> name)
157         {
158             this.name = checkNotNull(name, "name");
159             return this;
160         }
161 
162         public Builder nameI18nKey(Option<String> nameI18nKey)
163         {
164             this.nameI18nKey = checkNotNull(nameI18nKey, "nameI18nKey");
165             return this;
166         }
167 
168         public Builder description(Option<String> description)
169         {
170             this.description = checkNotNull(description, "description");
171             return this;
172         }
173 
174         public Builder descriptionI18nKey(Option<String> descriptionI18nKey)
175         {
176             this.descriptionI18nKey = checkNotNull(descriptionI18nKey, "descriptionI18nKey");
177             return this;
178         }
179 
180         public Builder alias(Option<String> alias)
181         {
182             this.alias = checkNotNull(alias, "alias");
183             return this;
184         }
185         
186         public Builder serviceProperties(Map<String, String> serviceProperties)
187         {
188             this.serviceProperties.putAll(checkNotNull(serviceProperties, "serviceProperties"));
189             return this;
190         }
191     }
192 }