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

it.tidalwave.bluemarine2.model.spi.MetadataSupport Maven / Gradle / Ivy

There is a newer version: 1.1-ALPHA-1
Show newest version
/*
 * #%L
 * *********************************************************************************************************************
 *
 * blueMarine2 - Semantic Media Center
 * http://bluemarine2.tidalwave.it - git clone https://[email protected]/tidalwave/bluemarine2-src.git
 * %%
 * Copyright (C) 2015 - 2016 Tidalwave s.a.s. (http://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$
 *
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.bluemarine2.model.spi;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.nio.file.Path;
import it.tidalwave.util.Key;
import it.tidalwave.bluemarine2.model.MediaItem;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@RequiredArgsConstructor @ToString(of = "properties") @Slf4j
public class MetadataSupport implements MediaItem.Metadata
  {
    @Getter @Nonnull
    protected final Path path;

    protected final Map, Object> properties = new HashMap<>();

    /*******************************************************************************************************************
     *
     * {@inheritDocs}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public  Optional get (final @Nonnull Key key)
      {
        return Optional.ofNullable(((T)properties.get(key)));
      }

    /*******************************************************************************************************************
     *
     * {@inheritDocs}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public  T getAll (final @Nonnull Key key)
      {
        final T list = (T)properties.get(key);
        return (list != null) ? list : (T)Collections.emptyList();
      }

    /*******************************************************************************************************************
     *
     * {@inheritDocs}
     *
     ******************************************************************************************************************/
    @Override
    public boolean containsKey (final @Nonnull Key key)
      {
        return properties.containsKey(key);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDocs}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Set> getKeys()
      {
        return Collections.unmodifiableSet(properties.keySet());
      }

    /*******************************************************************************************************************
     *
     * {@inheritDocs}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Set, ?>> getEntries()
      {
        return Collections.unmodifiableSet(properties.entrySet());
      }

    /*******************************************************************************************************************
     *
     * {@inheritDocs}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public  MediaItem.Metadata with (final @Nonnull Key key, final @Nonnull T value)
      {
        final MetadataSupport clone = new MetadataSupport(path);
        clone.properties.putAll(this.properties);
        clone.put(key, value);
        return clone;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDocs}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public  MediaItem.Metadata with (final @Nonnull Key key, final @Nonnull Optional value)
      {
        return !value.isPresent() ? this : with(key, value.get()); // FIXME: use lambda
      }

    /*******************************************************************************************************************
     *
     * FIXME: remove this, make it truly immutable.
     *
     ******************************************************************************************************************/
    protected  void put (final @Nonnull Key key, final @Nullable V value)
      {
        if (value != null)
          {
            properties.put(key, value);
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy