View Javadoc
1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   */
14  /*
15   * This file has been modified by Chris Pheby in accordance with Section 4.2
16   * of the Apache Software License
17   */
18  package org.jadira.dependencynavigator.controller.model;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.jadira.dependencynavigator.model.Artifact;
28  import org.jadira.dependencynavigator.model.ArtifactInstanceMap;
29  import org.jadira.dependencynavigator.model.Duplicate;
30  
31  public class DuplicatesListManager {
32      private Duplicate selected;
33      private Map<String, Duplicate> artifactVersions;
34      private List<Duplicate> duplicates;
35      private List<Duplicate> ordered;
36      private ArtifactVersionsListManager artifactVersionsListManager;
37      private DuplicatesComparator duplicatesComparator;
38  
39      public DuplicatesListManager() {
40          reset();
41      }
42  
43      public void reset() {
44          this.artifactVersionsListManager = new ArtifactVersionsListManager();
45          artifactVersions = new HashMap<String, Duplicate>();
46          duplicates = new ArrayList<Duplicate>();
47          ordered = new ArrayList<Duplicate>();
48          duplicatesComparator = new DuplicatesComparator();
49      }
50  
51      /**
52       * Add artifact to existing duplicate or create new duplicate instance
53       * @param instance of artifact
54       */
55      public void add(Artifact instance) {
56          // check artifact agains duplicate version map
57          ArtifactInstanceMap map = artifactVersionsListManager.add(instance);
58          String artifactKey = instance.getGroupId() + ":" + instance.getArtifactId();
59          Duplicate duplicate = artifactVersions.get(artifactKey);
60          if (duplicate == null) {
61              duplicate = new Duplicate(map, duplicates);
62              artifactVersions.put(artifactKey, duplicate);
63              ordered.add(duplicate);
64              Collections.sort(ordered, duplicatesComparator);
65          } else {
66              duplicate.addVersion(map);
67          }
68      }
69  
70      public void setOrderByIdentifier() {
71          duplicatesComparator.setOrderBy();
72          Collections.sort(ordered, duplicatesComparator);
73      }
74  
75      public ArtifactVersionsListManager getVersionsListManager() {
76          return artifactVersionsListManager;
77      }
78  
79      public Duplicate get(int index) {
80          return ordered.get(index);
81      }
82  
83      public int size() {
84          return ordered.size();
85      }
86  
87      public void select(int index) {
88          deselect();
89          selected = get(index);
90          selected.select(true, artifactVersionsListManager);
91      }
92  
93      public void deselect() {
94          if (selected != null) {
95              selected.select(false, artifactVersionsListManager);
96          }
97      }
98  
99      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 }