Skip to contentPackage: VirtualMediaFolder$EntityCollectionFactory
VirtualMediaFolder$EntityCollectionFactory
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.Nonnull;
30: import java.util.Collection;
31: import java.util.Optional;
32: import java.util.function.Function;
33: import java.nio.file.Path;
34: import it.tidalwave.util.Id;
35: import it.tidalwave.role.Identifiable;
36: import it.tidalwave.role.ui.Displayable;
37: import it.tidalwave.bluemarine2.model.spi.EntityWithRoles;
38: import it.tidalwave.bluemarine2.model.spi.PathAwareEntity;
39: import it.tidalwave.bluemarine2.model.spi.PathAwareFinder;
40: import lombok.Getter;
41: import lombok.ToString;
42: import static it.tidalwave.util.Parameters.r;
43:
44: /***********************************************************************************************************************
45: *
46: * Represents a folder which doesn't have a physical counterpart in the repository. It can be used to created in-memory
47: * aggregations of media items.
48: *
49: * @stereotype Datum
50: *
51: * @author Fabrizio Giudici
52: *
53: **********************************************************************************************************************/
54: @ToString(exclude = "finderFactory")
55: public class VirtualMediaFolder extends EntityWithRoles implements MediaFolder
56: {
57: // These two interfaces are needed to avoid clashes with constructor overloading
58: public static interface EntityCollectionFactory extends Function<MediaFolder, Collection<? extends PathAwareEntity>>
59: {
60: }
61:
62: public static interface EntityFinderFactory extends Function<MediaFolder, PathAwareFinder>
63: {
64: }
65:
66: @Getter @Nonnull
67: private final Path path;
68:
69: @Nonnull
70: private final Optional<? extends MediaFolder> optionalParent;
71:
72: @Nonnull
73: private final EntityFinderFactory finderFactory;
74:
75: public VirtualMediaFolder (@Nonnull final MediaFolder parent,
76: @Nonnull final Path pathSegment,
77: @Nonnull final String displayName,
78: @Nonnull final EntityCollectionFactory childrenFactory)
79: {
80: this(Optional.of(parent), pathSegment, displayName, Optional.of(childrenFactory), Optional.empty());
81: }
82:
83: public VirtualMediaFolder (@Nonnull final Optional<? extends MediaFolder> optionalParent,
84: @Nonnull final Path pathSegment,
85: @Nonnull final String displayName,
86: @Nonnull final EntityCollectionFactory childrenFactory)
87: {
88: this(optionalParent, pathSegment, displayName, Optional.of(childrenFactory), Optional.empty());
89: }
90:
91: public VirtualMediaFolder (@Nonnull final MediaFolder parent,
92: @Nonnull final Path pathSegment,
93: @Nonnull final String displayName,
94: @Nonnull final EntityFinderFactory finderFactory)
95: {
96: this(Optional.of(parent), pathSegment, displayName, Optional.empty(), Optional.of(finderFactory));
97: }
98:
99: public VirtualMediaFolder (@Nonnull final Optional<? extends MediaFolder> optionalParent,
100: @Nonnull final Path pathSegment,
101: @Nonnull final String displayName,
102: @Nonnull final EntityFinderFactory finderFactory)
103: {
104: this(optionalParent, pathSegment, displayName, Optional.empty(), Optional.of(finderFactory));
105: }
106:
107: private VirtualMediaFolder (@Nonnull final Optional<? extends MediaFolder> optionalParent,
108: @Nonnull final Path pathSegment,
109: @Nonnull final String displayName,
110: @Nonnull final Optional<EntityCollectionFactory> childrenSupplier,
111: @Nonnull final Optional<EntityFinderFactory> finderFactory)
112: {
113: // FIXME: review if first should be prioritised
114: super(r((Identifiable)() -> Id.of(absolutePath(optionalParent, pathSegment).toString()),
115: Displayable.of(displayName)));
116: this.path = absolutePath(optionalParent, pathSegment);
117: this.optionalParent = optionalParent;
118: this.finderFactory = finderFactory.orElse(mediaFolder -> mediaFolder.finderOf(childrenSupplier.get()));
119: }
120:
121: @Override @Nonnull
122: public Optional<PathAwareEntity> getParent()
123: {
124: return (Optional<PathAwareEntity>)(Object)optionalParent;
125: }
126:
127: @Override @Nonnull
128: public PathAwareFinder findChildren()
129: {
130: return finderFactory.apply(this);
131: }
132:
133: @Override @Nonnull
134: public String toDumpString()
135: {
136: return String.format("Folder(path=%s)", path);
137: }
138:
139: @Nonnull
140: private static Path absolutePath (@Nonnull final Optional<? extends MediaFolder> optionalParent,
141: @Nonnull final Path pathSegment)
142: {
143: return optionalParent.map(parent -> parent.getPath().resolve(pathSegment)).orElse(pathSegment);
144: }
145: }