| 1 |
package nom.tam.fits.compression.provider.param.base; |
| 2 |
|
| 3 |
/* |
| 4 |
* #%L |
| 5 |
* nom.tam FITS library |
| 6 |
* %% |
| 7 |
* Copyright (C) 1996 - 2024 nom-tam-fits |
| 8 |
* %% |
| 9 |
* This is free and unencumbered software released into the public domain. |
| 10 |
* |
| 11 |
* Anyone is free to copy, modify, publish, use, compile, sell, or |
| 12 |
* distribute this software, either in source code form or as a compiled |
| 13 |
* binary, for any purpose, commercial or non-commercial, and by any |
| 14 |
* means. |
| 15 |
* |
| 16 |
* In jurisdictions that recognize copyright laws, the author or authors |
| 17 |
* of this software dedicate any and all copyright interest in the |
| 18 |
* software to the public domain. We make this dedication for the benefit |
| 19 |
* of the public at large and to the detriment of our heirs and |
| 20 |
* successors. We intend this dedication to be an overt act of |
| 21 |
* relinquishment in perpetuity of all present and future rights to this |
| 22 |
* software under copyright law. |
| 23 |
* |
| 24 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 27 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 28 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 29 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 30 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 31 |
* #L% |
| 32 |
*/ |
| 33 |
|
| 34 |
import java.lang.reflect.Array; |
| 35 |
|
| 36 |
import nom.tam.fits.compression.provider.param.api.ICompressColumnParameter; |
| 37 |
|
| 38 |
/** |
| 39 |
* <p> |
| 40 |
* (<i>for internal use</i>) Compression parameters that are stored in the table along with the compressed data. Each |
| 41 |
* parameter is associated to a comlumn in the table, and the parameter takes the value that is stored in the same row |
| 42 |
* as the compressed data themselves. |
| 43 |
* </p> |
| 44 |
* <p> |
| 45 |
* It is possible to make independent copies of a set of such parameters, e.g. for parallel processing. In such cases |
| 46 |
* all copies share their underlying column data with the original, so changing the sotrage array of column data in |
| 47 |
* either the original or any of its decendants will affect the original and all decendans equally. |
| 48 |
* </p> |
| 49 |
* |
| 50 |
* @author Attila Kovacs |
| 51 |
* |
| 52 |
* @param <T> The generic array type that contains the individual parameters for each tile as a table column. |
| 53 |
* @param <OPTION> The generic type of compression option that is associated with these parameters |
| 54 |
*/ |
| 55 |
public abstract class CompressColumnParameter<T, OPTION> extends CompressParameter<OPTION> |
| 56 |
implements ICompressColumnParameter { |
| 57 |
|
| 58 |
private Data column; |
| 59 |
|
| 60 |
private final Class<T> type; |
| 61 |
|
| 62 |
/** |
| 63 |
* Creates a new compression parameter, which stores a per-tile value for a compression option in a table column. |
| 64 |
* |
| 65 |
* @param name the FITS parameter name, that is the column name which stores the values |
| 66 |
* @param option the compression option that uses the parameter value |
| 67 |
* @param type the Java class of the parameter, such as {@link java.lang.Integer} or {@link java.lang.String}. |
| 68 |
*/ |
| 69 |
protected CompressColumnParameter(String name, OPTION option, Class<T> type) { |
| 70 |
super(name, option); |
| 71 |
column = new Data(); |
| 72 |
this.type = type; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public synchronized T getColumnData() { |
| 77 |
return column.getValues(); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public synchronized void setColumnData(Object columnValue, int sizeValue) { |
| 82 |
column.create(columnValue, sizeValue); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* The shared column data across all copies and the original column parameter. |
| 87 |
* |
| 88 |
* @author Attila Kovacs |
| 89 |
* |
| 90 |
* @since 1.18 |
| 91 |
*/ |
| 92 |
private final class Data { |
| 93 |
private T values; |
| 94 |
|
| 95 |
private synchronized T getValues() { |
| 96 |
return values; |
| 97 |
} |
| 98 |
|
| 99 |
private synchronized void create(Object columnValue, int sizeValue) { |
| 100 |
if (sizeValue <= 0) { |
| 101 |
values = null; |
| 102 |
} else { |
| 103 |
if (columnValue == null) { |
| 104 |
columnValue = Array.newInstance(type.getComponentType(), sizeValue); |
| 105 |
} |
| 106 |
values = type.cast(columnValue); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |