Debian Codegraph @

src/dr/evolution/util/Taxa.java

raw symbols SCIP
1 /*
2 * Taxa.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.Identifiable;
29
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Iterator;
33 import java.util.List;
34
35 /**
36 * Class for a list of taxa.
37 *
38 * @version $Id: Taxa.java,v 1.29 2006/09/05 13:29:34 rambaut Exp $
39 *
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 */
43 public class Taxa implements MutableTaxonList, Identifiable, Comparable<Taxa> {
44
45 private final ArrayList<MutableTaxonListListener> mutableTaxonListListeners = new ArrayList<MutableTaxonListListener>();
46 ArrayList<Taxon> taxa = new ArrayList<Taxon>();
47
48 private String id = null;
49
50 public Taxa() {
51 }
52
53 public Taxa(String id) {
54 this.id = id;
55 }
56
57 public Taxa(TaxonList list) {
58 addTaxa(list);
59 }
60
61 public Taxa(Collection<Taxon> taxa) {
62 addTaxa(taxa);
63 }
64
65 /**
66 * Adds the given taxon and returns its index. If the taxon is already in the list then it is not
67 * added but it returns its index.
68 *
69 * @param taxon the taxon to be added
70 */
71 public int addTaxon(Taxon taxon) {
72 int index = getTaxonIndex(taxon);
73 if (index == -1) {
74 taxa.add(taxon);
75 fireTaxonAdded(taxon);
76 index = taxa.size() - 1;
77 }
78 return index;
79 }
80
81 /** Removes a taxon of the given name and returns true if successful. */
82 public boolean removeTaxon(Taxon taxon) {
83 boolean success = taxa.remove(taxon);
84 if (success) {
85 fireTaxonRemoved(taxon);
86 }
87 return success;
88 }
89
90 public void addTaxa(TaxonList taxa) {
91 for(int nt = 0; nt < taxa.getTaxonCount(); ++nt) {
92 addTaxon(taxa.getTaxon(nt));
93 }
94 }
95
96 public void addTaxa(Collection<Taxon> taxa) {
97 for(Taxon taxon : taxa) {
98 addTaxon(taxon);
99 }
100 }
101
102 public void removeTaxa(TaxonList taxa) {
103 for(int nt = 0; nt < taxa.getTaxonCount(); ++nt) {
104 removeTaxon(taxa.getTaxon(nt));
105 }
106 }
107
108 public void removeTaxa(Collection<Taxon> taxa) {
109 for(Taxon taxon : taxa) {
110 removeTaxon(taxon);
111 }
112 }
113
114 public void removeAllTaxa() {
115 taxa.clear();
116 fireTaxonRemoved(null);
117 }
118
119 /**
120 * @return a count of the number of taxa in the list.
121 */
122 public int getTaxonCount() {
123 return taxa.size();
124 }
125
126 /**
127 * @return the ith taxon.
128 */
129 public Taxon getTaxon(int taxonIndex) {
130 return taxa.get(taxonIndex);
131 }
132
133 /**
134 * @return the ID of the ith taxon.
135 */
136 public String getTaxonId(int taxonIndex) {
137 return (taxa.get(taxonIndex)).getId();
138 }
139
140 /**
141 * Sets the unique identifier of the ith taxon.
142 */
143 public void setTaxonId(int taxonIndex, String id) {
144 (taxa.get(taxonIndex)).setId(id);
145 fireTaxaChanged();
146 }
147
148 /**
149 * returns the index of the taxon with the given id.
150 */
151 public int getTaxonIndex(String id) {
152 for (int i = 0; i < taxa.size(); i++) {
153 if (getTaxonId(i).equals(id)) return i;
154 }
155 return -1;
156 }
157
158 /**
159 * returns the index of the given taxon.
160 */
161 public int getTaxonIndex(Taxon taxon) {
162 for (int i = 0; i < taxa.size(); i++) {
163 if (getTaxon(i) == taxon) return i;
164 }
165 return -1;
166 }
167
168 public List<Taxon> asList() {
169 return new ArrayList<Taxon>(taxa);
170 }
171
172 /**
173 * returns whether the list contains this taxon
174 * @param taxon
175 * @return true if taxon is in the list
176 */
177 public boolean contains(Taxon taxon) {
178 return taxa.contains(taxon);
179 }
180
181 /**
182 * Returns true if at least 1 member of taxonList is contained in this Taxa.
183 * @param taxonList a TaxonList
184 * @return true if any of taxonList is in this Taxa
185 */
186 public boolean containsAny(TaxonList taxonList) {
187
188 for (int i = 0; i < taxonList.getTaxonCount(); i++) {
189 Taxon taxon = taxonList.getTaxon(i);
190 if (taxa.contains(taxon)) {
191 return true;
192 }
193 }
194 return false;
195 }
196
197 /**
198 * Returns true if at least 1 member of taxonList is contained in this Taxa.
199 * @param taxa a collection of taxa
200 * @return true if any of taxa is in this Taxa
201 */
202 public boolean containsAny(Collection<Taxon> taxa) {
203
204 for (Taxon taxon : taxa) {
205 if (taxa.contains(taxon)) {
206 return true;
207 }
208 }
209 return false;
210 }
211
212 /**
213 * Returns true if taxonList is a subset of the taxa in this Taxa.
214 * @param taxonList a TaxonList
215 * @return true if all of taxonList is in this Taxa
216 */
217 public boolean containsAll(TaxonList taxonList) {
218
219 for (int i = 0; i < taxonList.getTaxonCount(); i++) {
220 Taxon taxon = taxonList.getTaxon(i);
221 if (!taxa.contains(taxon)) {
222 return false;
223 }
224 }
225 return true;
226 }
227
228 /**
229 * Returns true if taxonList is a subset of the taxa in this Taxa.
230 * @param taxa a collection of taxa
231 * @return true if all of taxa is in this Taxa
232 */
233 public boolean containsAll(Collection<Taxon> taxa) {
234
235 for (Taxon taxon : taxa) {
236 if (!taxa.contains(taxon)) {
237 return false;
238 }
239 }
240 return true;
241 }
242
243 public String getId() { return id; }
244 public void setId(String id) { this.id = id; }
245
246 public int compareTo(Taxa o) {
247 return getId().compareTo(o.getId());
248 }
249
250 public String toString() { return id; }
251
252 public Iterator<Taxon> iterator() {
253 return taxa.iterator();
254 }
255
256 /**
257 * Sets an named attribute for a given taxon.
258 * @param taxonIndex the index of the taxon whose attribute is being set.
259 * @param name the name of the attribute.
260 * @param value the new value of the attribute.
261 */
262 public void setTaxonAttribute(int taxonIndex, String name, Object value) {
263 Taxon taxon = getTaxon(taxonIndex);
264 taxon.setAttribute(name, value);
265 fireTaxaChanged();
266 }
267
268 /**
269 * @return an object representing the named attributed for the given taxon.
270 * @param taxonIndex the index of the taxon whose attribute is being fetched.
271 * @param name the name of the attribute of interest.
272 */
273 public Object getTaxonAttribute(int taxonIndex, String name) {
274 Taxon taxon = getTaxon(taxonIndex);
275 return taxon.getAttribute(name);
276 }
277
278 public void addMutableTaxonListListener(MutableTaxonListListener listener) {
279 mutableTaxonListListeners.add(listener);
280 }
281
282 private void fireTaxonAdded(Taxon taxon) {
283 for (MutableTaxonListListener mutableTaxonListListener : mutableTaxonListListeners) {
284 mutableTaxonListListener.taxonAdded(this, taxon);
285 }
286 }
287
288 private void fireTaxonRemoved(Taxon taxon) {
289 for (MutableTaxonListListener mutableTaxonListListener : mutableTaxonListListeners) {
290 mutableTaxonListListener.taxonRemoved(this, taxon);
291 }
292 }
293
294 private void fireTaxaChanged() {
295 for (MutableTaxonListListener mutableTaxonListListener : mutableTaxonListListeners) {
296 mutableTaxonListListener.taxaChanged(this);
297 }
298 }
299
300 @Override
301 public boolean equals(final Object o) {
302 if (!(o instanceof TaxonList)) return false;
303 return Utils.areTaxaIdentical(this, (TaxonList)o);
304 }
305 }