All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.taxonomy.TaxonDifferenceSet Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.tidalwave.bluebill.taxonomy;

import it.tidalwave.bluebill.taxonomy.TaxonomyConcept;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nonnull;
import org.openrdf.elmo.Entity;


abstract class Difference
  {
    public enum Type
      {
        PATH,
        NAME
      }

    @Nonnull
    private final Type type;

    @Nonnull
    public abstract TaxonomyConcept getTaxonomyConcept();

    public Difference (@Nonnull final Type type)
      {
        this.type = type;
      }

    @Nonnull
    public Type getType()
      {
        return type;
      }
  }

public class TaxonDifferenceSet implements Iterable
  {
    @Nonnull
    private final TaxonomyConcept taxon;
    
    @Nonnull
    private final TaxonomyConcept otherTaxon;

    private final List differences = new ArrayList();

    public TaxonDifferenceSet (@Nonnull final TaxonomyConcept taxon,
                            @Nonnull final TaxonomyConcept otherTaxon)
      {
        this.taxon = taxon;
        this.otherTaxon = otherTaxon;

        final String path1 = getPath(taxon);
        final String path2 = getPath(otherTaxon);

        // FIXME: poor way - use getType()
        final boolean isSpeciesOrSubSpecies = Character.isLowerCase(taxon.getDisplayName().charAt(0));

        if (!path1.equals(path2))
          {
            differences.add(new Difference(Difference.Type.PATH)
              {
                @Override
                public TaxonomyConcept getTaxonomyConcept() 
                  {
                    return taxon;
                  }

                @Override
                public String toString()
                  {
                    return path1 + " -> " + path2;
                  }
              });
          }

        if (isSpeciesOrSubSpecies && !nullEquals(taxon.getDisplayName(Locale.ENGLISH), otherTaxon.getDisplayName(Locale.ENGLISH)))
          {
            differences.add(new Difference(Difference.Type.NAME)
              {
                @Override
                public TaxonomyConcept getTaxonomyConcept()
                  {
                    return taxon;
                  }

                @Override
                public String toString()
                  {
                    return String.format("%s -> %s", taxon.getDisplayName(Locale.ENGLISH), otherTaxon.getDisplayName(Locale.ENGLISH));
                  }
              });
          }
      }

    public boolean isEmpty()
      {
        return differences.isEmpty();
      }
    
    private static boolean nullEquals (@Nonnull final String s1, @Nonnull final String s2)
      {
        return (s1 == null) ? (s2 == null) : s1.equals(s2);
      }

    private static String getPath (@Nonnull final TaxonomyConcept taxon)
      {
        return taxon.as(Entity.class).getQName().getLocalPart().replaceAll(".*/Aves", "/Aves");
      }

    @Override
    public Iterator iterator()
      {
        return Collections.unmodifiableCollection(differences).iterator();
      }

    @Override
    public String toString()
      {
        final StringBuilder buffer = new StringBuilder();
        buffer.append(taxon.getDisplayName(Locale.ITALIAN)).append("\n"); // FIXME locale

        for (final Difference difference : differences)
          {
            buffer.append(difference.toString()).append("\n");
          }

        return buffer.toString();
      }
  }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy