1 package nom.tam.fits.compression.provider.param.base;
2
3 import nom.tam.fits.Header;
4 import nom.tam.fits.HeaderCard;
5 import nom.tam.fits.HeaderCardException;
6 import nom.tam.fits.compression.provider.param.api.ICompressHeaderParameter;
7 import nom.tam.fits.compression.provider.param.api.IHeaderAccess;
8
9 /*
10 * #%L
11 * nom.tam FITS library
12 * %%
13 * Copyright (C) 1996 - 2024 nom-tam-fits
14 * %%
15 * This is free and unencumbered software released into the public domain.
16 *
17 * Anyone is free to copy, modify, publish, use, compile, sell, or
18 * distribute this software, either in source code form or as a compiled
19 * binary, for any purpose, commercial or non-commercial, and by any
20 * means.
21 *
22 * In jurisdictions that recognize copyright laws, the author or authors
23 * of this software dedicate any and all copyright interest in the
24 * software to the public domain. We make this dedication for the benefit
25 * of the public at large and to the detriment of our heirs and
26 * successors. We intend this dedication to be an overt act of
27 * relinquishment in perpetuity of all present and future rights to this
28 * software under copyright law.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
34 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
35 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
36 * OTHER DEALINGS IN THE SOFTWARE.
37 * #L%
38 */
39
40 import static nom.tam.fits.header.Compression.ZNAMEn;
41 import static nom.tam.fits.header.Compression.ZVALn;
42
43 /**
44 * (<i>for internal use</i>) Visibility may be reduced to protected.
45 *
46 * @param <OPTION> The generic type of the compression option for which this parameter is used.
47 */
48 @SuppressWarnings("javadoc")
49 public abstract class CompressHeaderParameter<OPTION> extends CompressParameter<OPTION>
50 implements ICompressHeaderParameter {
51
52 protected CompressHeaderParameter(String name, OPTION option) {
53 super(name, option);
54 }
55
56 /**
57 * @deprecated Use {@link #findZVal(Header)} instead.
58 */
59 public HeaderCard findZVal(IHeaderAccess header) {
60 return findZVal(header.getHeader());
61 }
62
63 /**
64 * @deprecated Use {@link #nextFreeZVal(Header)} instead.
65 */
66 public int nextFreeZVal(IHeaderAccess header) {
67 return nextFreeZVal(header.getHeader());
68 }
69
70 /**
71 * Finds the ZVAL header value corresponding to this compression parameter
72 *
73 * @param header The compressed HDU header
74 *
75 * @return the header card containing the ZVAL for this compression parameter
76 *
77 * @throws HeaderCardException if there was an issue accessing the header
78 */
79 public HeaderCard findZVal(Header header) throws HeaderCardException {
80 int nval = 1;
81 HeaderCard card = header.getCard(ZNAMEn.n(nval));
82 while (card != null) {
83 if (card.getValue().equals(getName())) {
84 return header.getCard(ZVALn.n(nval));
85 }
86 card = header.getCard(ZNAMEn.n(++nval));
87 }
88 return null;
89 }
90
91 /**
92 * <p>
93 * Finds the next available (or previously used) the ZNAME / ZVAL index in the header that we can use to store this
94 * parameter.
95 * </p>
96 * <p>
97 * Unfortunately, the way it was implemented, using this repeatedly on the same header and compression parameter
98 * keeps adding new entries, rather than updating the existing one. As of 1.19, the behavior is changed to update
99 * existing values -- resulting in a more predictable behavior.
100 * </p>
101 *
102 * @param header The compressed HDU header
103 *
104 * @return the ZNAME / ZVAL index we might use to store a new parameter
105 *
106 * @throws HeaderCardException if there was an issue accessing the header
107 */
108 public int nextFreeZVal(Header header) throws HeaderCardException {
109 for (int n = 1;; n++) {
110 HeaderCard card = header.getCard(ZNAMEn.n(n));
111 if (card == null) {
112 return n;
113 }
114 if (getName().equals(card.getValue())) {
115 return n;
116 }
117 }
118 }
119 }