p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / name / 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.editor.name;
11
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18
19 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
20 import eu.etaxonomy.cdm.model.taxon.Synonym;
21 import eu.etaxonomy.cdm.model.taxon.Taxon;
22 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
23 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
24
25 /**
26 * Retrieves the elements of a <code>Taxon</code>'s synonymy list, including misapplied names,
27 * using an iterator.
28 * <p>
29 * Note: does not return an entry for the <code>Taxon</code> itself.
30 * </p>
31 *
32 * @author p.ciardelli
33 * @created 09.05.2008
34 * @version 1.0
35 */
36 public class IterableSynonymyList implements Iterable<TaxonBase> {
37 private static final Logger logger = Logger
38 .getLogger(IterableSynonymyList.class);
39
40 private Taxon taxon;
41
42 public IterableSynonymyList(Taxon taxon) {
43 this.taxon = taxon;
44 }
45
46 public Iterator<TaxonBase> iterator() {
47 return new TaxonIterator(taxon);
48 }
49
50 class TaxonIterator implements Iterator<TaxonBase> {
51
52 List<TaxonBase> synonymyList;
53 int index = 0;
54
55 public TaxonIterator(Taxon taxon) {
56 synonymyList = new ArrayList<TaxonBase>();
57 HomotypicalGroup homotypicGroup = taxon.getHomotypicGroup();
58
59 if (homotypicGroup != null) {
60 List<Synonym> homotypicSynonyms = homotypicGroup.getSynonymsInGroup(taxon.getSec());
61 for (Synonym synonym : homotypicSynonyms) {
62
63 // Make sure synonym belongs to the taxon
64 if (synonym.getAcceptedTaxa().contains(taxon)) {
65 synonymyList.add(synonym);
66 }
67 }
68 }
69
70 List<HomotypicalGroup> heterotypicGroups = taxon.getHeterotypicSynonymyGroups();
71 for (HomotypicalGroup heterotypicGroup : heterotypicGroups) {
72
73 // Make sure this is not the taxon's homotypic group
74 if (!heterotypicGroup.equals(homotypicGroup)) {
75
76 List<Synonym> heterotypicSynonyms = heterotypicGroup.
77 getSynonymsInGroup(taxon.getSec());
78 for (Synonym synonym : heterotypicSynonyms) {
79
80 // Make sure synonym belongs to the taxon
81 if (synonym.getAcceptedTaxa().contains(taxon)) {
82 synonymyList.add(synonym);
83 }
84 }
85 }
86 }
87
88 Set<Taxon> misappliedNames = taxon.getMisappliedNames();
89 for (Taxon misappliedName : misappliedNames) {
90 synonymyList.add(misappliedName);
91 }
92
93 }
94
95 public boolean hasNext() {
96 if (synonymyList.size() == index) {
97 return false;
98 }
99 return true;
100 }
101
102 public TaxonBase next() {
103 TaxonBase next = synonymyList.get(index);
104 index++;
105 return next;
106 }
107
108 public void remove() {}
109 }
110 }