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