142d9965fbed5d376db62c555e04fc83f624ab7f
[jalview.git] / src / jalview / datamodel / DBRefEntry.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.datamodel;
22
23 import jalview.api.DBRefEntryI;
24
25 public class DBRefEntry implements DBRefEntryI
26 {
27   String source = "", version = "", accessionId = "";
28
29   private int startRes, endRes;
30   /**
31    * maps from associated sequence to the database sequence's coordinate system
32    */
33   Mapping map = null;
34
35   public DBRefEntry()
36   {
37
38   }
39
40   public DBRefEntry(String source, String version, String accessionId)
41   {
42     this(source, version, accessionId, null);
43   }
44
45   /**
46    * 
47    * @param source
48    *          canonical source (uppercase only)
49    * @param version
50    *          (source dependent version string)
51    * @param accessionId
52    *          (source dependent accession number string)
53    * @param map
54    *          (mapping from local sequence numbering to source accession
55    *          numbering)
56    */
57   public DBRefEntry(String source, String version, String accessionId,
58           Mapping map)
59   {
60     this.source = source.toUpperCase();
61     this.version = version;
62     this.accessionId = accessionId;
63     this.map = map;
64   }
65
66   public DBRefEntry(DBRefEntry entry)
67   {
68     this(
69             (entry.source == null ? "" : new String(entry.source)),
70             (entry.version == null ? "" : new String(entry.version)),
71             (entry.accessionId == null ? "" : new String(entry.accessionId)),
72             (entry.map == null ? null : new Mapping(entry.map)));
73   }
74
75   @Override
76   public boolean equals(Object o)
77   {
78     // TODO should also override hashCode to ensure equal objects have equal
79     // hashcodes
80     if (o == null || !(o instanceof DBRefEntry))
81     {
82       return false;
83     }
84     DBRefEntry entry = (DBRefEntry) o;
85     if (entry == this)
86     {
87       return true;
88     }
89     if (equalRef(entry)
90             && ((map == null && entry.map == null) || (map != null
91                     && entry.map != null && map.equals(entry.map))))
92     {
93       return true;
94     }
95     return false;
96   }
97
98   /**
99    * test for similar DBRef attributes, except for the map object.
100    * 
101    * @param entry
102    * @return true if source, accession and version are equal with those of entry
103    */
104   @Override
105   public boolean equalRef(DBRefEntryI entry)
106   {
107     if (entry == null)
108     {
109       return false;
110     }
111     if (entry == this)
112     {
113       return true;
114     }
115     if (entry != null
116             && (source != null && entry.getSource() != null && source
117                     .equalsIgnoreCase(entry.getSource()))
118             && (accessionId != null && entry.getAccessionId() != null && accessionId
119                     .equalsIgnoreCase(entry.getAccessionId()))
120             && (version != null && entry.getVersion() != null && version
121                     .equalsIgnoreCase(entry.getVersion())))
122     {
123       return true;
124     }
125     return false;
126   }
127
128   @Override
129   public String getSource()
130   {
131     return source;
132   }
133
134   @Override
135   public String getVersion()
136   {
137     return version;
138   }
139
140   @Override
141   public String getAccessionId()
142   {
143     return accessionId;
144   }
145
146   /**
147    * @param accessionId
148    *          the accessionId to set
149    */
150   @Override
151   public void setAccessionId(String accessionId)
152   {
153     this.accessionId = accessionId;
154   }
155
156   /**
157    * @param source
158    *          the source to set
159    */
160   @Override
161   public void setSource(String source)
162   {
163     this.source = source;
164   }
165
166   /**
167    * @param version
168    *          the version to set
169    */
170   @Override
171   public void setVersion(String version)
172   {
173     this.version = version;
174   }
175
176   /**
177    * @return the map
178    */
179   public Mapping getMap()
180   {
181     return map;
182   }
183
184   /**
185    * @param map
186    *          the map to set
187    */
188   public void setMap(Mapping map)
189   {
190     this.map = map;
191   }
192
193   public boolean hasMap()
194   {
195     return map != null;
196   }
197
198   /**
199    * 
200    * @return source+":"+accessionId
201    */
202   public String getSrcAccString()
203   {
204     return ((source != null) ? source : "") + ":"
205             + ((accessionId != null) ? accessionId : "");
206   }
207
208   @Override
209   public String toString()
210   {
211     return getSrcAccString();
212   }
213
214   public int getStartRes()
215   {
216     return startRes;
217   }
218
219   public void setStartRes(int startRes)
220   {
221     this.startRes = startRes;
222   }
223
224   public int getEndRes()
225   {
226     return endRes;
227   }
228
229   public void setEndRes(int endRes)
230   {
231     this.endRes = endRes;
232   }
233 }