900d1961cad98c8a2010d1ccd0619161ab42ef68
[jalview.git] / src / org / biojava / dasobert / feature / FeatureComparator.java
1 /*
2  *                    BioJava development code
3  *
4  * This code may be freely distributed and modified under the
5  * terms of the GNU Lesser General Public Licence.  This should
6  * be distributed with the code.  If you do not have a copy,
7  * see:
8  *
9  *      http://www.gnu.org/copyleft/lesser.html
10  *
11  * Copyright for this code is held jointly by the individual
12  * authors.  These should be listed in @author doc comments.
13  *
14  * For more information on the BioJava project and its aims,
15  * or to join the biojava-l mailing list, visit the home page
16  * at:
17  *
18  *      http://www.biojava.org/
19  *
20  * Created on 23.09.2004
21  * @author Andreas Prlic
22  *
23  */
24
25
26 package org.biojava.dasobert.feature ;
27
28 import java.util.Comparator ;
29 import java.util.Iterator;
30 import java.util.List;
31
32
33 /** a comparator to sort Features byt type
34  * @author Andreas Prlic
35  */
36
37 public class FeatureComparator 
38 implements Comparator
39 {
40
41         public FeatureComparator() {
42         }
43
44         public int compare(Object a, Object b) {
45                 FeatureTrack x = (FeatureTrack) a;
46                 FeatureTrack y = (FeatureTrack) b;
47
48                 String typea = x.getType();
49                 String typeb = y.getType();
50
51                 if ( ! typea.equals(typeb))
52                         return typea.compareTo(typeb);
53
54                 List s1 = x.getSegments();
55                 List s2 = y.getSegments();
56
57                 Iterator iter1 = s1.iterator();
58                 Iterator iter2 = s2.iterator();
59
60                 while (iter1.hasNext()){
61                         Segment seg1 = (Segment)iter1.next();
62                         int start1 = seg1.getStart();
63
64                         while (iter2.hasNext()){
65                                 Segment seg2 = (Segment)iter2.next();
66                                 int start2 = seg2.getStart();
67
68                                 if ( start1 < start2){
69                                         return -1;
70                                 } if ( start1 > start2){
71                                         return 1;
72                                 }
73
74                         }
75                 }
76
77                 return 0;
78
79         }
80
81 }