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

it.tidalwave.bluebill.mobile.taxonomy.TaxonObservable Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * 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.mobile.taxonomy;

import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import javax.annotation.Nonnull;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.observation.Observable;
import it.tidalwave.observation.simple.SimpleObservable;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.taxonomy.mobile.Taxonomy;

/***********************************************************************************************************************
 *
 * The {@link Observable} role bound to a {@link Taxon}.
 * Makes sure that only the id of a Taxon is serialized, and not the whole class with the whole Taxonomy.
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class TaxonObservable extends SimpleObservable implements Externalizable
  {
    public static final Class TaxonObservable = TaxonObservable.class;
 
    private static final long serialVersionUID = 754645623437593487L;
    
    private transient Taxon taxon;
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public TaxonObservable() // for deserialization
      {
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public TaxonObservable (final @Nonnull Taxon taxon,
                            final @Nonnull Object ... capabilities)
      {
        super(capabilities);
        this.taxon = taxon;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public void writeExternal (final @Nonnull ObjectOutput oo)
      throws IOException
      {
        oo.writeObject(taxon.getId());
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public void readExternal (final @Nonnull ObjectInput oi)
      throws IOException, ClassNotFoundException
      {
        final Id id = (Id)oi.readObject();
        try
          {
            final Taxonomy taxonomy = Locator.find(TaxonomyPreferences.class).getTaxonomy();
            taxon = taxonomy.findTaxonById(id);
          }
        catch (NotFoundException e)
          {
            throw new IOException(e.toString()); // Java 5 / Android compatibility
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    protected Lookup createLookup() 
      {
        return new ProxyLookup(Lookups.singleton(taxon), super.createLookup());
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean equals (Object o)
      {
        if ((o == null) || !(o instanceof TaxonObservable))
          {
            return false;
          }

        return taxon.equals(((TaxonObservable)o).taxon);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public int hashCode()
      {
        return taxon.hashCode();
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy