View Javadoc
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      @Deprecated
60      public HeaderCard findZVal(IHeaderAccess header) {
61          return findZVal(header.getHeader());
62      }
63  
64      /**
65       * @deprecated Use {@link #nextFreeZVal(Header)} instead.
66       */
67      @Deprecated
68      public int nextFreeZVal(IHeaderAccess header) {
69          return nextFreeZVal(header.getHeader());
70      }
71  
72      /**
73       * Finds the ZVAL header value corresponding to this compression parameter
74       * 
75       * @param  header              The compressed HDU header
76       * 
77       * @return                     the header card containing the ZVAL for this compression parameter
78       * 
79       * @throws HeaderCardException if there was an issue accessing the header
80       */
81      public HeaderCard findZVal(Header header) throws HeaderCardException {
82          int nval = 1;
83          HeaderCard card = header.getCard(ZNAMEn.n(nval));
84          while (card != null) {
85              if (card.getValue().equals(getName())) {
86                  return header.getCard(ZVALn.n(nval));
87              }
88              card = header.getCard(ZNAMEn.n(++nval));
89          }
90          return null;
91      }
92  
93      /**
94       * <p>
95       * Finds the next available (or previously used) the ZNAME / ZVAL index in the header that we can use to store this
96       * parameter.
97       * </p>
98       * <p>
99       * Unfortunately, the way it was implemented, using this repeatedly on the same header and compression parameter
100      * keeps adding new entries, rather than updating the existing one. As of 1.19, the behavior is changed to update
101      * existing values -- resulting in a more predictable behavior.
102      * </p>
103      * 
104      * @param  header              The compressed HDU header
105      * 
106      * @return                     the ZNAME / ZVAL index we might use to store a new parameter
107      * 
108      * @throws HeaderCardException if there was an issue accessing the header
109      */
110     public int nextFreeZVal(Header header) throws HeaderCardException {
111         for (int n = 1;; n++) {
112             HeaderCard card = header.getCard(ZNAMEn.n(n));
113             if (card == null) {
114                 return n;
115             }
116             if (getName().equals(card.getValue())) {
117                 return n;
118             }
119         }
120     }
121 }