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
25 package org.biojava.dasobert.dasregistry;
30 * a comparator to sort DasSources
32 * @author Andreas Prlic, Thomas Down
35 public abstract class DasSourceComparator implements Comparator
38 private final String name;
40 private static final Map COMPS_BY_NAME;
42 private static final int TWODAYS = 1000 * 60 * 60 * 24 * 2;
44 public DasSourceComparator(String str)
46 // System.out.println("new dasSourceComparator " + str);
50 public static final Comparator BY_ID = new DasSourceComparator("id")
52 protected Comparable getField(DasSource ds)
58 public static final Comparator BY_NICKNAME = new DasSourceComparator(
61 protected Comparable getField(DasSource ds)
63 return ds.getNickname();
67 public static final Comparator BY_STATUS = new DasSourceComparator(
70 protected Comparable getField(DasSource ds)
73 Date now = new Date();
75 if (ds.getLeaseDate().getTime() < (now.getTime() - TWODAYS))
76 return new Integer(0);
77 return new Integer(1);
81 public static final Comparator BY_REGISTER_DATE = new DasSourceComparator(
84 protected Comparable getField(DasSource ds)
86 return ds.getRegisterDate();
90 public static final Comparator BY_LEASE_DATE = new DasSourceComparator(
93 protected Comparable getField(DasSource ds)
95 return ds.getLeaseDate();
99 public static final Comparator BY_URL = new DasSourceComparator("url")
101 protected Comparable getField(DasSource ds)
107 public static final Comparator BY_ADMIN_EMAIL = new DasSourceComparator(
110 protected Comparable getField(DasSource ds)
112 return ds.getAdminemail();
116 public static final Comparator BY_DESCRIPTION = new DasSourceComparator(
119 protected Comparable getField(DasSource ds)
121 return ds.getDescription();
125 public static final Comparator BY_CAPABILITIES = new DasSourceComparator(
128 protected Comparable getField(DasSource ds)
130 String[] caps = ds.getCapabilities();
131 return caps.length == 0 ? "" : caps[0];
135 public static final Comparator BY_COORDINATE_SYSTEM = new DasSourceComparator(
138 protected Comparable getField(DasSource ds)
140 DasCoordinateSystem[] dcss = ds.getCoordinateSystem();
141 return dcss.length == 0 ? "" : dcss[0].toString();
147 COMPS_BY_NAME = new HashMap();
148 COMPS_BY_NAME.put(BY_ID.toString(), BY_ID);
149 COMPS_BY_NAME.put(BY_NICKNAME.toString(), BY_NICKNAME);
150 COMPS_BY_NAME.put(BY_REGISTER_DATE.toString(), BY_REGISTER_DATE);
151 COMPS_BY_NAME.put(BY_LEASE_DATE.toString(), BY_LEASE_DATE);
152 COMPS_BY_NAME.put(BY_URL.toString(), BY_URL);
153 COMPS_BY_NAME.put(BY_ADMIN_EMAIL.toString(), BY_ADMIN_EMAIL);
154 COMPS_BY_NAME.put(BY_DESCRIPTION.toString(), BY_DESCRIPTION);
155 COMPS_BY_NAME.put(BY_CAPABILITIES.toString(), BY_CAPABILITIES);
157 .put(BY_COORDINATE_SYSTEM.toString(), BY_COORDINATE_SYSTEM);
158 COMPS_BY_NAME.put(BY_STATUS.toString(), BY_STATUS);
162 public static Comparator fromString(String name)
164 if (COMPS_BY_NAME.containsKey(name))
166 return (Comparator) COMPS_BY_NAME.get(name);
170 throw new IllegalArgumentException("Can't compare by key " + name);
174 protected abstract Comparable getField(DasSource ds);
176 /** compare two DasSource objects */
177 public int compare(Object a, Object b)
180 DasSource x = (DasSource) a;
181 DasSource y = (DasSource) b;
182 return getField(x).compareTo(getField(y));
185 public String toString()