Formatting
[jalview.git] / src / org / biojava / dasobert / dasregistry / DasCoordSysComparator.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
32  */
33
34
35 public abstract class DasCoordSysComparator
36     implements Comparator
37 {
38
39   private final String name;
40   private static final Map COMPS_BY_NAME;
41
42   public DasCoordSysComparator(String str)
43   {
44     //System.out.println("new dasSourceComparator " + str);
45     name = str;
46   }
47
48   public static final Comparator BY_NAME = new DasCoordSysComparator("name")
49   {
50     protected Comparable getField(DasCoordinateSystem ds)
51     {
52       return ds.getName();
53     }
54   };
55
56   public static final Comparator BY_ID = new DasCoordSysComparator("id")
57   {
58     protected Comparable getField(DasCoordinateSystem ds)
59     {
60       return ds.getUniqueId();
61     }
62   };
63   public static final Comparator BY_CATEGORY = new DasCoordSysComparator(
64       "category")
65   {
66     protected Comparable getField(DasCoordinateSystem ds)
67     {
68       return ds.getCategory();
69     }
70   };
71   public static final Comparator BY_ORGANISM = new DasCoordSysComparator(
72       "organism")
73   {
74     protected Comparable getField(DasCoordinateSystem ds)
75     {
76       return ds.getOrganismName();
77     }
78   };
79   public static final Comparator BY_TAXID = new DasCoordSysComparator("taxid")
80   {
81     protected Comparable getField(DasCoordinateSystem ds)
82     {
83       return ds.getNCBITaxId() + "";
84     }
85   };
86
87   static
88   {
89     COMPS_BY_NAME = new HashMap();
90     COMPS_BY_NAME.put(BY_ID.toString(), BY_ID);
91     COMPS_BY_NAME.put(BY_NAME.toString(), BY_NAME);
92     COMPS_BY_NAME.put(BY_CATEGORY.toString(), BY_CATEGORY);
93     COMPS_BY_NAME.put(BY_ORGANISM.toString(), BY_ORGANISM);
94     COMPS_BY_NAME.put(BY_TAXID.toString(), BY_TAXID);
95   }
96
97   public static Comparator fromString(String name)
98   {
99     if (COMPS_BY_NAME.containsKey(name))
100     {
101       return (Comparator) COMPS_BY_NAME.get(name);
102     }
103     else
104     {
105       throw new IllegalArgumentException("Can't compare by key " + name);
106     }
107   }
108
109   protected abstract Comparable getField(DasCoordinateSystem ds);
110
111   /** compare two DasCoordSys objects */
112   public int compare(Object a, Object b)
113   {
114     DasCoordinateSystem x = (DasCoordinateSystem) a;
115     DasCoordinateSystem y = (DasCoordinateSystem) b;
116     return getField(x).compareTo(getField(y));
117   }
118
119   public String toString()
120   {
121     return name;
122   }
123
124 }