File: HDF5CompoundMemberInformation.java

package info (click to toggle)
libsis-jhdf5-java 19.04.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,668 kB
  • sloc: java: 79,644; ansic: 18,986; sh: 309; makefile: 49; xml: 12
file content (289 lines) | stat: -rw-r--r-- 11,305 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ch.systemsx.cisd.hdf5;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.BitSet;

import ch.systemsx.cisd.base.mdarray.MDAbstractArray;
import ch.systemsx.cisd.base.mdarray.MDByteArray;
import ch.systemsx.cisd.base.mdarray.MDDoubleArray;
import ch.systemsx.cisd.base.mdarray.MDFloatArray;
import ch.systemsx.cisd.base.mdarray.MDIntArray;
import ch.systemsx.cisd.base.mdarray.MDLongArray;
import ch.systemsx.cisd.base.mdarray.MDShortArray;
import ch.systemsx.cisd.hdf5.HDF5DataTypeInformation.DataTypeInfoOptions;

/**
 * Contains information about one member of an HDF5 compound data type.
 * 
 * @author Bernd Rinn
 */
public final class HDF5CompoundMemberInformation implements
        Comparable<HDF5CompoundMemberInformation>
{
    private final String memberName;

    private final HDF5DataTypeInformation dataTypeInformation;

    private final int offsetOnDisk;

    private final int offsetInMemory;

    private final String[] enumValuesOrNull;

    HDF5CompoundMemberInformation(String memberName, HDF5DataTypeInformation dataTypeInformation,
            int offsetOnDisk, int offsetInMemory, String[] enumValuesOrNull)
    {
        assert memberName != null;
        assert dataTypeInformation != null;
        assert offsetOnDisk >= 0;

        this.memberName = memberName;
        this.dataTypeInformation = dataTypeInformation;
        this.enumValuesOrNull = enumValuesOrNull;
        this.offsetOnDisk = offsetOnDisk;
        this.offsetInMemory = PaddingUtils.padOffset(offsetInMemory, dataTypeInformation.getElementSizeForPadding());

    }

    HDF5CompoundMemberInformation(String memberName, HDF5DataTypeInformation dataTypeInformation,
            int offsetOnDisk, int offsetInMemory)
    {
        this(memberName, dataTypeInformation, offsetOnDisk, offsetInMemory, null);
    }

    /**
     * Returns the name of the member.
     */
    public String getName()
    {
        return memberName;
    }

    /**
     * Returns the type information of the member.
     */
    public HDF5DataTypeInformation getType()
    {
        return dataTypeInformation;
    }

    /**
     * Returns the values of the enumeration type of this compound member, if it is of an
     * enumeration type and <code>null</code> otherwise.
     */
    public String[] tryGetEnumValues()
    {
        return enumValuesOrNull;
    }

    /**
     * Returns the byte offset of this member within the compound data type on disk. 0 meaning that
     * the member is the first one in the compound data type.
     * <p>
     * The on-disk representation is packed.
     */
    public int getOffsetOnDisk()
    {
        return offsetOnDisk;
    }

    /**
     * Returns the byte offset of this member within the compound data type in memory. 0 meaning
     * that the member is the first one in the compound data type.
     * <p>
     * The in-memory representation may contain padding to ensure that read access is always
     * aligned.
     */
    public int getOffsetInMemory()
    {
        return offsetInMemory;
    }

    /**
     * Creates the compound member information for the given <var>compoundClass</var> and
     * <var>members</var>. The returned array will contain the members in the order of the
     * <var>members</var>.
     * <p>
     * Call <code>Arrays.sort(compoundInformation)</code> to sort the array in alphabetical order of
     * names.
     * <p>
     * Can be used to compare compound types, e.g. via
     * {@link java.util.Arrays#equals(Object[], Object[])}.
     */
    public static HDF5CompoundMemberInformation[] create(Class<?> compoundClass,
            String houseKeepingNameSuffix, final HDF5CompoundMemberMapping... members)
    {
        assert compoundClass != null;
        final HDF5CompoundMemberInformation[] info =
                new HDF5CompoundMemberInformation[members.length];
        int offsetOnDisk = 0;
        int offsetInMemory = 0;
        for (int i = 0; i < info.length; ++i)
        {
            info[i] =
                    new HDF5CompoundMemberInformation(members[i].getMemberName(),
                            getTypeInformation(compoundClass, houseKeepingNameSuffix, members[i]),
                            offsetOnDisk, offsetInMemory);
            final int elementSize = info[i].getType().getElementSize();
            final int size = info[i].getType().getSize();
            offsetOnDisk += size;
            offsetInMemory = PaddingUtils.padOffset(offsetInMemory + size, elementSize);
        }
        Arrays.sort(info);
        return info;
    }

    private static HDF5DataTypeInformation getTypeInformation(Class<?> compoundClass,
            String houseKeepingNameSuffix, final HDF5CompoundMemberMapping member)
    {
        final Field fieldOrNull = member.tryGetField(compoundClass);
        final Class<?> fieldTypeOrNull = (fieldOrNull == null) ? null : fieldOrNull.getType();
        final HDF5DataTypeInformation typeInfo;
        if (fieldTypeOrNull == boolean.class)
        {
            typeInfo =
                    new HDF5DataTypeInformation(HDF5DataClass.BOOLEAN, houseKeepingNameSuffix, 1,
                            false);
        } else if (fieldTypeOrNull == byte.class || fieldTypeOrNull == byte[].class
                || fieldTypeOrNull == byte[][].class || fieldTypeOrNull == MDByteArray.class)
        {
            typeInfo =
                    new HDF5DataTypeInformation(HDF5DataClass.INTEGER, houseKeepingNameSuffix, 1,
                            false == member.isUnsigned());
        } else if (fieldTypeOrNull == short.class || fieldTypeOrNull == short[].class
                || fieldTypeOrNull == short[][].class || fieldTypeOrNull == MDShortArray.class)
        {
            typeInfo =
                    new HDF5DataTypeInformation(HDF5DataClass.INTEGER, houseKeepingNameSuffix, 2,
                            false == member.isUnsigned());
        } else if (fieldTypeOrNull == int.class || fieldTypeOrNull == int[].class
                || fieldTypeOrNull == int[][].class || fieldTypeOrNull == MDIntArray.class)
        {
            typeInfo =
                    new HDF5DataTypeInformation(HDF5DataClass.INTEGER, houseKeepingNameSuffix, 4,
                            false == member.isUnsigned());
        } else if (fieldTypeOrNull == long.class || fieldTypeOrNull == long[].class
                || fieldTypeOrNull == long[][].class || fieldTypeOrNull == MDLongArray.class)
        {
            typeInfo =
                    new HDF5DataTypeInformation(HDF5DataClass.INTEGER, houseKeepingNameSuffix, 8,
                            false == member.isUnsigned());
        } else if (fieldTypeOrNull == BitSet.class)
        {
            typeInfo =
                    new HDF5DataTypeInformation(HDF5DataClass.BITFIELD, houseKeepingNameSuffix, 8,
                            member.getMemberTypeLength() / 64
                                    + (member.getMemberTypeLength() % 64 != 0 ? 1 : 0), false);
        } else if (fieldTypeOrNull == float.class || fieldTypeOrNull == float[].class
                || fieldTypeOrNull == float[][].class || fieldTypeOrNull == MDFloatArray.class)
        {
            typeInfo =
                    new HDF5DataTypeInformation(HDF5DataClass.FLOAT, houseKeepingNameSuffix, 4,
                            true);
        } else if (fieldTypeOrNull == double.class || fieldTypeOrNull == double[].class
                || fieldTypeOrNull == double[][].class || fieldTypeOrNull == MDDoubleArray.class)
        {
            typeInfo =
                    new HDF5DataTypeInformation(HDF5DataClass.FLOAT, houseKeepingNameSuffix, 8,
                            true);
        } else if (fieldTypeOrNull == String.class || fieldTypeOrNull == char[].class)
        {
            if (member.isReference())
            {
                typeInfo =
                        new HDF5DataTypeInformation(HDF5DataClass.REFERENCE, houseKeepingNameSuffix,
                                HDF5BaseReader.REFERENCE_SIZE_IN_BYTES, false);
            } else
            {
                typeInfo =
                        new HDF5DataTypeInformation(HDF5DataClass.STRING, houseKeepingNameSuffix,
                                member.getMemberTypeLength(), false);
            }
        } else if (fieldTypeOrNull == HDF5EnumerationValue.class)
        {
            final DataTypeInfoOptions options =
                    new DataTypeInfoOptions("UNKNOWN".equals(member.tryGetEnumerationType()
                            .getName()) == false, member.tryGetTypeVariant() != null);
            typeInfo =
                    new HDF5DataTypeInformation(
                            options.knowsDataTypePath() ? HDF5Utils.createDataTypePath(
                                    HDF5Utils.ENUM_PREFIX, houseKeepingNameSuffix, member
                                            .tryGetEnumerationType().getName()) : null, options,
                            HDF5DataClass.ENUM, houseKeepingNameSuffix, member
                                    .tryGetEnumerationType().getStorageForm().getStorageSize(),
                            false);
            if (options.knowsDataTypeVariant())
            {
                typeInfo.setTypeVariant(member.tryGetTypeVariant());
            }
        } else
        {
            typeInfo =
                    new HDF5DataTypeInformation(HDF5DataClass.OTHER, houseKeepingNameSuffix, -1,
                            false);
        }
        if (fieldTypeOrNull != null
                && (fieldTypeOrNull.isArray() && fieldTypeOrNull != char[].class)
                || MDAbstractArray.class.isAssignableFrom(fieldTypeOrNull))
        {
            typeInfo.setDimensions(member.getMemberTypeDimensions());
        }
        return typeInfo;
    }

    //
    // Object
    //

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null || obj instanceof HDF5CompoundMemberInformation == false)
        {
            return false;
        }
        final HDF5CompoundMemberInformation that = (HDF5CompoundMemberInformation) obj;
        return memberName.equals(that.memberName)
                && dataTypeInformation.equals(that.dataTypeInformation);
    }

    @Override
    public int hashCode()
    {
        return (17 * 59 + memberName.hashCode()) * 59 + dataTypeInformation.hashCode();
    }

    @Override
    public String toString()
    {
        return memberName + ":" + dataTypeInformation.toString();
    }

    //
    // Comparable<HDF5CompoundMemberInformation>
    //

    @Override
    public int compareTo(HDF5CompoundMemberInformation o)
    {
        return memberName.compareTo(o.memberName);
    }

}