2 * BioJava development code
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,
9 * http://www.gnu.org/copyleft/lesser.html
11 * Copyright for this code is held jointly by the individual
12 * authors. These should be listed in @author doc comments.
14 * For more information on the BioJava project and its aims,
15 * or to join the biojava-l mailing list, visit the home page
18 * http://www.biojava.org/
20 * Created on 15.04.2004
21 * @author Andreas Prlic
26 package org.biojava.dasobert.dasregistry ;
28 import java.util.Comparator ;
29 import java.util.Map ;
30 import java.util.HashMap ;
33 /** a comparator to sort DasSources
34 * @author Andreas Prlic, Thomas Down
38 public abstract class DasSourceComparator
42 private final String name ;
43 private static final Map COMPS_BY_NAME;
46 public DasSourceComparator(String str) {
47 //System.out.println("new dasSourceComparator " + str);
51 public static final Comparator BY_ID = new DasSourceComparator("id") {
52 protected Comparable getField(DasSource ds) {
57 public static final Comparator BY_NICKNAME = new DasSourceComparator("nickname") {
58 protected Comparable getField(DasSource ds) {
59 return ds.getNickname();
62 public static final Comparator BY_REGISTER_DATE = new DasSourceComparator("registerdate") {
63 protected Comparable getField(DasSource ds) {
64 return ds.getRegisterDate();
67 public static final Comparator BY_LEASE_DATE = new DasSourceComparator("leasedate") {
68 protected Comparable getField(DasSource ds) {
69 return ds.getLeaseDate();
72 public static final Comparator BY_URL = new DasSourceComparator("url") {
73 protected Comparable getField(DasSource ds) {
77 public static final Comparator BY_ADMIN_EMAIL = new DasSourceComparator("adminemail") {
78 protected Comparable getField(DasSource ds) {
79 return ds.getAdminemail();
82 public static final Comparator BY_DESCRIPTION = new DasSourceComparator("description") {
83 protected Comparable getField(DasSource ds) {
84 return ds.getDescription();
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];
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();
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);
115 public static Comparator fromString(String name) {
116 if (COMPS_BY_NAME.containsKey(name)) {
117 return (Comparator) COMPS_BY_NAME.get(name);
119 throw new IllegalArgumentException("Can't compare by key " + name);
123 protected abstract Comparable getField(DasSource ds);
125 /** compare two DasSource objects */
126 public int compare( Object a, Object b) {
128 DasSource x = (DasSource) a ;
129 DasSource y = (DasSource) b ;
130 return getField(x).compareTo(getField(y));
133 public String toString() {