Das client files
[jalview.git] / src / org / biojava / dasobert / dasregistry / DasCoordSysComparator.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 15.04.2004
21  * @author Andreas Prlic
22  *
23  */
24
25
26 package org.biojava.dasobert.dasregistry ;
27
28 import java.util.Comparator ;
29 import java.util.Map ;
30 import java.util.HashMap ;
31
32 import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
33
34 /** a comparator to sort DasSources 
35  * @author Andreas Prlic 
36  */
37
38
39 public abstract class DasCoordSysComparator
40     implements Comparator
41
42
43     private final String name ;
44     private static final Map COMPS_BY_NAME;
45
46
47     public DasCoordSysComparator(String str) {
48         //System.out.println("new dasSourceComparator " + str);
49         name = str ;
50     }
51    
52     public static final Comparator BY_NAME = new DasCoordSysComparator("name") {
53         protected Comparable getField(DasCoordinateSystem ds) {
54             return ds.getName();
55         }
56     };    
57
58     public static final Comparator BY_ID = new DasCoordSysComparator("id") {
59         protected Comparable getField(DasCoordinateSystem ds) {
60             return ds.getUniqueId();
61         }
62     };    
63     public static final Comparator BY_CATEGORY = new DasCoordSysComparator("category") {
64         protected Comparable getField(DasCoordinateSystem ds) {
65             return ds.getCategory();
66         }
67     };
68     public static final Comparator BY_ORGANISM = new DasCoordSysComparator("organism") {
69         protected Comparable getField(DasCoordinateSystem ds) {
70             return ds.getOrganismName();
71         }
72     };
73     public static final Comparator BY_TAXID = new DasCoordSysComparator("taxid") {
74         protected Comparable getField(DasCoordinateSystem ds) {
75             return ds.getNCBITaxId()+"";
76         }
77     };
78    
79     
80
81     static {
82         COMPS_BY_NAME = new HashMap();
83         COMPS_BY_NAME.put(BY_ID.toString(),           BY_ID);
84         COMPS_BY_NAME.put(BY_NAME.toString(),         BY_NAME);
85         COMPS_BY_NAME.put(BY_CATEGORY.toString(),     BY_CATEGORY);
86         COMPS_BY_NAME.put(BY_ORGANISM.toString(),     BY_ORGANISM);
87         COMPS_BY_NAME.put(BY_TAXID.toString(),        BY_TAXID);
88     }
89
90    
91
92     public static Comparator fromString(String name) {
93         if (COMPS_BY_NAME.containsKey(name)) {
94             return (Comparator) COMPS_BY_NAME.get(name);
95         } else {
96             throw new IllegalArgumentException("Can't compare by key " + name);
97         }
98     }
99
100     protected abstract Comparable getField(DasCoordinateSystem ds);
101
102     /** compare two DasCoordSys objects */
103     public int compare( Object a, Object b) {
104         DasCoordinateSystem x = (DasCoordinateSystem) a ;
105         DasCoordinateSystem y = (DasCoordinateSystem) b ;
106         return getField(x).compareTo(getField(y));
107     }
108
109     public String toString() {
110         return name;
111     }
112
113
114 }
115
116