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

it.tidalwave.bluebill.taxonomy.TaxonDifferenceSet 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 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 Taxon getTaxonomyConcept();

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

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

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

    private final List differences = new ArrayList();

    public TaxonDifferenceSet (final @Nonnull Taxon taxon, final @Nonnull Taxon 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 Taxon 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 Taxon 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 (final @Nonnull String s1, final @Nonnull String s2)
      {
        return (s1 == null) ? (s2 == null) : s1.equals(s2);
      }

    private static String getPath (final @Nonnull Taxon 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