Skip to content

Package: DefaultRepositoryEntityFactory

DefaultRepositoryEntityFactory

nameinstructionbranchcomplexitylinemethod
DefaultRepositoryEntityFactory()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createEntity(Repository, Class, BindingSet)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
initialize()
M: 0 C: 53
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
lambda$notFound$0(Class, Repository, BindingSet)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
notFound(Class)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

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.catalog.factory;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.PostConstruct;
31: import java.util.HashMap;
32: import java.util.Map;
33: import java.net.URL;
34: import org.eclipse.rdf4j.query.BindingSet;
35: import org.eclipse.rdf4j.repository.Repository;
36: import it.tidalwave.bluemarine2.model.audio.AudioFile;
37: import it.tidalwave.bluemarine2.model.audio.MusicArtist;
38: import it.tidalwave.bluemarine2.model.audio.MusicPerformer;
39: import it.tidalwave.bluemarine2.model.audio.Performance;
40: import it.tidalwave.bluemarine2.model.audio.Record;
41: import it.tidalwave.bluemarine2.model.audio.Track;
42: import it.tidalwave.bluemarine2.model.impl.catalog.RepositoryAudioFile;
43: import it.tidalwave.bluemarine2.model.impl.catalog.RepositoryMusicArtist;
44: import it.tidalwave.bluemarine2.model.impl.catalog.RepositoryMusicPerformer;
45: import it.tidalwave.bluemarine2.model.impl.catalog.RepositoryPerformance;
46: import it.tidalwave.bluemarine2.model.impl.catalog.RepositoryRecord;
47: import it.tidalwave.bluemarine2.model.impl.catalog.RepositoryTrack;
48:
49: /***********************************************************************************************************************
50: *
51: * @author Fabrizio Giudici
52: *
53: **********************************************************************************************************************/
54: public class DefaultRepositoryEntityFactory implements RepositoryEntityFactory
55: {
56: private final Map<Class<?>, EntityFactoryFunction<?>> factoryMapByType = new HashMap<>();
57:
58: /*******************************************************************************************************************
59: *
60: * {@inheritDoc}
61: *
62: ******************************************************************************************************************/
63: @Override @Nonnull
64: public <E> E createEntity (@Nonnull final Repository repository,
65: @Nonnull final Class<E> entityClass,
66: @Nonnull final BindingSet bindingSet)
67: {
68: return entityClass.cast(factoryMapByType.getOrDefault(entityClass, notFound(entityClass))
69: .apply(repository, bindingSet));
70: }
71:
72: /*******************************************************************************************************************
73: *
74: ******************************************************************************************************************/
75: @Nonnull
76: private <E> EntityFactoryFunction<E> notFound (@Nonnull final Class<E> entityClass)
77: {
78: return (repository, bindingSet) ->
79: {
80: throw new RuntimeException("Unknown type: " + entityClass + "; registered: " + factoryMapByType.keySet());
81: };
82: }
83:
84: /*******************************************************************************************************************
85: *
86: ******************************************************************************************************************/
87: @PostConstruct
88: private void initialize()
89: {
90: // FIXME: use discovery. Either plain class discovery, or Spring bean discovery
91: factoryMapByType.put(String.class, new StringFactory());
92: factoryMapByType.put(URL.class, new UrlFactory());
93: factoryMapByType.put(Record.class, RepositoryRecord::new);
94: factoryMapByType.put(MusicArtist.class, RepositoryMusicArtist::new);
95: factoryMapByType.put(MusicPerformer.class, RepositoryMusicPerformer::new);
96: factoryMapByType.put(Track.class, RepositoryTrack::new);
97: factoryMapByType.put(Performance.class, RepositoryPerformance::new);
98: factoryMapByType.put(AudioFile.class, RepositoryAudioFile::new);
99: }
100: }