1 package fr.jayasoft.ivy.latest; 2 3 import java.util.ArrayList ; 4 import java.util.Arrays ; 5 import java.util.Collections ; 6 import java.util.Comparator ; 7 import java.util.Date ; 8 import java.util.List ; 9 10 import fr.jayasoft.ivy.ArtifactInfo; 11 12 public class ComparatorLatestStrategy extends AbstractLatestStrategy { 13 14 private Comparator _comparator; 15 16 public ComparatorLatestStrategy() { 17 } 18 19 public ComparatorLatestStrategy(Comparator comparator) { 20 _comparator = comparator; 21 } 22 23 public ArtifactInfo findLatest(ArtifactInfo[] artifacts, Date date) { 24 if (artifacts == null) { 25 return null; 26 } 27 ArtifactInfo found = null; 28 for (int i = 0; i < artifacts.length; i++) { 29 ArtifactInfo art = artifacts[i]; 30 if (found == null || _comparator.compare(art, found) > 0) { 31 if (date != null) { 32 long lastModified = art.getLastModified(); 33 if (lastModified > date.getTime()) { 34 continue; 35 } 36 } 37 found = art; 38 } 39 } 40 return found; 41 } 42 43 public List sort(ArtifactInfo[] infos) { 44 List ret = new ArrayList (Arrays.asList(infos)); 45 Collections.sort(ret, _comparator); 46 return ret; 47 } 48 49 public Comparator getComparator() { 50 return _comparator; 51 } 52 53 public void setComparator(Comparator comparator) { 54 _comparator = comparator; 55 } 56 57 } 58 | Popular Tags |