PathAwareMediaFolderDecorator.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * blueMarine II: Semantic Media Centre
  5.  * http://tidalwave.it/projects/bluemarine2
  6.  *
  7.  * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/bluemarine2-src
  23.  * git clone https://github.com/tidalwave-it/bluemarine2-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.bluemarine2.model.impl;

  28. import javax.annotation.Nonnull;
  29. import java.nio.file.Path;
  30. import java.util.Collection;
  31. import java.util.Collections;
  32. import it.tidalwave.util.Finder;
  33. import it.tidalwave.util.MappingFinder;
  34. import it.tidalwave.role.SimpleComposite;
  35. import it.tidalwave.bluemarine2.model.MediaFolder;
  36. import it.tidalwave.bluemarine2.model.spi.Entity;
  37. import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
  38. import it.tidalwave.bluemarine2.model.spi.PathAwareFinder;

  39. /***********************************************************************************************************************
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **********************************************************************************************************************/
  44. public class PathAwareMediaFolderDecorator extends PathAwareEntityDecorator implements MediaFolder
  45.   {
  46.     /*******************************************************************************************************************
  47.      *
  48.      * Default constructor.
  49.      *
  50.      * @param   delegate            the delegate
  51.      * @param   parent              the parent
  52.      * @param   pathSegment         the path segment of this object
  53.      * @param   additionalRoles     some additional roles
  54.      *
  55.      ******************************************************************************************************************/
  56.     public PathAwareMediaFolderDecorator (@Nonnull final Entity delegate,
  57.                                           @Nonnull final PathAwareEntity parent,
  58.                                           @Nonnull final Path pathSegment,
  59.                                           @Nonnull final Collection<Object> additionalRoles)
  60.       {
  61.         super(delegate, parent, pathSegment, additionalRoles);
  62.       }

  63.     public PathAwareMediaFolderDecorator (@Nonnull final Entity delegate,
  64.                                           @Nonnull final PathAwareEntity parent,
  65.                                           @Nonnull final Path pathSegment)
  66.       {
  67.         this(delegate, parent, pathSegment, Collections.emptyList());
  68.       }

  69.     /*******************************************************************************************************************
  70.      *
  71.      * {@inheritDoc}
  72.      *
  73.      ******************************************************************************************************************/
  74.     @Override @Nonnull
  75.     public PathAwareFinder findChildren()
  76.       {
  77.         final SimpleComposite<Entity> composite = delegate.as(SimpleComposite.class);
  78.         return wrappedFinder(this, composite.findChildren());
  79.       }

  80.     /*******************************************************************************************************************
  81.      *
  82.      * {@inheritDoc}
  83.      *
  84.      ******************************************************************************************************************/
  85.     @Override @Nonnull
  86.     public String toDumpString()
  87.       {
  88.         return String.format("Folder(path=%s, parent=Folder(path=%s))", getPath(), parent.getPath());
  89.       }

  90.     /*******************************************************************************************************************
  91.      *
  92.      * Creates a wrapped finder, that wraps all entities in its result.
  93.      *
  94.      * @param   parent          the parent
  95.      * @param   finder          the source finder
  96.      * @return                  the wrapped finder
  97.      *
  98.      ******************************************************************************************************************/
  99.     @Nonnull
  100.     private static PathAwareFinder wrappedFinder (@Nonnull final MediaFolder parent,
  101.                                                   @Nonnull final Finder<? extends Entity> finder)
  102.       {
  103.         if (finder instanceof PathAwareEntityFinderDelegate)
  104.           {
  105.             return (PathAwareFinder)finder;
  106.           }

  107.         return new PathAwareEntityFinderDelegate(parent,
  108.                              new MappingFinder<>((Finder)finder, child -> wrappedEntity(parent, (Entity)child)));
  109.       }
  110.   }