(JAL-1016) noted position where race condition occurs
[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 package org.biojava.dasobert.dasregistry;
26
27 import java.util.*;
28
29 /**
30  * a comparator to sort DasSources
31  * 
32  * @author Andreas Prlic
33  */
34
35 public abstract class DasCoordSysComparator implements Comparator
36 {
37
38   private final String name;
39
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
64   public static final Comparator BY_CATEGORY = new DasCoordSysComparator(
65           "category")
66   {
67     protected Comparable getField(DasCoordinateSystem ds)
68     {
69       return ds.getCategory();
70     }
71   };
72
73   public static final Comparator BY_ORGANISM = new DasCoordSysComparator(
74           "organism")
75   {
76     protected Comparable getField(DasCoordinateSystem ds)
77     {
78       return ds.getOrganismName();
79     }
80   };
81
82   public static final Comparator BY_TAXID = new DasCoordSysComparator(
83           "taxid")
84   {
85     protected Comparable getField(DasCoordinateSystem ds)
86     {
87       return ds.getNCBITaxId() + "";
88     }
89   };
90
91   static
92   {
93     COMPS_BY_NAME = new HashMap();
94     COMPS_BY_NAME.put(BY_ID.toString(), BY_ID);
95     COMPS_BY_NAME.put(BY_NAME.toString(), BY_NAME);
96     COMPS_BY_NAME.put(BY_CATEGORY.toString(), BY_CATEGORY);
97     COMPS_BY_NAME.put(BY_ORGANISM.toString(), BY_ORGANISM);
98     COMPS_BY_NAME.put(BY_TAXID.toString(), BY_TAXID);
99   }
100
101   public static Comparator fromString(String name)
102   {
103     if (COMPS_BY_NAME.containsKey(name))
104     {
105       return (Comparator) COMPS_BY_NAME.get(name);
106     }
107     else
108     {
109       throw new IllegalArgumentException("Can't compare by key " + name);
110     }
111   }
112
113   protected abstract Comparable getField(DasCoordinateSystem ds);
114
115   /** compare two DasCoordSys objects */
116   public int compare(Object a, Object b)
117   {
118     DasCoordinateSystem x = (DasCoordinateSystem) a;
119     DasCoordinateSystem y = (DasCoordinateSystem) b;
120     return getField(x).compareTo(getField(y));
121   }
122
123   public String toString()
124   {
125     return name;
126   }
127
128 }