001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.cli.compiler.dependencies;
018
019import java.util.Collections;
020import java.util.List;
021
022import org.springframework.util.Assert;
023
024/**
025 * A single dependency.
026 *
027 * @author Phillip Webb
028 * @since 1.3.0
029 */
030public final class Dependency {
031
032        private final String groupId;
033
034        private final String artifactId;
035
036        private final String version;
037
038        private final List<Exclusion> exclusions;
039
040        /**
041         * Create a new {@link Dependency} instance.
042         * @param groupId the group ID
043         * @param artifactId the artifact ID
044         * @param version the version
045         */
046        public Dependency(String groupId, String artifactId, String version) {
047                this(groupId, artifactId, version, Collections.<Exclusion>emptyList());
048        }
049
050        /**
051         * Create a new {@link Dependency} instance.
052         * @param groupId the group ID
053         * @param artifactId the artifact ID
054         * @param version the version
055         * @param exclusions the exclusions
056         */
057        public Dependency(String groupId, String artifactId, String version,
058                        List<Exclusion> exclusions) {
059                Assert.notNull(groupId, "GroupId must not be null");
060                Assert.notNull(artifactId, "ArtifactId must not be null");
061                Assert.notNull(version, "Version must not be null");
062                Assert.notNull(exclusions, "Exclusions must not be null");
063                this.groupId = groupId;
064                this.artifactId = artifactId;
065                this.version = version;
066                this.exclusions = Collections.unmodifiableList(exclusions);
067        }
068
069        /**
070         * Return the dependency group id.
071         * @return the group ID
072         */
073        public String getGroupId() {
074                return this.groupId;
075        }
076
077        /**
078         * Return the dependency artifact id.
079         * @return the artifact ID
080         */
081        public String getArtifactId() {
082                return this.artifactId;
083        }
084
085        /**
086         * Return the dependency version.
087         * @return the version
088         */
089        public String getVersion() {
090                return this.version;
091        }
092
093        /**
094         * Return the dependency exclusions.
095         * @return the exclusions
096         */
097        public List<Exclusion> getExclusions() {
098                return this.exclusions;
099        }
100
101        @Override
102        public String toString() {
103                return this.groupId + ":" + this.artifactId + ":" + this.version;
104        }
105
106        @Override
107        public int hashCode() {
108                final int prime = 31;
109                int result = 1;
110                result = prime * result + this.groupId.hashCode();
111                result = prime * result + this.artifactId.hashCode();
112                result = prime * result + this.version.hashCode();
113                result = prime * result + this.exclusions.hashCode();
114                return result;
115        }
116
117        @Override
118        public boolean equals(Object obj) {
119                if (this == obj) {
120                        return true;
121                }
122                if (obj == null) {
123                        return false;
124                }
125                if (getClass() == obj.getClass()) {
126                        Dependency other = (Dependency) obj;
127                        boolean result = true;
128                        result = result && this.groupId.equals(other.groupId);
129                        result = result && this.artifactId.equals(other.artifactId);
130                        result = result && this.version.equals(other.version);
131                        result = result && this.exclusions.equals(other.exclusions);
132                        return result;
133                }
134                return false;
135        }
136
137        /**
138         * A dependency exclusion.
139         */
140        public static final class Exclusion {
141
142                private final String groupId;
143
144                private final String artifactId;
145
146                Exclusion(String groupId, String artifactId) {
147                        Assert.notNull(groupId, "GroupId must not be null");
148                        Assert.notNull(groupId, "ArtifactId must not be null");
149                        this.groupId = groupId;
150                        this.artifactId = artifactId;
151                }
152
153                /**
154                 * Return the exclusion artifact ID.
155                 * @return the exclusion artifact ID
156                 */
157                public String getArtifactId() {
158                        return this.artifactId;
159                }
160
161                /**
162                 * Return the exclusion group ID.
163                 * @return the exclusion group ID
164                 */
165                public String getGroupId() {
166                        return this.groupId;
167                }
168
169                @Override
170                public String toString() {
171                        return this.groupId + ":" + this.artifactId;
172                }
173
174                @Override
175                public int hashCode() {
176                        return this.groupId.hashCode() * 31 + this.artifactId.hashCode();
177                }
178
179                @Override
180                public boolean equals(Object obj) {
181                        if (this == obj) {
182                                return true;
183                        }
184                        if (obj == null) {
185                                return false;
186                        }
187                        if (getClass() == obj.getClass()) {
188                                Exclusion other = (Exclusion) obj;
189                                boolean result = true;
190                                result = result && this.groupId.equals(other.groupId);
191                                result = result && this.artifactId.equals(other.artifactId);
192                                return result;
193                        }
194                        return false;
195                }
196
197        }
198
199}