it.tidalwave.bluebill.taxonomy.TaxonomyComparator Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Resources - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it
* SCM: https://java.net/hg/bluebill~resources-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import it.tidalwave.bluebill.taxonomy.spi.DefaultTaxonomyTraverser;
/**
* FIXME: move to Taxonomy API
*
* @author fritz
*/
public class TaxonomyComparator // FIXME: bad name, it's not a Comparator. Rename into TaxonDifferenceSetFactory.
{
private final List differenceList = new ArrayList();
public TaxonomyComparator (final @Nonnull Taxonomy taxonomy,
final @Nonnull Taxonomy otherTaxonomy)
{
// FIXME: taxonomy.as(TaxonomyTraverser).accept(...)
final TaxonomyTraverser traverser = new DefaultTaxonomyTraverser(taxonomy);
traverser.accept(new TaxonomyVisitorSupport()
{
@Override
public void preVisit (final @Nonnull Taxon taxon)
{
final List extends Taxon> synonyms = taxon.findSynonyms().results();
if (synonyms.isEmpty())
{
// TODO: missing counterpart
}
else if (synonyms.size() == 1)
{
final TaxonDifferenceSet taxonDifferenceSet = new TaxonDifferenceSet(taxon, synonyms.iterator().next());
if (!taxonDifferenceSet.isEmpty())
{
differenceList.add(taxonDifferenceSet);
}
}
else // FIXME: indeed you should only retain the one of the target taxonomy
{
System.err.println("WARNING Synonym count > 1 for " + taxon.getDisplayName());
// throw new RuntimeException("Synonym count > 1: " + synonyms);
}
}
});
}
@Override
public String toString()
{
final StringBuilder buffer = new StringBuilder();
for (final TaxonDifferenceSet taxonDifference : differenceList)
{
buffer.append(taxonDifference.toString()).append("\n");
}
return buffer.toString();
}
}