Package: PlayList
PlayList
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PlayList() |
|
|
|
|
|
||||||||||||||||||||
PlayList(Object, Collection) |
|
|
|
|
|
||||||||||||||||||||
empty() |
|
|
|
|
|
||||||||||||||||||||
getCurrentItem() |
|
|
|
|
|
||||||||||||||||||||
getIndex() |
|
|
|
|
|
||||||||||||||||||||
getSize() |
|
|
|
|
|
||||||||||||||||||||
hasNext() |
|
|
|
|
|
||||||||||||||||||||
hasNextProperty() |
|
|
|
|
|
||||||||||||||||||||
hasPrevious() |
|
|
|
|
|
||||||||||||||||||||
hasPreviousProperty() |
|
|
|
|
|
||||||||||||||||||||
next() |
|
|
|
|
|
||||||||||||||||||||
peekNext() |
|
|
|
|
|
||||||||||||||||||||
previous() |
|
|
|
|
|
||||||||||||||||||||
update() |
|
|
|
|
|
Coverage
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:
29: import javax.annotation.Nonnegative;
30: import javax.annotation.Nonnull;
31: import java.util.ArrayList;
32: import java.util.Collection;
33: import java.util.Collections;
34: import java.util.List;
35: import java.util.Optional;
36: import javafx.beans.property.BooleanProperty;
37: import javafx.beans.property.SimpleBooleanProperty;
38: import lombok.Getter;
39: import lombok.experimental.Accessors;
40:
41: /***********************************************************************************************************************
42: *
43: * @author Fabrizio Giudici
44: *
45: **********************************************************************************************************************/
46: public class PlayList<ENTITY>
47: {
48: @Getter @Nonnull
49: private Optional<ENTITY> currentItem;
50:
51: @Nonnull
52: private final List<ENTITY> items;
53:
54: @Getter @Nonnegative
55: private int index;
56:
57: @Getter @Accessors(fluent = true)
58: private final BooleanProperty hasPreviousProperty = new SimpleBooleanProperty();
59:
60: @Getter @Accessors(fluent = true)
61: private final BooleanProperty hasNextProperty = new SimpleBooleanProperty();
62:
63: /*******************************************************************************************************************
64: *
65: ******************************************************************************************************************/
66: private PlayList()
67: {
68: currentItem = Optional.empty();
69: items = Collections.emptyList();
70: }
71:
72: /*******************************************************************************************************************
73: *
74: * Creates a new instance out of a collection of items, with the given current item.
75: *
76: * @param currentItem the item designated to be current
77: * @param items all the items - if empty, a playlist with a single element will be created
78: *
79: ******************************************************************************************************************/
80: public PlayList (@Nonnull final ENTITY currentItem, @Nonnull final Collection<ENTITY> items)
81: {
82:• this.items = new ArrayList<>(items.isEmpty() ? List.of(currentItem) : items);
83: this.currentItem = Optional.of(currentItem);
84: this.index = this.items.indexOf(currentItem);
85: update();
86: }
87:
88: /*******************************************************************************************************************
89: *
90: * Returns an empty playlist.
91: *
92: * @return an empty playlist
93: *
94: ******************************************************************************************************************/
95: @Nonnull
96: public static <T> PlayList<T> empty()
97: {
98: return new PlayList<>();
99: }
100:
101: /*******************************************************************************************************************
102: *
103: * Returns {@code true} if there is a previous item.
104: *
105: * @return {@code true} if there is a previous item
106: *
107: ******************************************************************************************************************/
108: public boolean hasPrevious()
109: {
110: return hasPreviousProperty.get();
111: }
112:
113: /*******************************************************************************************************************
114: *
115: * Returns {@code true} if there is a next item.
116: *
117: * @return {@code true} if there is a next item
118: *
119: ******************************************************************************************************************/
120: public boolean hasNext()
121: {
122: return hasNextProperty.get();
123: }
124:
125: /*******************************************************************************************************************
126: *
127: * Moves back to the previous item, if present, and returns it.
128: *
129: * @return the previous item
130: *
131: ******************************************************************************************************************/
132: @Nonnull
133: public Optional<ENTITY> previous()
134: {
135: currentItem = Optional.of(items.get(--index));
136: update();
137: return currentItem;
138: }
139:
140: /*******************************************************************************************************************
141: *
142: * Moves forward to the next item, if present, and returns it.
143: *
144: * @return the next item
145: *
146: ******************************************************************************************************************/
147: @Nonnull
148: public Optional<ENTITY> next()
149: {
150: currentItem = Optional.of(items.get(++index));
151: update();
152: return currentItem;
153: }
154:
155: /*******************************************************************************************************************
156: *
157: * Returns the next item, if present, without making it the current one.
158: *
159: * @return the next item
160: *
161: ******************************************************************************************************************/
162: @Nonnull
163: public Optional<ENTITY> peekNext()
164: {
165:• return hasNext() ? Optional.of(items.get(index + 1)) : Optional.empty();
166: }
167:
168: /*******************************************************************************************************************
169: *
170: * Return the number of items in this play list.
171: *
172: * @return the number of items
173: *
174: ******************************************************************************************************************/
175: @Nonnegative
176: public int getSize()
177: {
178: return items.size();
179: }
180:
181: /*******************************************************************************************************************
182: *
183: ******************************************************************************************************************/
184: private void update()
185: {
186:• hasPreviousProperty.set(index > 0);
187:• hasNextProperty.set(index < items.size() - 1);
188: }
189: }