Debian Codegraph @

src/dr/evolution/util/Taxon.java

raw symbols SCIP
1 /*
2 * Taxon.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.util;
27
28 import dr.util.Attributable;
29 import dr.util.Identifiable;
30
31 import java.util.ArrayList;
32 import java.util.Iterator;
33
34 /**
35 * Class for data about a taxon.
36 *
37 * @version $Id: Taxon.java,v 1.24 2006/09/05 13:29:34 rambaut Exp $
38 *
39 * @author Andrew Rambaut
40 * @author Alexei Drummond
41 */
42 public class Taxon implements Attributable, Identifiable, Comparable<Taxon> {
43
44 public Taxon(String id) {
45 setId(id);
46 }
47
48 /**
49 * Sets a date for this taxon.
50 */
51 public void setDate(Date date) {
52 setAttribute("date", date);
53 addDateToTimeScale(date);
54 }
55
56 /**
57 * @return a date for this taxon.
58 */
59 public Date getDate() {
60 Object date = getAttribute("date");
61 if (date != null && date instanceof Date) {
62 return (Date)date;
63 }
64 return null;
65 }
66
67 /**
68 * @return a height for this taxon.
69 * This gets the height from the globally defined timescale of dates for all Taxa.
70 */
71 public double getHeight() {
72 Object date = getAttribute("date");
73 if (date != null && date instanceof Date) {
74 return getHeightFromDate((Date)date);
75 }
76 return 0.0;
77 }
78
79 /**
80 * Sets a location for this taxon.
81 */
82 public void setLocation(Location location) {
83 setAttribute("location", location);
84 }
85
86 /**
87 * @return a location for this taxon.
88 */
89 public Location getLocation() {
90 Object location = getAttribute("location");
91 if (location != null && location instanceof Location) {
92 return (Location)location;
93 }
94 return null;
95 }
96 // **************************************************************
97 // Attributable IMPLEMENTATION
98 // **************************************************************
99
100 private Attributable.AttributeHelper attributes = null;
101
102 /**
103 * Sets an named attribute for this object.
104 * @param name the name of the attribute.
105 * @param value the new value of the attribute.
106 */
107 public void setAttribute(String name, Object value) {
108 if (attributes == null)
109 attributes = new Attributable.AttributeHelper();
110 attributes.setAttribute(name, value);
111 }
112
113 /**
114 * @return an object representing the named attributed for this object.
115 * @param name the name of the attribute of interest.
116 */
117 public Object getAttribute(String name) {
118 if (attributes == null)
119 return null;
120 else
121 return attributes.getAttribute(name);
122 }
123
124 /**
125 * if attributes == null, return false
126 * @param name attribute name
127 * @return boolean whether contains attribute by given its name
128 */
129 public boolean containsAttribute(String name) {
130 return attributes != null && attributes.containsAttribute(name);
131 }
132
133 /**
134 * @return an iterator of the attributes that this object has.
135 */
136 public Iterator<String> getAttributeNames() {
137 if (attributes == null)
138 return new ArrayList<String>().iterator();
139 else
140 return attributes.getAttributeNames();
141 }
142
143 // **************************************************************
144 // Identifiable IMPLEMENTATION
145 // **************************************************************
146
147 protected String id = null;
148
149 /**
150 * @return the id.
151 */
152 public String getId() {
153 return id;
154 }
155
156 /**
157 * Sets the id.
158 */
159 public void setId(String id) {
160 this.id = id;
161 }
162
163 public String toString() { return getId(); }
164
165 @Override
166 public boolean equals(final Object o) {
167 return getId().equals(((Taxon)o).getId());
168 }
169
170 @Override
171 public int hashCode() {
172 return getId().hashCode();
173 }
174
175 // **************************************************************
176 // Comparable IMPLEMENTATION
177 // **************************************************************
178
179 public int compareTo(Taxon o) {
180 return getId().compareTo(o.getId());
181 }
182
183
184 private static void addDateToTimeScale(Date date) {
185 if (date != null && (mostRecentDate == null || date.after(mostRecentDate))) {
186 mostRecentDate = date;
187 timeScale = null;
188 }
189 }
190
191 public static double getHeightFromDate(Date date) {
192
193 if (timeScale == null) {
194 Date mostRecent = mostRecentDate;
195 if (mostRecent == null) {
196 mostRecent = dr.evolution.util.Date.createRelativeAge(0.0, date.getUnits());
197 }
198
199 timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
200 }
201
202 return timeScale.convertTime(date.getTimeValue(), date);
203 }
204
205 public static Date getMostRecentDate() {
206 return mostRecentDate;
207 }
208
209 private static dr.evolution.util.Date mostRecentDate = null;
210 private static TimeScale timeScale = null;
211 }
212