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

it.tidalwave.bluebill.taxonomy.mobile.impl.SimpleTaxonomy Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - 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/mobile
 * SCM: https://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.taxonomy.mobile.impl;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.taxonomy.mobile.Taxonomy;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon.Rank;
import it.tidalwave.bluebill.taxonomy.mobile.TaxonFinder;
import it.tidalwave.netbeans.util.AsLookupSupport;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class SimpleTaxonomy extends AsLookupSupport implements Taxonomy
  {
    private static final long serialVersionUID = 65968939768498676L;

    protected final Map namesBagByLocale = new HashMap();

    protected final Map> alternateLanguageCodesMap = new HashMap>();

    protected final Map> taxonListMapByRank = new HashMap>();

    protected final Map taxonMapById = new HashMap();

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public SimpleTaxonomy (final @Nonnull Object ... instanceCapabilities)
      {
        super(instanceCapabilities);
        
        alternateLanguageCodesMap.put("en",    Arrays.asList("en_gb", "en_us", "en_ca"));
        alternateLanguageCodesMap.put("en_gb", Arrays.asList("en",    "en_us", "en_ca"));
        alternateLanguageCodesMap.put("en_us", Arrays.asList("en_ca", "en",    "en_gb"));
        alternateLanguageCodesMap.put("en_ca", Arrays.asList("en_us", "en",    "en_gb"));

        alternateLanguageCodesMap.put("fr",    Arrays.asList("fr_fr", "fr_be", "fr_ca"));
        alternateLanguageCodesMap.put("fr_fr", Arrays.asList("fr",    "fr_be", "fr_ca"));
        alternateLanguageCodesMap.put("fr_be", Arrays.asList("fr_fr", "fr",    "fr_ca"));
        alternateLanguageCodesMap.put("fr_ca", Arrays.asList("fr",    "fr_fr", "fr_be"));

        alternateLanguageCodesMap.put("it",    Arrays.asList("it_it", "it_ch"));
        alternateLanguageCodesMap.put("it_it", Arrays.asList("it",    "it_ch"));
        alternateLanguageCodesMap.put("it_ch", Arrays.asList("it",    "it_it"));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public TaxonFinder findTaxa()
      {
        return new SimpleTaxonFinderSupport() 
          {
            @Override @Nonnull
            protected List computeResults() 
              {
                if (id != null)
                  {
                    final Taxon taxon = taxonMapById.get(id);
                    return (taxon != null) ? Collections.singletonList(taxon) : Collections.emptyList();
                  }
                
                else if (rank != null)
                  {
                    return Collections.unmodifiableList(taxonListMapByRank.get(rank));                    
                  }
                
                else
                  {
                    throw new IllegalArgumentException("Can only find taxa by id or by rank");  
                  }
              } 
          };  
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected String getDisplayName (final @Nonnull Taxon taxon, @Nonnull Locale locale)
      throws NotFoundException
      {
        final String languageCode = getLanguageCode(locale);

//        System.err.println("  " + locale + " " + languageCode);
        // namesBagByLocale.containsKey(locale) seems not to work

        try
          {
            return tryDisplayName(taxon, languageCode);
          }
        catch (NotFoundException e)
          {
            final List altLanguageCodes = alternateLanguageCodesMap.get(languageCode);

            if (altLanguageCodes != null)
              {
                for (final String altLanguageCode : altLanguageCodes)
                  {
                    try
                      {
                        return tryDisplayName(taxon, altLanguageCode);
                      }
                    catch (NotFoundException e2)
                      {
                        // next round
                      }
                  }
              }
 
            else if (languageCode.length() == 5) // e.g. es_ES -> es
              {
                return tryDisplayName(taxon, languageCode.substring(0, 2));
              }

            throw e;
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private String tryDisplayName (final @Nonnull Taxon taxon, final @Nonnull String languageCode)
      throws NotFoundException
      {
        final Locale locale = SimpleTaxonomyManager.LOCALE_MAP_BY_CODE.get(languageCode);

        if (locale == null)
          {
            throw new NotFoundException();
          }

        return NotFoundException.throwWhenNull(namesBagByLocale.get(locale), "").getLocalizedName(taxon);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public Set getLocales()
      {
        return new HashSet(namesBagByLocale.keySet());
      }

//    /*******************************************************************************************************************
//     *
//     *
//     ******************************************************************************************************************/
//    @Nonnull
//    protected static String findAlternateLanguage (final @Nonnull String language)
//      {
//        String newLanguage = language;
//
//        if (newLanguage.length() == 5)
//          {
//            if (newLanguage.equalsIgnoreCase("en_ca"))
//              {
//                newLanguage = "en_us";
//              }
//            else
//              {
//                newLanguage = newLanguage.substring(0, 2);
//              }
//          }
//        else if (newLanguage.equals("en"))
//          {
//            newLanguage += "_us";
//          }
//        else if (newLanguage.equals("fr"))
//          {
//            newLanguage += "_ca";
//          }
//        //            else
//            // Don't fail now; sometimes the language might be fine.
//        //              {
//        //                  System.err.println("Cannot normalize locale: " + newLanguage);
//        //                throw new NotFoundException();
//        //              }
//
////        System.err.println("LOCALE CHANGED FROM " + language + " TO " + newLanguage);
//        return newLanguage;
//      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private static String getLanguageCode (final @Nonnull Locale locale)
      {
        String languageCode = locale.getLanguage().toLowerCase();
        final String countryCode = locale.getCountry().toLowerCase();

        if ((countryCode != null) && !countryCode.equals(""))
          {
            languageCode += "_" + countryCode;
          }
        
        return languageCode;
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy