2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.eflora
;
11 import java
.util
.HashMap
;
12 import java
.util
.HashSet
;
16 import org
.apache
.commons
.lang
.StringUtils
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
22 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
24 public class UnmatchedLeads
{
25 private static final Logger logger
= Logger
.getLogger(UnmatchedLeads
.class);
28 private Map
<UnmatchedLeadsKey
, Set
<FeatureNode
>> map
= new HashMap
<UnmatchedLeadsKey
, Set
<FeatureNode
>>();
30 public static class UnmatchedLeadsKey
{
31 public static UnmatchedLeadsKey
NewInstance(PolytomousKey key
, String num
){
32 UnmatchedLeadsKey result
= new UnmatchedLeadsKey();
38 public static UnmatchedLeadsKey
NewInstance(String num
, String taxon
){
39 num
= (StringUtils
.isBlank(num
) ?
"" : num
+ " " );
40 return NewInstance(num
+ taxon
);
43 public static UnmatchedLeadsKey
NewInstance(String numAndTaxon
){
44 UnmatchedLeadsKey result
= new UnmatchedLeadsKey();
45 result
.numAndTaxon
= numAndTaxon
;
57 public boolean isInnerLead(){
61 public boolean isTaxonLead(){
62 return (numAndTaxon
!= null);
66 public boolean equals(Object object
){
67 if (object
== null || ! (object
instanceof UnmatchedLeadsKey
)){
70 UnmatchedLeadsKey unmatchedLeadsKey
= (UnmatchedLeadsKey
)object
;
71 if (! CdmUtils
.nullSafeEqual(this.num
, unmatchedLeadsKey
.num
)){
74 if (! CdmUtils
.nullSafeEqual(this.numAndTaxon
, unmatchedLeadsKey
.numAndTaxon
)){
77 if (! CdmUtils
.nullSafeEqual(this.key
, unmatchedLeadsKey
.key
)){
84 public int hashCode() {
85 int hashCode
= 29 * 7;
86 if(this.key
!= null && this.key
.getUuid() != null) {
87 hashCode
= hashCode
+ this.key
.getUuid().hashCode();
89 if(this.numAndTaxon
!= null ) {
90 hashCode
= hashCode
+ this.numAndTaxon
.hashCode();
92 if(this.num
!= null) {
93 hashCode
= hashCode
+ this.num
.hashCode();
99 public String
toString(){
101 if (this.num
!= null){
104 if (this.key
!= null){
105 result
+= ":" + this.key
.getUuid();
107 if (this.numAndTaxon
!= null){
108 result
+= this.numAndTaxon
;
115 //************************* FACTORY METHODS ********************************/
117 public static UnmatchedLeads
NewInstance(){
118 return new UnmatchedLeads();
121 //************************* METHODS ********************************/
124 public void addKey(UnmatchedLeadsKey key
, FeatureNode node
){
125 Set
<FeatureNode
> nodes
= map
.get(key
);
127 nodes
= new HashSet
<FeatureNode
>();
130 logger
.info("A Feature node for this key does already exist: " + key
.toString());
135 public Set
<FeatureNode
> getNodes(UnmatchedLeadsKey key
){
136 Set
<FeatureNode
> result
= new HashSet
<FeatureNode
>();
137 Set
<FeatureNode
> nodes
= map
.get(key
);
139 result
.addAll(nodes
);
144 public boolean removeNode(UnmatchedLeadsKey key
, FeatureNode matchingNode
) {
145 Set
<FeatureNode
> nodes
= map
.get(key
);
147 boolean result
= nodes
.remove(matchingNode
);
148 if (nodes
.isEmpty()){
156 public void saveToSession(IFeatureTreeService service
){
157 Set
<FeatureNode
> allNodes
= new HashSet
<FeatureNode
>();
158 for (Set
<FeatureNode
> set
:map
.values()){
159 allNodes
.addAll(set
);
161 service
.saveOrUpdateFeatureNodesAll(allNodes
);
164 //********************** toString()******************************/
167 public String
toString(){
169 for (UnmatchedLeadsKey key
: map
.keySet()){
170 result
+= (result
.equals("[")?
"":"; ") + key
.toString();