Merge branch 'develop' into features/JAL-3010ontologyFeatureSettings
[jalview.git] / src / org / biojava / nbio / ontology / Synonym.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 Jan 24, 2008
21  *
22  */
23
24 package org.biojava.nbio.ontology;
25
26 import java.util.Comparator;
27
28
29 public class Synonym implements Comparable<Synonym>{
30
31
32         public final static int UNKNOWN_SCOPE = -1;
33         public final static int RELATED_SYNONYM = 0;
34         public final static int EXACT_SYNONYM = 1;
35         public final static int NARROW_SYNONYM = 2;
36         public final static int BROAD_SYNONYM = 3;
37
38         int scope;
39         String category;
40         String name;
41
42         @Override
43         public String toString(){
44                 String txt = "Synonym: name:"+name+ " category:" + category + " scope: " +scope;
45                 return txt;
46         }
47
48   @Override
49   public int hashCode()
50   {
51     return toString().hashCode();
52   }
53
54   @Override
55         public boolean equals(Object obj)
56         {
57     return obj instanceof Synonym
58             && ((Synonym) obj).toString().equals(this.toString());
59         }
60
61   public final static Comparator<Synonym> COMPARATOR = new Comparator<Synonym>()
62   {
63                 @Override
64                 public int compare(Synonym a, Synonym b) {
65                         if (a == null && b == null)
66       {
67         return 0;
68       }
69       if (a == null)
70       {
71         return -1;
72       }
73       if (b == null)
74       {
75         return 1;
76       }
77       if (a.equals(b))
78       {
79         return 0;
80       }
81       String catA = a.getCategory();
82       String catB = b.getCategory();
83       if (catA == null && catB != null)
84       {
85         return 1;
86       }
87       if (catA != null && catB == null)
88       {
89         return -1;
90       }
91       if (catA != null && catB != null)
92       {
93         int comp = catA.compareToIgnoreCase(catB);
94         if (comp != 0)
95         {
96           return comp;
97         }
98       }
99       // todo check for null name
100       return a.getName().compareToIgnoreCase(b.getName());
101     }
102         };
103
104         public Synonym() {
105         }
106         public String getName() {
107                 return name;
108         }
109         public void setName(String name) {
110                 this.name = name;
111         }
112         public String getCategory() {
113                 return category;
114         }
115         public void setCategory(String category) {
116                 this.category = category;
117         }
118         public int getScope() {
119                 return scope;
120         }
121         public void setScope(int scope) {
122                 this.scope = scope;
123         }
124         @Override
125         public int compareTo(Synonym o) {
126                 return COMPARATOR.compare(this, o);
127         }
128 }