0d52fd980a4359da901f494d19596da1dd338aa9
[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 package org.biojava.dasobert.dasregistry;
26
27 import java.util.*;
28
29 /**
30  * a comparator to sort DasSources
31  * 
32  * @author Andreas Prlic, Thomas Down
33  */
34
35 public abstract class DasSourceComparator implements Comparator
36 {
37
38   private final String name;
39
40   private static final Map COMPS_BY_NAME;
41
42   private static final int TWODAYS = 1000 * 60 * 60 * 24 * 2;
43
44   public DasSourceComparator(String str)
45   {
46     // System.out.println("new dasSourceComparator " + str);
47     name = str;
48   }
49
50   public static final Comparator BY_ID = new DasSourceComparator("id")
51   {
52     protected Comparable getField(DasSource ds)
53     {
54       return ds.getId();
55     }
56   };
57
58   public static final Comparator BY_NICKNAME = new DasSourceComparator(
59           "nickname")
60   {
61     protected Comparable getField(DasSource ds)
62     {
63       return ds.getNickname();
64     }
65   };
66
67   public static final Comparator BY_STATUS = new DasSourceComparator(
68           "status")
69   {
70     protected Comparable getField(DasSource ds)
71     {
72
73       Date now = new Date();
74
75       if (ds.getLeaseDate().getTime() < (now.getTime() - TWODAYS))
76         return new Integer(0);
77       return new Integer(1);
78     }
79   };
80
81   public static final Comparator BY_REGISTER_DATE = new DasSourceComparator(
82           "registerdate")
83   {
84     protected Comparable getField(DasSource ds)
85     {
86       return ds.getRegisterDate();
87     }
88   };
89
90   public static final Comparator BY_LEASE_DATE = new DasSourceComparator(
91           "leasedate")
92   {
93     protected Comparable getField(DasSource ds)
94     {
95       return ds.getLeaseDate();
96     }
97   };
98
99   public static final Comparator BY_URL = new DasSourceComparator("url")
100   {
101     protected Comparable getField(DasSource ds)
102     {
103       return ds.getUrl();
104     }
105   };
106
107   public static final Comparator BY_ADMIN_EMAIL = new DasSourceComparator(
108           "adminemail")
109   {
110     protected Comparable getField(DasSource ds)
111     {
112       return ds.getAdminemail();
113     }
114   };
115
116   public static final Comparator BY_DESCRIPTION = new DasSourceComparator(
117           "description")
118   {
119     protected Comparable getField(DasSource ds)
120     {
121       return ds.getDescription();
122     }
123   };
124
125   public static final Comparator BY_CAPABILITIES = new DasSourceComparator(
126           "capabilities")
127   {
128     protected Comparable getField(DasSource ds)
129     {
130       String[] caps = ds.getCapabilities();
131       return caps.length == 0 ? "" : caps[0];
132     }
133   };
134
135   public static final Comparator BY_COORDINATE_SYSTEM = new DasSourceComparator(
136           "coordinateSystem")
137   {
138     protected Comparable getField(DasSource ds)
139     {
140       DasCoordinateSystem[] dcss = ds.getCoordinateSystem();
141       return dcss.length == 0 ? "" : dcss[0].toString();
142     }
143   };
144
145   static
146   {
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);
156     COMPS_BY_NAME
157             .put(BY_COORDINATE_SYSTEM.toString(), BY_COORDINATE_SYSTEM);
158     COMPS_BY_NAME.put(BY_STATUS.toString(), BY_STATUS);
159
160   }
161
162   public static Comparator fromString(String name)
163   {
164     if (COMPS_BY_NAME.containsKey(name))
165     {
166       return (Comparator) COMPS_BY_NAME.get(name);
167     }
168     else
169     {
170       throw new IllegalArgumentException("Can't compare by key " + name);
171     }
172   }
173
174   protected abstract Comparable getField(DasSource ds);
175
176   /** compare two DasSource objects */
177   public int compare(Object a, Object b)
178   {
179
180     DasSource x = (DasSource) a;
181     DasSource y = (DasSource) b;
182     return getField(x).compareTo(getField(y));
183   }
184
185   public String toString()
186   {
187     return name;
188   }
189
190 }