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.controller.model;
019    
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.Comparator;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    import org.jadira.dependencynavigator.model.Artifact;
028    import org.jadira.dependencynavigator.model.ArtifactInstanceMap;
029    import org.jadira.dependencynavigator.model.Duplicate;
030    
031    public class DuplicatesListManager {
032        private Duplicate selected;
033        private Map<String, Duplicate> artifactVersions;
034        private List<Duplicate> duplicates;
035        private List<Duplicate> ordered;
036        private ArtifactVersionsListManager artifactVersionsListManager;
037        private DuplicatesComparator duplicatesComparator;
038    
039        public DuplicatesListManager() {
040            reset();
041        }
042    
043        public void reset() {
044            this.artifactVersionsListManager = new ArtifactVersionsListManager();
045            artifactVersions = new HashMap<String, Duplicate>();
046            duplicates = new ArrayList<Duplicate>();
047            ordered = new ArrayList<Duplicate>();
048            duplicatesComparator = new DuplicatesComparator();
049        }
050    
051        /**
052         * Add artifact to existing duplicate or create new duplicate instance
053         * @param instance of artifact
054         */
055        public void add(Artifact instance) {
056            // check artifact agains duplicate version map
057            ArtifactInstanceMap map = artifactVersionsListManager.add(instance);
058            String artifactKey = instance.getGroupId() + ":" + instance.getArtifactId();
059            Duplicate duplicate = artifactVersions.get(artifactKey);
060            if (duplicate == null) {
061                duplicate = new Duplicate(map, duplicates);
062                artifactVersions.put(artifactKey, duplicate);
063                ordered.add(duplicate);
064                Collections.sort(ordered, duplicatesComparator);
065            } else {
066                duplicate.addVersion(map);
067            }
068        }
069    
070        public void setOrderByIdentifier() {
071            duplicatesComparator.setOrderBy();
072            Collections.sort(ordered, duplicatesComparator);
073        }
074    
075        public ArtifactVersionsListManager getVersionsListManager() {
076            return artifactVersionsListManager;
077        }
078    
079        public Duplicate get(int index) {
080            return ordered.get(index);
081        }
082    
083        public int size() {
084            return ordered.size();
085        }
086    
087        public void select(int index) {
088            deselect();
089            selected = get(index);
090            selected.select(true, artifactVersionsListManager);
091        }
092    
093        public void deselect() {
094            if (selected != null) {
095                selected.select(false, artifactVersionsListManager);
096            }
097        }
098    
099        public static class DuplicatesComparator implements Comparator<Duplicate> {
100            private boolean reversed;
101    
102            DuplicatesComparator() {
103                reversed = false;
104            }
105    
106            private int implementReversed(int comparison) {
107                if (reversed) {
108                    return comparison * -1;
109                }
110                return comparison;
111            }
112    
113            public void setOrderBy() {
114                reversed = !reversed;
115            }
116    
117            public int compare(Duplicate o1, Duplicate o2) {
118                return implementReversed(o1.getIdentifier().compareToIgnoreCase(o2.getIdentifier()));
119            }
120        }
121    }