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.Collection;
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.jadira.dependencynavigator.model.Artifact;
30  import org.jadira.dependencynavigator.model.ArtifactInstanceMap;
31  
32  /**
33   * Holds and indexed, sortable list of artifact versions. Provides selection, deselection and sort order functionality.
34   */
35  public class ArtifactVersionsListManager {
36      private List<ArtifactInstanceMap> selected;
37      private List<ArtifactInstanceMap> ordered;
38      private Map<String, ArtifactInstanceMap> indexed;
39      private ArtifactInstanceMapComparator artifactInstanceMapComparator;
40      public static final int DEFAULT_ORDER_BY = 1;
41  
42      public ArtifactVersionsListManager() {
43          ordered = new ArrayList<ArtifactInstanceMap>();
44          indexed = new HashMap<String, ArtifactInstanceMap>();
45          artifactInstanceMapComparator = new ArtifactInstanceMapComparator();
46          selected = new ArrayList<ArtifactInstanceMap>();
47      }
48  
49      public ArtifactInstanceMap get(int index) {
50          if (index >= 0 && index < ordered.size()) {
51              return ordered.get(index);
52          } else
53              return null;
54      }
55  
56      public void select(int index) {
57          List<ArtifactInstanceMap> newlySelectedMaps = new ArrayList<ArtifactInstanceMap>();
58          ArtifactInstanceMap mapAtIndex = get(index);
59          if (mapAtIndex != null) {
60              newlySelectedMaps.add(mapAtIndex);
61          }
62          select(newlySelectedMaps);
63      }
64  
65      /**
66       * Reselect artifact maps
67       * @param newlySelected collection of newly selected artifact instance maps
68       */
69      public void select(Collection<ArtifactInstanceMap> newlySelected) {
70          for (Iterator<ArtifactInstanceMap> selectdIterator = selected.iterator(); selectdIterator.hasNext();) {
71              ArtifactInstanceMap map = selectdIterator.next();
72              map.select(false);
73              selectdIterator.remove();
74          }
75          for (ArtifactInstanceMap map : newlySelected) {
76              map.select(true);
77              selected.add(map);
78          }
79      }
80  
81      public int size() {
82          return ordered.size();
83      }
84  
85      ArtifactInstanceMap add(Artifact instance) {
86          String version = instance.getVersion();
87          // add artifact reference to the instance map
88          String fullKey = instance.getGroupId() + ":" + instance.getArtifactId() + ":" + version;
89          ArtifactInstanceMap map = indexed.get(fullKey);
90          if (map == null) {
91              map = new ArtifactInstanceMap(instance);
92              indexed.put(fullKey, map);
93              ordered.add(map);
94              Collections.sort(ordered, artifactInstanceMapComparator);
95          }
96          map.addInstance(instance);
97          return map;
98      }
99  
100     public void setArtifactInstanceMapComparator(int field) {
101         artifactInstanceMapComparator.setOrderBy(field);
102         Collections.sort(ordered, artifactInstanceMapComparator);
103     }
104 
105     static class ArtifactInstanceMapComparator implements Comparator<ArtifactInstanceMap> {
106         private int orderBy;
107         private boolean reversed;
108 
109         ArtifactInstanceMapComparator() {
110             orderBy = DEFAULT_ORDER_BY;
111             reversed = false;
112         }
113 
114         private int implementReversed(int comparison) {
115             if (reversed) {
116                 return comparison * -1;
117             }
118             return comparison;
119         }
120 
121         public void setOrderBy(int columnIndex) {
122             if (orderBy != columnIndex) {
123                 orderBy = columnIndex;
124                 reversed = false;
125             } else {
126                 reversed = !reversed;
127             }
128         }
129 
130         public int compare(ArtifactInstanceMap map1, ArtifactInstanceMap map2) {
131             switch (orderBy) {
132             case 0:
133                 return implementReversed(map1.getGroupId().compareToIgnoreCase(map2.getGroupId()));
134             case 1:
135                 return implementReversed(map1.getArtifactId().compareToIgnoreCase(map2.getArtifactId()));
136             case 2:
137                 return implementReversed(map1.getVersion().compareToIgnoreCase(map2.getVersion()));
138             case 3:
139                 return implementReversed(new Integer(map1.getInstances().size()).compareTo(map2.getInstances().size()));
140             default:
141                 throw new IllegalArgumentException("Field " + orderBy + " not found");
142             }
143         }
144     }
145 }