source: josm/trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFileWrapper.java@ 13601

Last change on this file since 13601 was 12788, checked in by bastiK, 7 years ago

see #15229 - PMD warnings

  • Property svn:eol-style set to native
File size: 2.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.projection.datum;
3
4import java.io.IOException;
5import java.io.InputStream;
6import java.util.Collections;
7import java.util.Map;
8import java.util.TreeMap;
9
10/**
11 * Wrapper for {@link NTV2GridShiftFile}.
12 *
13 * Loads the shift file from disk, when it is first accessed.
14 * @since 5226
15 */
16public class NTV2GridShiftFileWrapper {
17
18 private NTV2GridShiftFile instance;
19 private final String gridFileName;
20
21 public static final float NTV2_SOURCE_PRIORITY_LOCAL = 10f;
22 public static final float NTV2_SOURCE_PRIORITY_DOWNLOAD = 5f;
23
24 private static Map<Float, NTV2GridShiftFileSource> sources = new TreeMap<>(Collections.reverseOrder());
25
26 /**
27 * Register a source for NTV2 grid files.
28 * @param priority the priority, sources with higher priority are checked first;
29 * use {@link #NTV2_SOURCE_PRIORITY_LOCAL} for local files and
30 * {@link #NTV2_SOURCE_PRIORITY_DOWNLOAD} for remote downloads
31 * @param source the NTV2 grid file source
32 * @since 12777
33 */
34 public static void registerNTV2GridShiftFileSource(float priority, NTV2GridShiftFileSource source) {
35 sources.put(priority, source);
36 }
37
38 /**
39 * Constructs a new {@code NTV2GridShiftFileWrapper}.
40 * @param filename Path to the grid file (GSB format)
41 */
42 public NTV2GridShiftFileWrapper(String filename) {
43 this.gridFileName = filename;
44 }
45
46 /**
47 * Returns the actual {@link NTV2GridShiftFile} behind this wrapper.
48 * The grid file is only loaded once, when first accessed.
49 * @return The NTv2 grid file
50 * @throws IOException if the grid file cannot be found/loaded
51 */
52 public synchronized NTV2GridShiftFile getShiftFile() throws IOException {
53 if (instance == null) {
54 for (Map.Entry<Float, NTV2GridShiftFileSource> entry : sources.entrySet()) {
55 NTV2GridShiftFileSource source = entry.getValue();
56 try (InputStream is = source.getNTV2GridShiftFile(gridFileName)) {
57 if (is != null) {
58 NTV2GridShiftFile ntv2 = new NTV2GridShiftFile();
59 ntv2.loadGridShiftFile(is, false);
60 instance = ntv2;
61 break;
62 }
63 }
64 }
65 }
66 return instance;
67 }
68}
Note: See TracBrowser for help on using the repository browser.