Das client files
[jalview.git] / src / org / biojava / dasobert / dasregistry / DasSourceComparator.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
33 /** a comparator to sort DasSources 
34  * @author Andreas Prlic, Thomas Down
35  */
36
37
38 public abstract class DasSourceComparator
39     implements Comparator
40
41
42     private final String name ;
43     private static final Map COMPS_BY_NAME;
44
45
46     public DasSourceComparator(String str) {
47         //System.out.println("new dasSourceComparator " + str);
48         name = str ;
49     }
50    
51     public static final Comparator BY_ID = new DasSourceComparator("id") {
52         protected Comparable getField(DasSource ds) {
53             return ds.getId();
54         }
55     };    
56
57     public static final Comparator BY_NICKNAME = new DasSourceComparator("nickname") {
58         protected Comparable getField(DasSource ds) {
59             return ds.getNickname();
60         }
61     };    
62     public static final Comparator BY_REGISTER_DATE = new DasSourceComparator("registerdate") {
63         protected Comparable getField(DasSource ds) {
64             return ds.getRegisterDate();
65         }
66     };
67     public static final Comparator BY_LEASE_DATE = new DasSourceComparator("leasedate") {
68         protected Comparable getField(DasSource ds) {
69             return ds.getLeaseDate();
70         }
71     };
72     public static final Comparator BY_URL = new DasSourceComparator("url") {
73         protected Comparable getField(DasSource ds) {
74             return ds.getUrl();
75         }
76     };
77     public static final Comparator BY_ADMIN_EMAIL = new DasSourceComparator("adminemail") {
78         protected Comparable getField(DasSource ds) {
79             return ds.getAdminemail();
80         }
81     };
82     public static final Comparator BY_DESCRIPTION = new DasSourceComparator("description") {
83         protected Comparable getField(DasSource ds) {
84             return ds.getDescription();
85         }
86     };
87     public static final Comparator BY_CAPABILITIES = new DasSourceComparator("capabilities") {
88         protected Comparable getField(DasSource ds) {
89             String[] caps = ds.getCapabilities();
90             return caps.length == 0 ? "" : caps[0];
91         }
92     };
93     public static final Comparator BY_COORDINATE_SYSTEM = new DasSourceComparator("coordinateSystem") {
94         protected Comparable getField(DasSource ds) {
95             DasCoordinateSystem[] dcss = ds.getCoordinateSystem();
96             return dcss.length == 0 ? "" : dcss[0].toString();
97         }
98     };
99
100     static {
101         COMPS_BY_NAME = new HashMap();
102         COMPS_BY_NAME.put(BY_ID.toString(),                BY_ID);
103         COMPS_BY_NAME.put(BY_NICKNAME.toString(),          BY_NICKNAME);
104         COMPS_BY_NAME.put(BY_REGISTER_DATE.toString(),     BY_REGISTER_DATE);
105         COMPS_BY_NAME.put(BY_LEASE_DATE.toString(),        BY_LEASE_DATE);
106         COMPS_BY_NAME.put(BY_URL.toString(),               BY_URL);
107         COMPS_BY_NAME.put(BY_ADMIN_EMAIL.toString(),       BY_ADMIN_EMAIL);
108         COMPS_BY_NAME.put(BY_DESCRIPTION.toString(),       BY_DESCRIPTION);
109         COMPS_BY_NAME.put(BY_CAPABILITIES.toString(),      BY_CAPABILITIES);
110         COMPS_BY_NAME.put(BY_COORDINATE_SYSTEM.toString(), BY_COORDINATE_SYSTEM);
111     }
112
113    
114
115     public static Comparator fromString(String name) {
116         if (COMPS_BY_NAME.containsKey(name)) {
117             return (Comparator) COMPS_BY_NAME.get(name);
118         } else {
119             throw new IllegalArgumentException("Can't compare by key " + name);
120         }
121     }
122
123     protected abstract Comparable getField(DasSource ds);
124
125     /** compare two DasSource objects */
126     public int compare( Object a, Object b) {
127         
128         DasSource x = (DasSource) a ;
129         DasSource y = (DasSource) b ;
130         return getField(x).compareTo(getField(y));
131     }
132
133     public String toString() {
134         return name;
135     }
136
137
138 }
139
140