001    /*
002     *  Licensed under the Apache License, Version 2.0 (the "License");
003     *  you may not use this file except in compliance with the License.
004     *  You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     *  Unless required by applicable law or agreed to in writing, software
009     *  distributed under the License is distributed on an "AS IS" BASIS,
010     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     *  See the License for the specific language governing permissions and
012     *  limitations under the License.
013     */
014    /*
015     * This file has been modified by Chris Pheby in accordance with Section 4.2
016     * of the Apache Software License
017     */
018    package org.jadira.dependencynavigator.model;
019    
020    import org.jadira.dependencynavigator.controller.model.ArtifactVersionsListManager;
021    
022    import java.util.ArrayList;
023    import java.util.Iterator;
024    import java.util.List;
025    
026    /**
027     * Class describing duplicate artifacts in a dependency tree
028     */
029    public class Duplicate {
030        private String identifier;
031        private List<ArtifactInstanceMap> versions;
032        private List<Duplicate> duplicatesList;
033    
034        private boolean selected;
035    
036        public Duplicate(ArtifactInstanceMap map, List<Duplicate> duplicates) {
037            this.identifier = map.getGroupId() + ":" + map.getArtifactId();
038            this.duplicatesList = duplicates;
039            versions = new ArrayList<ArtifactInstanceMap>();
040            versions.add(map);
041        }
042    
043        public void addVersion(ArtifactInstanceMap map) {
044            if (versions.contains(map)) {
045                return;
046            }
047            versions.add(map);
048            if (versions.size() == 2) {
049                duplicatesList.add(this);
050            }
051        }
052    
053        public String getIdentifier() {
054            return identifier;
055        }
056    
057        public void select(boolean state, ArtifactVersionsListManager artifactVersionsListManager) {
058            selected = state;
059            artifactVersionsListManager.select(versions);
060        }
061    
062        public boolean isSelected() {
063            return selected;
064        }
065    
066        public String getVersions() {
067            StringBuffer versionList = new StringBuffer();
068            for (Iterator<ArtifactInstanceMap> iter = versions.iterator(); iter.hasNext();) {
069                ArtifactInstanceMap version = iter.next();
070                versionList.append(version.getVersion());
071                if (iter.hasNext()) {
072                    versionList.append(", ");
073                }
074            }
075            return versionList.toString();
076        }
077    
078    }