View Javadoc
1   package nom.tam.fits.compression.provider.param.api;
2   
3   import nom.tam.fits.Header;
4   
5   /*
6    * #%L
7    * nom.tam FITS library
8    * %%
9    * Copyright (C) 1996 - 2024 nom-tam-fits
10   * %%
11   * This is free and unencumbered software released into the public domain.
12   *
13   * Anyone is free to copy, modify, publish, use, compile, sell, or
14   * distribute this software, either in source code form or as a compiled
15   * binary, for any purpose, commercial or non-commercial, and by any
16   * means.
17   *
18   * In jurisdictions that recognize copyright laws, the author or authors
19   * of this software dedicate any and all copyright interest in the
20   * software to the public domain. We make this dedication for the benefit
21   * of the public at large and to the detriment of our heirs and
22   * successors. We intend this dedication to be an overt act of
23   * relinquishment in perpetuity of all present and future rights to this
24   * software under copyright law.
25   *
26   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32   * OTHER DEALINGS IN THE SOFTWARE.
33   * #L%
34   */
35  
36  import nom.tam.fits.HeaderCard;
37  import nom.tam.fits.HeaderCardException;
38  import nom.tam.fits.header.IFitsHeader;
39  
40  /**
41   * <p>
42   * (<i>for internal use / no longer used</i>) Access to a specific FITS header card with runtime exceptions only.
43   * Regular modifications to {@link HeaderCard} may throw {@link HeaderCardException}s, which are hard exceptions. They
44   * really should have been softer runtime exceptions from the start, but unfortunately that was choice this library made
45   * a very long time ago, and we therefore stick to it, at least until the next major code revision (major version 2 at
46   * the earliest). So this class provides an alternative access to a header card converting any
47   * <code>HeaderCardException</code>s to {@link IllegalArgumentException}.
48   * </p>
49   * <p>
50   * Unlike {@link HeaderAccess} this class operates on single cards. Methods that specify a keywords are applied to the
51   * selected card if and only if the keyword matches that of the card's keyword.
52   * </p>
53   * 
54   * @see        Header
55   * 
56   * @deprecated This class serves no purpose since 1.19. Will remove in some future. Prior to 1.19 {@link Header} threw
57   *                 hard {@link HeaderCardException}, and this class was added so we can convert these into soft
58   *                 {@link IllegalArgumentException} instead. However, now that we demoted
59   *                 <code>HeaderCardException</code> to be soft exceptions itself, there is no reason to convert. It just
60   *                 adds confusion.
61   */
62  public class HeaderCardAccess implements IHeaderAccess {
63  
64      private final HeaderCard headerCard;
65  
66      /**
67       * <p>
68       * Creates a new access to modifying a {@link HeaderCard} without the hard exceptions that <code>HeaderCard</code>
69       * may throw.
70       * </p>
71       * <p>
72       * Unlike {@link HeaderAccess} this class operates on single cards. Methods that specify a keywords are applied to
73       * the selected card if and only if the keyword matches that of the card's keyword.
74       * </p>
75       * 
76       * @param  headerCard               the FITS keyword of the card we will provide access to
77       * @param  value                    the initial string value for the card (assuming the keyword allows string
78       *                                      values).
79       * 
80       * @throws IllegalArgumentException if the header card could not be created
81       */
82      public HeaderCardAccess(IFitsHeader headerCard, String value) throws IllegalArgumentException {
83          try {
84              this.headerCard = new HeaderCard(headerCard.key(), value, null);
85          } catch (HeaderCardException e) {
86              throw new IllegalArgumentException("header card could not be created");
87          }
88      }
89  
90      @Override
91      public final Header getHeader() {
92          Header header = new Header();
93          header.addLine(headerCard);
94          return header;
95      }
96  
97      /**
98       * Returns the header card that this class is providing access to.
99       * 
100      * @return the Header card that we access through this class
101      * 
102      * @since  1.19
103      */
104     public final HeaderCard getHeaderCard() {
105         return headerCard;
106     }
107 
108     @Override
109     public void addValue(IFitsHeader key, int value) {
110         if (headerCard.getKey().equals(key.key())) {
111             headerCard.setValue(value);
112         }
113     }
114 
115     @Override
116     public void addValue(IFitsHeader key, String value) {
117         if (headerCard.getKey().equals(key.key())) {
118             headerCard.setValue(value);
119         }
120     }
121 
122     @Override
123     public HeaderCard findCard(IFitsHeader key) {
124         return findCard(key.key());
125     }
126 
127     @Override
128     public HeaderCard findCard(String key) {
129         if (headerCard.getKey().equals(key)) {
130             return headerCard;
131         }
132         return null;
133     }
134 }