Issues (44)

fr/arakne/swfmaploader/MapLoaderConfigurator.java (4 issues)

1
/*
2
 * This file is part of Swf Map Loader.
3
 *
4
 * Swf Map Loader 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
 * Swf Map Loader 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 Swf Map Loader.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2020-2020 Vincent Quatrevieux
18
 */
19
20
package fr.arakne.swfmaploader;
21
22
import fr.arakne.swfmaploader.cache.MapStructureCache;
23
import fr.arakne.swfmaploader.cache.NullMapStructureCache;
24
import fr.arakne.swfmaploader.cache.SqlMapStructureCache;
25
import fr.arakne.swfmaploader.map.MapFactory;
26
import fr.arakne.swfmaploader.map.SimpleMapFactory;
27
import fr.arakne.swfmaploader.swf.SwfFileLoader;
28
import fr.arakne.utils.maps.DofusMap;
29
import fr.arakne.utils.maps.MapCell;
30
import fr.arakne.utils.maps.serializer.DefaultMapDataSerializer;
31
32
import java.sql.SQLException;
33
34
/**
35
 * Utility class for configure and create a {@link MapLoader}
36
 *
37
 * <code>
38
 *     MapLoaderConfigurator configurator = new MapLoaderConfigurator();
39
 *
40
 *     MapLoader loader = configurator
41
 *         .baseUrl("http://my-server.com/dofus/swf/maps")
42
 *         .tempDir("/tmp/my-client")
43
 *         .cacheFile("map-cache.sqlite")
44
 *         .factory(new MyCustomMapFactory())
45
 *         .create()
46
 *    ;
47
 *
48
 *    MyMap map = loader.load(gmd.id(), gmd.version(), gmd.key());
49
 * </code>
50
 *
51
 * @param <C> The cell type
52
 * @param <M> The map type
53
 */
54 1
final public class MapLoaderConfigurator<C extends MapCell, M extends DofusMap<C>> {
0 ignored issues
show
Add a constructor to the class.
Loading history...
55
    private MapStructureCache cache;
56
    private DefaultMapDataSerializer serializer;
57
    private MapFactory<C, M> factory;
58
    private SwfFileLoader loader;
59
60 1
    private String tempDir = "./tmp";
61 1
    private String baseUrl = "file:./data/maps";
62
63
    /**
64
     * Define the cache system to use
65
     */
66
    public MapLoaderConfigurator<C, M> cache(MapStructureCache cache) {
67 1
        this.cache = cache;
68 1
        return this;
69
    }
70
71
    /**
72
     * Define the serializer to use
73
     */
74
    public MapLoaderConfigurator<C, M> serializer(DefaultMapDataSerializer serializer) {
75 1
        this.serializer = serializer;
76 1
        return this;
77
    }
78
79
    /**
80
     * Enable the cell cache for the inner map data serializer
81
     */
82
    public MapLoaderConfigurator<C, M> enableCellCache() {
83 1
        DefaultMapDataSerializer serializer = new DefaultMapDataSerializer();
0 ignored issues
show
Comprehensibility introduced by
The variable serializershadows a field with the same name declared in line 56. Consider renaming it.
Loading history...
84 1
        serializer.enableCache();
85
86 1
        return serializer(serializer);
87
    }
88
89
    /**
90
     * Define the factory to use
91
     */
92
    public MapLoaderConfigurator<C, M> factory(MapFactory<C, M> factory) {
93 1
        this.factory = factory;
94 1
        return this;
95
    }
96
97
    /**
98
     * Define the SWF loader to use
99
     *
100
     * @see MapLoaderConfigurator#baseUrl(String) To configure loader with the given base url
101
     * @see MapLoaderConfigurator#tempDir(String) To configure loader with the given temporary directory
102
     */
103
    public MapLoaderConfigurator<C, M> loader(SwfFileLoader loader) {
104 1
        this.loader = loader;
105 1
        return this;
106
    }
107
108
    /**
109
     * Define the used temporary directory for load SWF files
110
     */
111
    public MapLoaderConfigurator<C, M> tempDir(String tempDir) {
112 1
        this.tempDir = tempDir;
113 1
        return this;
114
    }
115
116
    /**
117
     * Define the base URL of the maps CDN
118
     */
119
    public MapLoaderConfigurator<C, M> baseUrl(String baseUrl) {
120 1
        this.baseUrl = baseUrl;
121 1
        return this;
122
    }
123
124
    /**
125
     * Define the SQLite database file for the map cache
126
     */
127
    public MapLoaderConfigurator<C, M> cacheFile(String filename) throws SQLException {
128 1
        return cache(SqlMapStructureCache.createBySqliteFile(filename));
129
    }
130
131
    /**
132
     * Create the {@link MapLoader} instance
133
     */
134
    @SuppressWarnings("unchecked")
0 ignored issues
show
Suppressing code checker warnings is not a best practice. Consider working on your code until it meets guidelines.
Loading history...
135
    public MapLoader<C, M> create() {
136 1
        return new MapLoader<>(
0 ignored issues
show
Comprehensibility introduced by
Reduce the number of conditional operators (4) used in the expression (maximum allowed 3).
Loading history...
137
            loader == null ? new SwfFileLoader(baseUrl, tempDir) : loader,
138
            serializer == null ? new DefaultMapDataSerializer() : serializer,
139
            cache == null ? new NullMapStructureCache() : cache,
140
            factory == null ? (MapFactory<C, M>) new SimpleMapFactory() : factory
141
        );
142
    }
143
}
144