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

Last change on this file since 14440 was 14440, checked in by Don-vip, 6 years ago

fix #17018 - NPE

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