PlayList.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;

  28. import javax.annotation.Nonnegative;
  29. import javax.annotation.Nonnull;
  30. import java.util.ArrayList;
  31. import java.util.Collection;
  32. import java.util.Collections;
  33. import java.util.List;
  34. import java.util.Optional;
  35. import javafx.beans.property.BooleanProperty;
  36. import javafx.beans.property.SimpleBooleanProperty;
  37. import lombok.Getter;
  38. import lombok.experimental.Accessors;

  39. /***********************************************************************************************************************
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **********************************************************************************************************************/
  44. public class PlayList<ENTITY>
  45.   {
  46.     @Getter @Nonnull
  47.     private Optional<ENTITY> currentItem;

  48.     @Nonnull
  49.     private final List<ENTITY> items;

  50.     @Getter @Nonnegative
  51.     private int index;

  52.     @Getter @Accessors(fluent = true)
  53.     private final BooleanProperty hasPreviousProperty = new SimpleBooleanProperty();

  54.     @Getter @Accessors(fluent = true)
  55.     private final BooleanProperty hasNextProperty = new SimpleBooleanProperty();

  56.     /*******************************************************************************************************************
  57.      *
  58.      ******************************************************************************************************************/
  59.     private PlayList()
  60.       {
  61.         currentItem = Optional.empty();
  62.         items = Collections.emptyList();
  63.       }

  64.     /*******************************************************************************************************************
  65.      *
  66.      * Creates a new instance out of a collection of items, with the given current item.
  67.      *
  68.      * @param   currentItem     the item designated to be current
  69.      * @param   items           all the items - if empty, a playlist with a single element will be created
  70.      *
  71.      ******************************************************************************************************************/
  72.     public PlayList (@Nonnull final ENTITY currentItem, @Nonnull final Collection<ENTITY> items)
  73.       {
  74.         this.items = new ArrayList<>(items.isEmpty() ? List.of(currentItem) : items);
  75.         this.currentItem = Optional.of(currentItem);
  76.         this.index = this.items.indexOf(currentItem);
  77.         update();
  78.       }

  79.     /*******************************************************************************************************************
  80.      *
  81.      * Returns an empty playlist.
  82.      *
  83.      * @return  an empty playlist
  84.      *
  85.      ******************************************************************************************************************/
  86.     @Nonnull
  87.     public static <T> PlayList<T> empty()
  88.       {
  89.         return new PlayList<>();
  90.       }

  91.     /*******************************************************************************************************************
  92.      *
  93.      * Returns {@code true} if there is a previous item.
  94.      *
  95.      * @return  {@code true} if there is a previous item
  96.      *
  97.      ******************************************************************************************************************/
  98.     public boolean hasPrevious()
  99.       {
  100.         return hasPreviousProperty.get();
  101.       }

  102.     /*******************************************************************************************************************
  103.      *
  104.      * Returns {@code true} if there is a next item.
  105.      *
  106.      * @return  {@code true} if there is a next item
  107.      *
  108.      ******************************************************************************************************************/
  109.     public boolean hasNext()
  110.       {
  111.         return hasNextProperty.get();
  112.       }

  113.     /*******************************************************************************************************************
  114.      *
  115.      * Moves back to the previous item, if present, and returns it.
  116.      *
  117.      * @return  the previous item
  118.      *
  119.      ******************************************************************************************************************/
  120.     @Nonnull
  121.     public Optional<ENTITY> previous()
  122.       {
  123.         currentItem = Optional.of(items.get(--index));
  124.         update();
  125.         return currentItem;
  126.       }

  127.     /*******************************************************************************************************************
  128.      *
  129.      * Moves forward to the next item, if present, and returns it.
  130.      *
  131.      * @return  the next item
  132.      *
  133.      ******************************************************************************************************************/
  134.     @Nonnull
  135.     public Optional<ENTITY> next()
  136.       {
  137.         currentItem = Optional.of(items.get(++index));
  138.         update();
  139.         return currentItem;
  140.       }

  141.     /*******************************************************************************************************************
  142.      *
  143.      * Returns the next item, if present, without making it the current one.
  144.      *
  145.      * @return  the next item
  146.      *
  147.      ******************************************************************************************************************/
  148.     @Nonnull
  149.     public Optional<ENTITY> peekNext()
  150.       {
  151.         return hasNext() ? Optional.of(items.get(index + 1)) : Optional.empty();
  152.       }

  153.     /*******************************************************************************************************************
  154.      *
  155.      * Return the number of items in this play list.
  156.      *
  157.      * @return  the number of items
  158.      *
  159.      ******************************************************************************************************************/
  160.     @Nonnegative
  161.     public int getSize()
  162.       {
  163.         return items.size();
  164.       }

  165.     /*******************************************************************************************************************
  166.      *
  167.      ******************************************************************************************************************/
  168.     private void update()
  169.       {
  170.         hasPreviousProperty.set(index > 0);
  171.         hasNextProperty.set(index < items.size() - 1);
  172.       }
  173.   }