| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class Taxon implements Attributable, Identifiable, Comparable<Taxon> { |
| 43 |
|
| 44 |
public Taxon(String id) { |
| 45 |
setId(id); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public void setDate(Date date) { |
| 52 |
setAttribute("date", date); |
| 53 |
addDateToTimeScale(date); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 69 |
|
| 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 |
|
| 81 |
|
| 82 |
public void setLocation(Location location) { |
| 83 |
setAttribute("location", location); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 98 |
|
| 99 |
|
| 100 |
private Attributable.AttributeHelper attributes = null; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 115 |
|
| 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 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public boolean containsAttribute(String name) { |
| 130 |
return attributes != null && attributes.containsAttribute(name); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 145 |
|
| 146 |
|
| 147 |
protected String id = null; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public String getId() { |
| 153 |
return id; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 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 |
|