1   package com.atlassian.plugins.codegen;
2   
3   import static com.atlassian.fugue.Option.some;
4   import static com.atlassian.plugins.codegen.ArtifactId.artifactId;
5   import static com.atlassian.plugins.codegen.VersionId.version;
6   import static com.google.common.base.Preconditions.checkNotNull;
7   
8   /**
9    * Describes a dependency on an external artifact that should be added to the POM.
10   */
11  public final class ArtifactDependency implements PluginProjectChange
12  {
13      public enum Scope
14      {
15          DEFAULT,
16          COMPILE,
17          PROVIDED,
18          TEST
19      };
20  
21      private ArtifactId artifactId;
22      private VersionId versionId;
23      private Scope scope;
24      
25      /**
26       * Creates a dependency descriptor whose version string is provided explicitly.
27       * @param groupId  Maven group ID
28       * @param artifactId  Maven artifact ID
29       * @param version  the version identifier
30       * @param scope  dependency scope (use DEfAULT to omit scope declaration)
31       */
32      public static ArtifactDependency dependency(String groupId, String artifactId, VersionId versionId, Scope scope)
33      {
34          return new ArtifactDependency(artifactId(some(groupId), artifactId), versionId, scope);
35      }
36  
37      /**
38       * Creates a dependency descriptor whose version string is provided explicitly.
39       * @param groupId  Maven group ID
40       * @param artifactId  Maven artifact ID
41       * @param version  the version string
42       * @param scope  dependency scope (use DEfAULT to omit scope declaration)
43       */
44      public static ArtifactDependency dependency(String groupId, String artifactId, String version, Scope scope)
45      {
46          return new ArtifactDependency(artifactId(some(groupId), artifactId), version(version), scope);
47      }
48  
49      /**
50       * Creates a dependency descriptor whose version string is provided explicitly.
51       * @param groupAndArtifactId  Maven group and artifact ID
52       * @param versionId  the version identifier
53       * @param scope  dependency scope (use DEfAULT to omit scope declaration)
54       */
55      public static ArtifactDependency dependency(ArtifactId groupAndArtifactId, VersionId versionId, Scope scope)
56      {
57          return new ArtifactDependency(groupAndArtifactId, versionId, scope);
58      }
59  
60      /**
61       * Creates a dependency descriptor whose version string is provided explicitly.
62       * @param groupAndArtifactId  Maven group and artifact ID
63       * @param version  the version string
64       * @param scope  dependency scope (use DEfAULT to omit scope declaration)
65       */
66      public static ArtifactDependency dependency(ArtifactId groupAndArtifactId, String version, Scope scope)
67      {
68          return new ArtifactDependency(groupAndArtifactId, version(version), scope);
69      }
70  
71      private ArtifactDependency(ArtifactId artifactId, VersionId versionId, Scope scope)
72      {
73          this.artifactId = checkNotNull(artifactId, "artifactId");
74          if (!artifactId.getGroupId().isDefined())
75          {
76              throw new IllegalArgumentException("Group ID must be specified for dependency");
77          }
78          this.versionId = checkNotNull(versionId, "versionId");
79          if (!versionId.getVersion().isDefined())
80          {
81              throw new IllegalArgumentException("Version must be specified for dependency");
82          }
83          this.scope = checkNotNull(scope, "scope");
84      }
85      
86      public ArtifactId getGroupAndArtifactId()
87      {
88          return artifactId;
89      }
90      
91      public VersionId getVersionId()
92      {
93          return versionId;
94      }
95      
96      public Scope getScope()
97      {
98          return scope;
99      }
100     
101     @Override
102     public String toString()
103     {
104         return "[dependency: " + artifactId + "]";
105     }
106 }