Skip to contentMethod: MetadataSupport(Path)
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.spi;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.Nullable;
31: import java.util.Collections;
32: import java.util.HashMap;
33: import java.util.Map;
34: import java.util.Optional;
35: import java.util.Set;
36: import java.util.function.Function;
37: import java.nio.file.Path;
38: import it.tidalwave.util.Key;
39: import it.tidalwave.bluemarine2.model.MediaItem;
40: import lombok.Getter;
41: import lombok.RequiredArgsConstructor;
42: import lombok.ToString;
43: import lombok.extern.slf4j.Slf4j;
44:
45: /***********************************************************************************************************************
46: *
47: * @author Fabrizio Giudici
48: *
49: **********************************************************************************************************************/
50: @RequiredArgsConstructor @ToString(of = "properties") @Slf4j
51: public class MetadataSupport implements MediaItem.Metadata
52: {
53: @Getter @Nonnull
54: protected final Path path;
55:
56: @Nonnull
57: protected Optional<Function<Key<?>, MediaItem.Metadata>> fallback;
58:
59: protected final Map<Key<?>, Object> properties = new HashMap<>();
60:
61: /*******************************************************************************************************************
62: *
63: *
64: *
65: ******************************************************************************************************************/
66: public MetadataSupport (@Nonnull final Path path)
67: {
68: this(path, Optional.empty());
69: }
70:
71: /*******************************************************************************************************************
72: *
73: * {@inheritDoc}
74: *
75: ******************************************************************************************************************/
76: @Override @Nonnull
77: public <T> Optional<T> get (@Nonnull final Key<T> key)
78: {
79: return properties.containsKey(key) ? Optional.ofNullable((T)properties.get(key))
80: : fallback.flatMap(fb -> fb.apply(key).get(key));
81: }
82:
83: /*******************************************************************************************************************
84: *
85: * {@inheritDoc}
86: *
87: ******************************************************************************************************************/
88: @Override @Nonnull
89: public <T> T getAll (@Nonnull final Key<T> key)
90: {
91: final T list = (T)properties.get(key);
92: return (list != null) ? list :
93: fallback.flatMap(fb -> fb.apply(key).get(key)).orElse((T)Collections.emptyList());
94: }
95:
96: /*******************************************************************************************************************
97: *
98: * {@inheritDoc}
99: *
100: ******************************************************************************************************************/
101: @Override
102: public boolean containsKey (@Nonnull final Key<?> key)
103: {
104: return properties.containsKey(key);
105: }
106:
107: /*******************************************************************************************************************
108: *
109: * {@inheritDoc}
110: *
111: ******************************************************************************************************************/
112: @Override @Nonnull
113: public Set<Key<?>> getKeys()
114: {
115: return Collections.unmodifiableSet(properties.keySet());
116: }
117:
118: /*******************************************************************************************************************
119: *
120: * {@inheritDoc}
121: *
122: ******************************************************************************************************************/
123: @Override @Nonnull
124: public Set<Map.Entry<Key<?>, ?>> getEntries()
125: {
126: return Collections.unmodifiableSet(properties.entrySet());
127: }
128:
129: /*******************************************************************************************************************
130: *
131: * {@inheritDoc}
132: *
133: ******************************************************************************************************************/
134: @Override @Nonnull
135: public <T> MediaItem.Metadata with (@Nonnull final Key<T> key, @Nonnull final T value)
136: {
137: final MetadataSupport clone = new MetadataSupport(path, fallback);
138: clone.properties.putAll(this.properties);
139: clone.put(key, value);
140:
141: if (value instanceof ITunesComment)
142: {
143: clone.put(CDDB, ((ITunesComment) value).getCddb());
144: }
145:
146: return clone;
147: }
148:
149: /*******************************************************************************************************************
150: *
151: * {@inheritDoc}
152: *
153: ******************************************************************************************************************/
154: @Override @Nonnull
155: public MediaItem.Metadata withFallback (@Nonnull final Function<Key<?>, MediaItem.Metadata> fallback)
156: {
157: final MetadataSupport clone = new MetadataSupport(path, Optional.of(fallback));
158: clone.properties.putAll(this.properties);
159: return clone;
160: }
161:
162: /*******************************************************************************************************************
163: *
164: * {@inheritDoc}
165: *
166: ******************************************************************************************************************/
167: @Override @Nonnull
168: public <T> MediaItem.Metadata with (@Nonnull final Key<T> key, @Nonnull final Optional<T> value)
169: {
170: return value.map(t -> with(key, t)).orElse(this);
171: }
172:
173: /*******************************************************************************************************************
174: *
175: * FIXME: remove this, make it truly immutable.
176: *
177: ******************************************************************************************************************/
178: protected <V> void put (@Nonnull final Key<V> key, @Nullable final V value)
179: {
180: if (value != null)
181: {
182: properties.put(key, value);
183: }
184: }
185: }