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

it.tidalwave.metadata.MetadataItemHolder Maven / Gradle / Ivy

/*******************************************************************************
 *
 * blueMarine - open source photo workflow
 * =======================================
 *
 * Copyright (C) 2003-2009 by Fabrizio Giudici
 * Project home page: http://bluemarine.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. 
 *
 *******************************************************************************
 *
 * $Id: MetadataItemHolder.java,v b89b0b568aeb 2010/02/17 15:26:52 fabrizio $
 *
 ******************************************************************************/
package it.tidalwave.metadata;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Date;
import java.io.Serializable;
import org.openide.util.Parameters;

/*******************************************************************************
 *
 * This class is used by many parts of this API to wrap an existing bean of 
 * metadata, in order to add some important properties:
 * 
 * 
    *
  • available which tells wether the metadata item exists or * not; as a general policy, an object representing the item is always created * (eventually empty) so the {@link #get()} method always return a non * null;
  • *
  • latestModificationTime which tells when the item has been * modified for the last time. Initially this value is set by the item provider * (e.g. if it read the item from a file, it could be the latest modification * time of the file); the value might change later if some attributes are edited. *
  • *
  • origin the origin where the idem came from (e.g. "Image", * "XMP", "Persistence")
  • *
  • sourceName the name of the {@link it.tidalwave.metadata.spi.MetadataItemSource} that * provided the item.
  • *
* * Implementors of providers will have to subclass MetadataItemHolder * to implement {@link #isAvailable()} with a specific version that matches their * own metadata item. * * API users will never have to instantiate a MetadataItemHolder as the * correct way to retrieve an instance of it is to call * {@link Metadata#findOrCreateItem(java.lang.Class, it.tidalwave.metadata.Metadata.FindOption[]) * Metadata.findOrCreateItem()}. * * This class supports {@link PropertyChangeListener}s. * * @author Fabrizio Giudici * @version $Id: MetadataItemHolder.java,v b89b0b568aeb 2010/02/17 15:26:52 fabrizio $ * ******************************************************************************/ public abstract class MetadataItemHolder implements Serializable { protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this); public static enum Mode { READ_ONLY, READ_WRITE } @Nonnull private final Bean bean; @CheckForNull private final String sourceName; @CheckForNull private final String origin; @CheckForNull private Date latestModificationTime; private boolean previousAvailable; /*************************************************************************** * * **************************************************************************/ protected final PropertyChangeListener propertyChangeListener = new PropertyChangeListener() { @Override public void propertyChange (@Nonnull final PropertyChangeEvent event) { setLatestModificationTime(TimestampProvider.Locator.findTimestampProvider().getTimestamp()); final boolean newAvailable = isAvailable(); if (previousAvailable != newAvailable) { pcs.firePropertyChange("available", previousAvailable, newAvailable); previousAvailable = newAvailable; } } }; /*************************************************************************** * * **************************************************************************/ protected MetadataItemHolder (@Nonnull final Bean bean) { this(bean, Mode.READ_WRITE); } /*************************************************************************** * * **************************************************************************/ protected MetadataItemHolder (@Nonnull final Bean bean, @Nonnull final Mode mode) { this(bean, null, null, null, mode); } /*************************************************************************** * * **************************************************************************/ protected MetadataItemHolder (@Nonnull final Bean bean, @CheckForNull final String sourceName, @CheckForNull final String origin, @CheckForNull final Date latestModificationTime) { this(bean, sourceName, origin, latestModificationTime, Mode.READ_WRITE); } /*************************************************************************** * * **************************************************************************/ protected MetadataItemHolder (@Nonnull final Bean bean, @CheckForNull final String sourceName, @CheckForNull final String origin, @CheckForNull final Date latestModificationTime, @CheckForNull final Mode mode) { Parameters.notNull("bean", bean); this.bean = bean; this.sourceName = sourceName; this.origin = origin; this.latestModificationTime = (latestModificationTime == null) ? null : new Date(latestModificationTime.getTime()); this.previousAvailable = isAvailable(); if (mode == Mode.READ_WRITE) { MetadataUtils.addPropertyChangeListener(bean, propertyChangeListener); } } /*************************************************************************** * * Returns the held bean. * * @return the bean * **************************************************************************/ @Nonnull public Bean get() { return bean; } /*************************************************************************** * * Returns the origin. * * @return the origin * **************************************************************************/ @CheckForNull public String getOrigin() { return origin; } /*************************************************************************** * * Returns the source name. * * @return the source name * **************************************************************************/ @CheckForNull public String getSourceName() { return sourceName; } /*************************************************************************** * * Returns true if the held bean is available, * false if it is void. * * @return the bean availability * **************************************************************************/ public abstract boolean isAvailable(); /*************************************************************************** * * Returns the latest modification time of this object. This property will * be updated whenever a contained item is updated. * * @return the latest modification time * **************************************************************************/ @CheckForNull public Date getLatestModificationTime() { return (latestModificationTime == null) ? null : new Date(latestModificationTime.getTime()); } /*************************************************************************** * * **************************************************************************/ public void addPropertyChangeListener (@Nonnull final PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } /*************************************************************************** * * **************************************************************************/ public void addPropertyChangeListener (@Nonnull final String propertyName, @Nonnull final PropertyChangeListener listener) { pcs.addPropertyChangeListener(propertyName, listener); } /*************************************************************************** * * **************************************************************************/ public void removePropertyChangeListener (@Nonnull final PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener); } /*************************************************************************** * * **************************************************************************/ public void removePropertyChangeListener (@Nonnull final String propertyName, @Nonnull final PropertyChangeListener listener) { pcs.removePropertyChangeListener(propertyName, listener); } /*************************************************************************** * * **************************************************************************/ @Nonnull public PropertyChangeListener[] getPropertyChangeListeners() { return pcs.getPropertyChangeListeners(); } /*************************************************************************** * * {@inheritDoc} * **************************************************************************/ @Override @Nonnull final public String toString() { return String.format("%s[%s, %s, %s]", getClass().getSimpleName(), origin, sourceName, bean); } /*************************************************************************** * * {@inheritDoc} * **************************************************************************/ @Override public boolean equals (final Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final MetadataItemHolder other = (MetadataItemHolder) obj; return this.bean == other.bean; } /*************************************************************************** * * {@inheritDoc} * **************************************************************************/ @Override public int hashCode() { return bean.hashCode(); } /*************************************************************************** * * **************************************************************************/ protected void setLatestModificationTime (@Nonnull final Date latestModificationTime) { assert latestModificationTime != null; final Date oldValue = this.latestModificationTime; this.latestModificationTime = latestModificationTime; pcs.firePropertyChange("latestModificationTime", oldValue, this.latestModificationTime); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy