2b75c22e3eeee043df944ee05338f642a903587d
[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 package org.biojava.dasobert.feature;
26
27 import java.util.Comparator;
28 import java.util.Iterator;
29 import java.util.List;
30
31 /**
32  * a comparator to sort Features byt type
33  * 
34  * @author Andreas Prlic
35  */
36
37 public class FeatureComparator implements Comparator
38 {
39
40   public FeatureComparator()
41   {
42   }
43
44   public int compare(Object a, Object b)
45   {
46     FeatureTrack x = (FeatureTrack) a;
47     FeatureTrack y = (FeatureTrack) b;
48
49     String typea = x.getType();
50     String typeb = y.getType();
51
52     if (!typea.equals(typeb))
53       return typea.compareTo(typeb);
54
55     List s1 = x.getSegments();
56     List s2 = y.getSegments();
57
58     Iterator iter1 = s1.iterator();
59     Iterator iter2 = s2.iterator();
60
61     while (iter1.hasNext())
62     {
63       Segment seg1 = (Segment) iter1.next();
64       int start1 = seg1.getStart();
65
66       while (iter2.hasNext())
67       {
68         Segment seg2 = (Segment) iter2.next();
69         int start2 = seg2.getStart();
70
71         if (start1 < start2)
72         {
73           return -1;
74         }
75         if (start1 > start2)
76         {
77           return 1;
78         }
79
80       }
81     }
82
83     return 0;
84
85   }
86
87 }