Passed
Pull Request — master (#176)
by Vincent
10:42
created

get(Class)   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2021 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.core.di;
21
22
import java.util.HashMap;
23
import java.util.Map;
24
25
/**
26
 * Container with manually defined services as scope
27
 *
28
 * Usage:
29
 * <pre>{@code
30
 * Container container = ...;
31
 * Container scoped = container.with(myService, MyServiceInterface.class);
32
 *
33
 * scoped.get(MyServiceInterface.class); // Will return myService
34
 * }</pre>
35
 */
36
public final class ScopedContainer implements Container {
37
    private final Container container;
38
    private final Map<Class, Object> scope;
39
40 1
    private ScopedContainer(Container container, Map<Class, Object> scope) {
41 1
        this.container = container;
42 1
        this.scope = scope;
43 1
    }
44
45
    @Override
46
    public <T> T get(Class<T> type) throws ContainerException {
47 1
        if (scope.containsKey(type)) {
48 1
            return (T) scope.get(type);
49
        }
50
51 1
        return container.get(type);
52
    }
53
54
    @Override
55
    public boolean has(Class type) {
56 1
        return scope.containsKey(type) || container.has(type);
57
    }
58
59
    @Override
60
    public void register(ContainerModule module) {
61 1
        throw new UnsupportedOperationException("Cannot register a module on a scoped container");
62
    }
63
64
    /**
65
     * Create the scoped container from the given mapping
66
     *
67
     * @param container Base container
68
     * @param mappings The mapping
69
     *
70
     * @return The created container instance
71
     */
72
    public static ScopedContainer fromMapping(Container container, Mapping[] mappings) {
73 1
        final Map<Class, Object> scope = new HashMap<>();
74
75 1
        for (Mapping mapping : mappings) {
76 1
            scope.put(mapping.value.getClass(), mapping.value);
77
78 1
            for (Class type : mapping.interfaces) {
79 1
                scope.put(type, mapping.value);
80
            }
81
        }
82
83 1
        return new ScopedContainer(container, scope);
84
    }
85
86
    /**
87
     * An interfaces / value mapping
88
     *
89
     * @param <T> The base type
90
     */
91
    public static final class Mapping<T> {
92
        private final T value;
93
        private final Class[] interfaces;
94
95 1
        public Mapping(T value, Class<? super T>... interfaces) {
96 1
            this.value = value;
97 1
            this.interfaces = interfaces;
98 1
        }
99
    }
100
}
101