Skip to content

Package: NameMangler

NameMangler

nameinstructionbranchcomplexitylinemethod
mangle(String)
M: 11 C: 40
78%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 8
80%
M: 0 C: 1
100%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * MapView: a JavaFX map renderer for tile-based servers
5: * http://tidalwave.it/projects/mapview
6: *
7: * Copyright (C) 2024 - 2025 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 the License.
12: * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/mapview-src
22: * git clone https://github.com/tidalwave-it/mapview-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.mapview.impl;
27:
28: import jakarta.annotation.Nonnull;
29: import lombok.experimental.UtilityClass;
30:
31: /***************************************************************************************************************************************************************
32: *
33: * A mangler for storing files in the tile cache.
34: *
35: * @author Fabrizio Giudici
36: *
37: **************************************************************************************************************************************************************/
38: @UtilityClass
39: public class NameMangler
40: {
41: /***********************************************************************************************************************************************************
42: * Mangles the given URL returning the local path to be used on the filesystem. The returned path is the original parameter prefixed with two directories
43: * such as <code>ab/cd/</code> where abcd is the CRC16 of the original URL.
44: * @param url the url to mangle
45: * @return the mangled path
46: **********************************************************************************************************************************************************/
47: @Nonnull
48: public static String mangle (@Nonnull final String url)
49: {
50: var s = url.substring("http://".length());
51: int i = s.indexOf('/');
52:
53:• if (i > 0)
54: {
55: s = s.substring(i + 1);
56: }
57:
58: i = s.lastIndexOf('?');
59:
60:• if (i >= 0)
61: {
62: s = s.substring(0, i);
63: }
64:
65: var crc16 = "0000" + Integer.toHexString(CRC16.crc16(s));
66: crc16 = crc16.substring(crc16.length() - 4);
67: return crc16.substring(0, 2) + "/" + crc16.substring(2, 4) + "/" + s;
68: }
69: }