Package: PathAwareEntityDecorator
PathAwareEntityDecorator
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PathAwareEntityDecorator(Entity, PathAwareEntity, Path) |
|
|
|
|
|
||||||||||||||||||||
PathAwareEntityDecorator(Entity, PathAwareEntity, Path, Collection) |
|
|
|
|
|
||||||||||||||||||||
getParent() |
|
|
|
|
|
||||||||||||||||||||
getPath() |
|
|
|
|
|
||||||||||||||||||||
getPathSegment() |
|
|
|
|
|
||||||||||||||||||||
idToPathSegment(Entity) |
|
|
|
|
|
||||||||||||||||||||
toDumpString() |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
||||||||||||||||||||
wrappedEntity(PathAwareEntity, Entity) |
|
|
|
|
|
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.impl;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Collection;
31: import java.util.Collections;
32: import java.util.Optional;
33: import java.nio.file.Path;
34: import java.nio.file.Paths;
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 lombok.Getter;
39: import static it.tidalwave.role.Identifiable._Identifiable_;
40: import static it.tidalwave.role.SimpleComposite._SimpleComposite_;
41:
42: /***********************************************************************************************************************
43: *
44: * An adapter for {@link Entity} to a {@link MediaFolder}. It can be used to adapt entities that naturally do
45: * not belong to a hierarchy, such as an artist, to contexts where a hierarchy is needed (e.g. for browsing).
46: *
47: * @author Fabrizio Giudici
48: *
49: **********************************************************************************************************************/
50: public class PathAwareEntityDecorator extends EntityDecorator implements PathAwareEntity
51: {
52: @Nonnull
53: protected final PathAwareEntity parent;
54:
55: @Getter @Nonnull
56: protected final Path pathSegment;
57:
58: /*******************************************************************************************************************
59: *
60: ******************************************************************************************************************/
61: protected PathAwareEntityDecorator (@Nonnull final Entity delegate,
62: @Nonnull final PathAwareEntity parent,
63: @Nonnull final Path pathSegment,
64: @Nonnull final Collection<Object> roles)
65: {
66: super(delegate, roles);
67: this.pathSegment = pathSegment;
68: this.parent = parent;
69: }
70:
71: protected PathAwareEntityDecorator (@Nonnull final Entity delegate,
72: @Nonnull final PathAwareEntity parent,
73: @Nonnull final Path pathSegment)
74: {
75: this(delegate, parent, pathSegment, Collections.emptyList());
76: }
77:
78: /*******************************************************************************************************************
79: *
80: * {@inheritDoc}
81: *
82: ******************************************************************************************************************/
83: @Override @Nonnull
84: public Optional<PathAwareEntity> getParent()
85: {
86: return Optional.of(parent);
87: }
88:
89: /*******************************************************************************************************************
90: *
91: * {@inheritDoc}
92: *
93: ******************************************************************************************************************/
94: @Override @Nonnull
95: public Path getPath()
96: {
97: return parent.getPath().resolve(pathSegment);
98: }
99:
100: /*******************************************************************************************************************
101: *
102: * {@inheritDoc}
103: *
104: ******************************************************************************************************************/
105: @Override @Nonnull
106: public String toString()
107: {
108: return String.format("%s(path=%s, delegate=%s, parent=%s)", getClass().getSimpleName(), getPath(), delegate, parent);
109: }
110:
111: /*******************************************************************************************************************
112: *
113: * {@inheritDoc}
114: *
115: ******************************************************************************************************************/
116: @Override @Nonnull
117: public String toDumpString()
118: {
119: return String.format("Entity(path=%s, delegate=%s, parent=Folder(path=%s))", getPath(), delegate, parent.getPath());
120: }
121:
122: /*******************************************************************************************************************
123: *
124: * Creates a wrapped entity, with the given parent.
125: *
126: * @param parent the parent
127: * @param entity the source entity
128: * @return the wrapped entity
129: *
130: ******************************************************************************************************************/
131: @Nonnull
132: protected static PathAwareEntity wrappedEntity (@Nonnull final PathAwareEntity parent, @Nonnull final Entity entity)
133: {
134:• if (entity instanceof PathAwareEntity) // FIXME: possibly avoid calling
135: {
136: return (PathAwareEntity)entity;
137: }
138:
139: final Path pathSegment = idToPathSegment(entity);
140:• return entity.maybeAs(_SimpleComposite_).isPresent()
141: ? new PathAwareMediaFolderDecorator(entity, parent, pathSegment)
142: : new PathAwareEntityDecorator(entity, parent, pathSegment);
143: }
144:
145: /*******************************************************************************************************************
146: *
147: ******************************************************************************************************************/
148: @Nonnull
149: private static Path idToPathSegment (@Nonnull final Entity entity)
150: {
151: return Paths.get(entity.as(_Identifiable_).getId().stringValue().replace('/', '_'));
152: }
153: }