f139f9092478516ffbf3a0751625569067153906
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / model / IterableSynonymyList.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.store.model;
11
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Set;
16
17 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
18 import eu.etaxonomy.cdm.model.taxon.Synonym;
19 import eu.etaxonomy.cdm.model.taxon.Taxon;
20 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
21
22 /**
23 * Retrieves the elements of a <code>Taxon</code>'s synonymy list, including misapplied names,
24 * using an iterator.
25 * <p>
26 * Note: does not return an entry for the <code>Taxon</code> itself.
27 * </p>
28 *
29 * @author p.ciardelli
30 * @created 09.05.2008
31 * @version 1.0
32 */
33 @SuppressWarnings("unchecked")
34 public class IterableSynonymyList implements Iterable<TaxonBase> {
35
36 private Taxon taxon;
37
38 public IterableSynonymyList(Taxon taxon) {
39 this.taxon = taxon;
40 }
41
42 public Iterator<TaxonBase> iterator() {
43 return new TaxonIterator(taxon);
44 }
45
46 class TaxonIterator implements Iterator<TaxonBase> {
47
48 List<TaxonBase> synonymyList;
49 int index = 0;
50
51 public TaxonIterator(Taxon taxon) {
52 synonymyList = new ArrayList<TaxonBase>();
53 HomotypicalGroup homotypicGroup = taxon.getHomotypicGroup();
54
55 if (homotypicGroup != null) {
56 List<Synonym> homotypicSynonyms = homotypicGroup.getSynonymsInGroup(taxon.getSec());
57 for (Synonym synonym : homotypicSynonyms) {
58
59 // Make sure synonym belongs to the taxon
60 if (synonym.getAcceptedTaxa().contains(taxon)) {
61 synonymyList.add(synonym);
62 }
63 }
64 }
65
66 List<HomotypicalGroup> heterotypicGroups = taxon.getHeterotypicSynonymyGroups();
67 for (HomotypicalGroup heterotypicGroup : heterotypicGroups) {
68
69 // Make sure this is not the taxon's homotypic group
70 if (!heterotypicGroup.equals(homotypicGroup)) {
71
72 List<Synonym> heterotypicSynonyms = heterotypicGroup.
73 getSynonymsInGroup(taxon.getSec());
74 for (Synonym synonym : heterotypicSynonyms) {
75
76 // Make sure synonym belongs to the taxon
77 if (synonym.getAcceptedTaxa().contains(taxon)) {
78 synonymyList.add(synonym);
79 }
80 }
81 }
82 }
83
84 Set<Taxon> misappliedNames = taxon.getMisappliedNames();
85 for (Taxon misappliedName : misappliedNames) {
86 synonymyList.add(misappliedName);
87 }
88
89 }
90
91 public boolean hasNext() {
92 if (synonymyList.size() == index) {
93 return false;
94 }
95 return true;
96 }
97
98 public TaxonBase<?> next() {
99 TaxonBase<?> next = synonymyList.get(index);
100 index++;
101 return next;
102 }
103
104 public void remove() {}
105 }
106 }