Formatting
[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
42   public DasSourceComparator(String str)
43   {
44     //System.out.println("new dasSourceComparator " + str);
45     name = str;
46   }
47
48   public static final Comparator BY_ID = new DasSourceComparator("id")
49   {
50     protected Comparable getField(DasSource ds)
51     {
52       return ds.getId();
53     }
54   };
55
56   public static final Comparator BY_NICKNAME = new DasSourceComparator(
57       "nickname")
58   {
59     protected Comparable getField(DasSource ds)
60     {
61       return ds.getNickname();
62     }
63   };
64   public static final Comparator BY_REGISTER_DATE = new DasSourceComparator(
65       "registerdate")
66   {
67     protected Comparable getField(DasSource ds)
68     {
69       return ds.getRegisterDate();
70     }
71   };
72   public static final Comparator BY_LEASE_DATE = new DasSourceComparator(
73       "leasedate")
74   {
75     protected Comparable getField(DasSource ds)
76     {
77       return ds.getLeaseDate();
78     }
79   };
80   public static final Comparator BY_URL = new DasSourceComparator("url")
81   {
82     protected Comparable getField(DasSource ds)
83     {
84       return ds.getUrl();
85     }
86   };
87   public static final Comparator BY_ADMIN_EMAIL = new DasSourceComparator(
88       "adminemail")
89   {
90     protected Comparable getField(DasSource ds)
91     {
92       return ds.getAdminemail();
93     }
94   };
95   public static final Comparator BY_DESCRIPTION = new DasSourceComparator(
96       "description")
97   {
98     protected Comparable getField(DasSource ds)
99     {
100       return ds.getDescription();
101     }
102   };
103   public static final Comparator BY_CAPABILITIES = new DasSourceComparator(
104       "capabilities")
105   {
106     protected Comparable getField(DasSource ds)
107     {
108       String[] caps = ds.getCapabilities();
109       return caps.length == 0 ? "" : caps[0];
110     }
111   };
112   public static final Comparator BY_COORDINATE_SYSTEM = new DasSourceComparator(
113       "coordinateSystem")
114   {
115     protected Comparable getField(DasSource ds)
116     {
117       DasCoordinateSystem[] dcss = ds.getCoordinateSystem();
118       return dcss.length == 0 ? "" : dcss[0].toString();
119     }
120   };
121
122   static
123   {
124     COMPS_BY_NAME = new HashMap();
125     COMPS_BY_NAME.put(BY_ID.toString(), BY_ID);
126     COMPS_BY_NAME.put(BY_NICKNAME.toString(), BY_NICKNAME);
127     COMPS_BY_NAME.put(BY_REGISTER_DATE.toString(), BY_REGISTER_DATE);
128     COMPS_BY_NAME.put(BY_LEASE_DATE.toString(), BY_LEASE_DATE);
129     COMPS_BY_NAME.put(BY_URL.toString(), BY_URL);
130     COMPS_BY_NAME.put(BY_ADMIN_EMAIL.toString(), BY_ADMIN_EMAIL);
131     COMPS_BY_NAME.put(BY_DESCRIPTION.toString(), BY_DESCRIPTION);
132     COMPS_BY_NAME.put(BY_CAPABILITIES.toString(), BY_CAPABILITIES);
133     COMPS_BY_NAME.put(BY_COORDINATE_SYSTEM.toString(), BY_COORDINATE_SYSTEM);
134   }
135
136   public static Comparator fromString(String name)
137   {
138     if (COMPS_BY_NAME.containsKey(name))
139     {
140       return (Comparator) COMPS_BY_NAME.get(name);
141     }
142     else
143     {
144       throw new IllegalArgumentException("Can't compare by key " + name);
145     }
146   }
147
148   protected abstract Comparable getField(DasSource ds);
149
150   /** compare two DasSource objects */
151   public int compare(Object a, Object b)
152   {
153
154     DasSource x = (DasSource) a;
155     DasSource y = (DasSource) b;
156     return getField(x).compareTo(getField(y));
157   }
158
159   public String toString()
160   {
161     return name;
162   }
163
164 }