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;
30 /** a comparator to sort DasSources
31 * @author Andreas Prlic, Thomas Down
35 public abstract class DasSourceComparator
39 private final String name;
40 private static final Map COMPS_BY_NAME;
41 private static final int TWODAYS = 1000 * 60 * 60 * 24 * 2;
43 public DasSourceComparator(String str)
45 //System.out.println("new dasSourceComparator " + str);
49 public static final Comparator BY_ID = new DasSourceComparator("id")
51 protected Comparable getField(DasSource ds)
57 public static final Comparator BY_NICKNAME = new DasSourceComparator(
60 protected Comparable getField(DasSource ds)
62 return ds.getNickname();
65 public static final Comparator BY_STATUS = new DasSourceComparator("status"){
66 protected Comparable getField(DasSource ds) {
68 Date now = new Date();
70 if (ds.getLeaseDate().getTime() < (now.getTime() - TWODAYS))
71 return new Integer(0);
72 return new Integer(1);
77 public static final Comparator BY_REGISTER_DATE = new DasSourceComparator(
80 protected Comparable getField(DasSource ds)
82 return ds.getRegisterDate();
85 public static final Comparator BY_LEASE_DATE = new DasSourceComparator(
88 protected Comparable getField(DasSource ds)
90 return ds.getLeaseDate();
93 public static final Comparator BY_URL = new DasSourceComparator("url")
95 protected Comparable getField(DasSource ds)
100 public static final Comparator BY_ADMIN_EMAIL = new DasSourceComparator(
103 protected Comparable getField(DasSource ds)
105 return ds.getAdminemail();
108 public static final Comparator BY_DESCRIPTION = new DasSourceComparator(
111 protected Comparable getField(DasSource ds)
113 return ds.getDescription();
116 public static final Comparator BY_CAPABILITIES = new DasSourceComparator(
119 protected Comparable getField(DasSource ds)
121 String[] caps = ds.getCapabilities();
122 return caps.length == 0 ? "" : caps[0];
125 public static final Comparator BY_COORDINATE_SYSTEM = new DasSourceComparator(
128 protected Comparable getField(DasSource ds)
130 DasCoordinateSystem[] dcss = ds.getCoordinateSystem();
131 return dcss.length == 0 ? "" : dcss[0].toString();
137 COMPS_BY_NAME = new HashMap();
138 COMPS_BY_NAME.put(BY_ID.toString(), BY_ID);
139 COMPS_BY_NAME.put(BY_NICKNAME.toString(), BY_NICKNAME);
140 COMPS_BY_NAME.put(BY_REGISTER_DATE.toString(), BY_REGISTER_DATE);
141 COMPS_BY_NAME.put(BY_LEASE_DATE.toString(), BY_LEASE_DATE);
142 COMPS_BY_NAME.put(BY_URL.toString(), BY_URL);
143 COMPS_BY_NAME.put(BY_ADMIN_EMAIL.toString(), BY_ADMIN_EMAIL);
144 COMPS_BY_NAME.put(BY_DESCRIPTION.toString(), BY_DESCRIPTION);
145 COMPS_BY_NAME.put(BY_CAPABILITIES.toString(), BY_CAPABILITIES);
146 COMPS_BY_NAME.put(BY_COORDINATE_SYSTEM.toString(), BY_COORDINATE_SYSTEM);
147 COMPS_BY_NAME.put(BY_STATUS.toString(), BY_STATUS);
151 public static Comparator fromString(String name)
153 if (COMPS_BY_NAME.containsKey(name))
155 return (Comparator) COMPS_BY_NAME.get(name);
159 throw new IllegalArgumentException("Can't compare by key " + name);
163 protected abstract Comparable getField(DasSource ds);
165 /** compare two DasSource objects */
166 public int compare(Object a, Object b)
169 DasSource x = (DasSource) a;
170 DasSource y = (DasSource) b;
171 return getField(x).compareTo(getField(y));
174 public String toString()