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.Collection; 022 import java.util.Collections; 023 import java.util.Comparator; 024 import java.util.HashMap; 025 import java.util.Iterator; 026 import java.util.List; 027 import java.util.Map; 028 029 import org.jadira.dependencynavigator.model.Artifact; 030 import org.jadira.dependencynavigator.model.ArtifactInstanceMap; 031 032 /** 033 * Holds and indexed, sortable list of artifact versions. Provides selection, deselection and sort order functionality. 034 */ 035 public class ArtifactVersionsListManager { 036 private List<ArtifactInstanceMap> selected; 037 private List<ArtifactInstanceMap> ordered; 038 private Map<String, ArtifactInstanceMap> indexed; 039 private ArtifactInstanceMapComparator artifactInstanceMapComparator; 040 public static final int DEFAULT_ORDER_BY = 1; 041 042 public ArtifactVersionsListManager() { 043 ordered = new ArrayList<ArtifactInstanceMap>(); 044 indexed = new HashMap<String, ArtifactInstanceMap>(); 045 artifactInstanceMapComparator = new ArtifactInstanceMapComparator(); 046 selected = new ArrayList<ArtifactInstanceMap>(); 047 } 048 049 public ArtifactInstanceMap get(int index) { 050 if (index >= 0 && index < ordered.size()) { 051 return ordered.get(index); 052 } else 053 return null; 054 } 055 056 public void select(int index) { 057 List<ArtifactInstanceMap> newlySelectedMaps = new ArrayList<ArtifactInstanceMap>(); 058 ArtifactInstanceMap mapAtIndex = get(index); 059 if (mapAtIndex != null) { 060 newlySelectedMaps.add(mapAtIndex); 061 } 062 select(newlySelectedMaps); 063 } 064 065 /** 066 * Reselect artifact maps 067 * @param newlySelected collection of newly selected artifact instance maps 068 */ 069 public void select(Collection<ArtifactInstanceMap> newlySelected) { 070 for (Iterator<ArtifactInstanceMap> selectdIterator = selected.iterator(); selectdIterator.hasNext();) { 071 ArtifactInstanceMap map = selectdIterator.next(); 072 map.select(false); 073 selectdIterator.remove(); 074 } 075 for (ArtifactInstanceMap map : newlySelected) { 076 map.select(true); 077 selected.add(map); 078 } 079 } 080 081 public int size() { 082 return ordered.size(); 083 } 084 085 ArtifactInstanceMap add(Artifact instance) { 086 String version = instance.getVersion(); 087 // add artifact reference to the instance map 088 String fullKey = instance.getGroupId() + ":" + instance.getArtifactId() + ":" + version; 089 ArtifactInstanceMap map = indexed.get(fullKey); 090 if (map == null) { 091 map = new ArtifactInstanceMap(instance); 092 indexed.put(fullKey, map); 093 ordered.add(map); 094 Collections.sort(ordered, artifactInstanceMapComparator); 095 } 096 map.addInstance(instance); 097 return map; 098 } 099 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 }