1+ package org .hdf5javalib .examples .read ;
2+
3+ import org .hdf5javalib .dataclass .*;
4+ import org .hdf5javalib .datasource .TypedDataSource ;
5+ import org .hdf5javalib .datatype .CompoundDatatype ;
6+ import org .hdf5javalib .examples .ResourceLoader ;
7+ import org .hdf5javalib .hdfjava .HdfDataset ;
8+ import org .hdf5javalib .hdfjava .HdfFileReader ;
9+ import org .junit .jupiter .api .Test ;
10+
11+ import java .math .BigDecimal ;
12+ import java .math .BigInteger ;
13+ import java .nio .channels .SeekableByteChannel ;
14+ import java .util .BitSet ;
15+ import java .util .Map ;
16+ import java .util .stream .Collectors ;
17+
18+ import static org .junit .jupiter .api .Assertions .*;
19+
20+ public class SeparateTypesReadTest {
21+
22+ public static class Compound {
23+ private Short a ;
24+ private Double b ;
25+
26+ public Short getA () {
27+ return a ;
28+ }
29+
30+ public void setA (Short a ) {
31+ this .a = a ;
32+ }
33+
34+ public Double getB () {
35+ return b ;
36+ }
37+
38+ public void setB (Double b ) {
39+ this .b = b ;
40+ }
41+ }
42+
43+ public static class CustomCompound {
44+ private String name ;
45+ private Short someShort ;
46+ private Double someDouble ;
47+
48+ private CustomCompound () {
49+ }
50+
51+ public static Builder builder () {
52+ return new Builder ();
53+ }
54+
55+ public String getName () {
56+ return name ;
57+ }
58+
59+ public void setName (String name ) {
60+ this .name = name ;
61+ }
62+
63+ public Short getSomeShort () {
64+ return someShort ;
65+ }
66+
67+ public void setSomeShort (Short someShort ) {
68+ this .someShort = someShort ;
69+ }
70+
71+ public Double getSomeDouble () {
72+ return someDouble ;
73+ }
74+
75+ public void setSomeDouble (Double someDouble ) {
76+ this .someDouble = someDouble ;
77+ }
78+
79+ public static class Builder {
80+ private final CustomCompound instance = new CustomCompound ();
81+
82+ public Builder name (String name ) {
83+ instance .setName (name );
84+ return this ;
85+ }
86+
87+ public Builder someShort (Short someShort ) {
88+ instance .setSomeShort (someShort );
89+ return this ;
90+ }
91+
92+ public Builder someDouble (Double someDouble ) {
93+ instance .setSomeDouble (someDouble );
94+ return this ;
95+ }
96+
97+ public CustomCompound build () {
98+ return instance ;
99+ }
100+ }
101+ }
102+
103+ @ Test
104+ void testAllTypesSeparateH5 () throws Exception {
105+ try (SeekableByteChannel channel = ResourceLoader .loadResourceAsChannel ("all_types_separate.h5" )) {
106+ HdfFileReader reader = new HdfFileReader (channel ).readFile ();
107+
108+ // fixed_point
109+ HdfDataset fixedPointDs = reader .getDataset ("/fixed_point" ).orElseThrow ();
110+ TypedDataSource <HdfFixedPoint > fpSource = new TypedDataSource <>(channel , reader , fixedPointDs , HdfFixedPoint .class );
111+ HdfFixedPoint fp = fpSource .readScalar ();
112+ assertEquals (42 , fp .getInstance (Integer .class ).intValue ());
113+ TypedDataSource <Integer > intSource = new TypedDataSource <>(channel , reader , fixedPointDs , Integer .class );
114+ assertEquals (42 , intSource .readScalar ().intValue ());
115+ TypedDataSource <Long > longSource = new TypedDataSource <>(channel , reader , fixedPointDs , Long .class );
116+ assertEquals (42L , longSource .readScalar ().longValue ());
117+ TypedDataSource <BigInteger > biSource = new TypedDataSource <>(channel , reader , fixedPointDs , BigInteger .class );
118+ assertEquals (BigInteger .valueOf (42 ), biSource .readScalar ());
119+ TypedDataSource <BigDecimal > bdSource = new TypedDataSource <>(channel , reader , fixedPointDs , BigDecimal .class );
120+ assertEquals (BigDecimal .valueOf (42 ), bdSource .readScalar ());
121+ TypedDataSource <String > strSource = new TypedDataSource <>(channel , reader , fixedPointDs , String .class );
122+ assertEquals ("42" , strSource .readScalar ());
123+
124+ // float
125+ HdfDataset floatDs = reader .getDataset ("/float" ).orElseThrow ();
126+ TypedDataSource <HdfFloatPoint > floatPointSource = new TypedDataSource <>(channel , reader , floatDs , HdfFloatPoint .class );
127+ HdfFloatPoint floatPoint = floatPointSource .readScalar ();
128+ assertEquals (3.14f , floatPoint .getInstance (Float .class ), 0.001f );
129+ TypedDataSource <Float > floatJavaSource = new TypedDataSource <>(channel , reader , floatDs , Float .class );
130+ assertEquals (3.14f , floatJavaSource .readScalar (), 0.001f );
131+ TypedDataSource <Double > doubleSource = new TypedDataSource <>(channel , reader , floatDs , Double .class );
132+ assertEquals (3.140000104904175 , doubleSource .readScalar (), 0.000001 );
133+ TypedDataSource <String > floatStrSource = new TypedDataSource <>(channel , reader , floatDs , String .class );
134+ assertEquals ("3.14" , floatStrSource .readScalar ());
135+
136+ // time
137+ HdfDataset timeDs = reader .getDataset ("/time" ).orElseThrow ();
138+ TypedDataSource <HdfTime > timeSource = new TypedDataSource <>(channel , reader , timeDs , HdfTime .class );
139+ HdfTime time = timeSource .readScalar ();
140+ assertEquals (1672531200L , time .getInstance (Long .class ).longValue ());
141+ TypedDataSource <Long > timeLongSource = new TypedDataSource <>(channel , reader , timeDs , Long .class );
142+ assertEquals (1672531200L , timeLongSource .readScalar ().longValue ());
143+ TypedDataSource <BigInteger > timeBiSource = new TypedDataSource <>(channel , reader , timeDs , BigInteger .class );
144+ assertEquals (BigInteger .valueOf (1672531200L ), timeBiSource .readScalar ());
145+ TypedDataSource <String > timeStrSource = new TypedDataSource <>(channel , reader , timeDs , String .class );
146+ assertEquals ("1672531200" , timeStrSource .readScalar ());
147+
148+ // string
149+ HdfDataset stringDs = reader .getDataset ("/string" ).orElseThrow ();
150+ TypedDataSource <HdfString > hdfStringSource = new TypedDataSource <>(channel , reader , stringDs , HdfString .class );
151+ HdfString hdfString = hdfStringSource .readScalar ();
152+ assertEquals ("Hello HDF5!" , hdfString .getInstance (String .class ));
153+ TypedDataSource <String > javaStringSource = new TypedDataSource <>(channel , reader , stringDs , String .class );
154+ assertEquals ("Hello HDF5!" , javaStringSource .readScalar ());
155+
156+ // bitfield
157+ HdfDataset bitfieldDs = reader .getDataset ("/bitfield" ).orElseThrow ();
158+ TypedDataSource <HdfBitField > bitFieldSource = new TypedDataSource <>(channel , reader , bitfieldDs , HdfBitField .class );
159+ HdfBitField bitField = bitFieldSource .readScalar ();
160+ assertEquals ("10101010" , bitField .getInstance (String .class ));
161+ TypedDataSource <BitSet > bitSetSource = new TypedDataSource <>(channel , reader , bitfieldDs , BitSet .class );
162+ BitSet expectedBitSet = new BitSet ();
163+ expectedBitSet .set (1 );
164+ expectedBitSet .set (3 );
165+ expectedBitSet .set (5 );
166+ expectedBitSet .set (7 );
167+ assertEquals (expectedBitSet , bitSetSource .readScalar ());
168+ TypedDataSource <String > bitStrSource = new TypedDataSource <>(channel , reader , bitfieldDs , String .class );
169+ assertEquals ("10101010" , bitStrSource .readScalar ());
170+
171+ // compound
172+ HdfDataset compoundDs = reader .getDataset ("/compound" ).orElseThrow ();
173+ TypedDataSource <HdfCompound > hdfCompoundSource = new TypedDataSource <>(channel , reader , compoundDs , HdfCompound .class );
174+ HdfCompound hdfCompound = hdfCompoundSource .readScalar ();
175+ assertEquals ("123, 9.81" , hdfCompound .getInstance (String .class ));
176+ TypedDataSource <String > compoundStrSource = new TypedDataSource <>(channel , reader , compoundDs , String .class );
177+ assertEquals ("123, 9.81" , compoundStrSource .readScalar ());
178+ TypedDataSource <Compound > compoundJavaSource = new TypedDataSource <>(channel , reader , compoundDs , Compound .class );
179+ Compound compoundJava = compoundJavaSource .readScalar ();
180+ assertEquals (Short .valueOf ((short )123 ), compoundJava .getA ());
181+ assertEquals (9.81 , compoundJava .getB (), 0.001 );
182+ CompoundDatatype .addConverter (CustomCompound .class , (bytes , compoundDataType ) -> {
183+ Map <String , HdfCompoundMember > nameToMember = compoundDataType .getInstance (HdfCompound .class , bytes )
184+ .getMembers ()
185+ .stream ()
186+ .collect (Collectors .toMap (m -> m .getDatatype ().getName (), m -> m ));
187+ return CustomCompound .builder ()
188+ .name ("Name" )
189+ .someShort (nameToMember .get ("a" ).getInstance (Short .class ))
190+ .someDouble (nameToMember .get ("b" ).getInstance (Double .class ))
191+ .build ();
192+ });
193+ TypedDataSource <CustomCompound > customCompoundSource = new TypedDataSource <>(channel , reader , compoundDs , CustomCompound .class );
194+ CustomCompound customCompound = customCompoundSource .readScalar ();
195+ assertEquals ("Name" , customCompound .getName ());
196+ assertEquals (Short .valueOf ((short )123 ), customCompound .getSomeShort ());
197+ assertEquals (9.81 , customCompound .getSomeDouble (), 0.001 );
198+
199+ // opaque
200+ HdfDataset opaqueDs = reader .getDataset ("/opaque" ).orElseThrow ();
201+ TypedDataSource <HdfOpaque > opaqueSource = new TypedDataSource <>(channel , reader , opaqueDs , HdfOpaque .class );
202+ HdfOpaque opaque = opaqueSource .readScalar ();
203+ assertEquals ("DEADBEEF" , opaque .getInstance (String .class ));
204+ TypedDataSource <String > opaqueStrSource = new TypedDataSource <>(channel , reader , opaqueDs , String .class );
205+ assertEquals ("DEADBEEF" , opaqueStrSource .readScalar ());
206+ TypedDataSource <byte []> opaqueByteSource = new TypedDataSource <>(channel , reader , opaqueDs , byte [].class );
207+ byte [] expectedOpaqueBytes = {-34 , -83 , -66 , -17 };
208+ assertArrayEquals (expectedOpaqueBytes , opaqueByteSource .readScalar ());
209+
210+ // reference
211+ HdfDataset referenceDs = reader .getDataset ("/reference" ).orElseThrow ();
212+ TypedDataSource <HdfReference > referenceSource = new TypedDataSource <>(channel , reader , referenceDs , HdfReference .class );
213+ HdfReference reference = referenceSource .readScalar ();
214+ assertEquals ("/target" , reference .getInstance (String .class ));
215+ TypedDataSource <String > referenceStrSource = new TypedDataSource <>(channel , reader , referenceDs , String .class );
216+ assertEquals ("/target" , referenceStrSource .readScalar ());
217+ TypedDataSource <byte []> referenceByteSource = new TypedDataSource <>(channel , reader , referenceDs , byte [].class );
218+ byte [] expectedReferenceBytes = {64 , 20 , 0 , 0 , 0 , 0 , 0 , 0 };
219+ assertArrayEquals (expectedReferenceBytes , referenceByteSource .readScalar ());
220+
221+ // enum
222+ HdfDataset enumDs = reader .getDataset ("/enum" ).orElseThrow ();
223+ TypedDataSource <HdfEnum > enumSource = new TypedDataSource <>(channel , reader , enumDs , HdfEnum .class );
224+ HdfEnum hdfEnum = enumSource .readScalar ();
225+ assertEquals ("GREEN" , hdfEnum .getInstance (String .class ));
226+ TypedDataSource <String > enumStrSource = new TypedDataSource <>(channel , reader , enumDs , String .class );
227+ assertEquals ("GREEN" , enumStrSource .readScalar ());
228+
229+ // vlen
230+ HdfDataset vlenDs = reader .getDataset ("/vlen" ).orElseThrow ();
231+ TypedDataSource <HdfVariableLength > vlenSource = new TypedDataSource <>(channel , reader , vlenDs , HdfVariableLength .class );
232+ HdfVariableLength vlen = vlenSource .readScalar ();
233+ assertEquals ("[7, 8, 9]" , vlen .getInstance (String .class ));
234+ TypedDataSource <String > vlenStrSource = new TypedDataSource <>(channel , reader , vlenDs , String .class );
235+ assertEquals ("[7, 8, 9]" , vlenStrSource .readScalar ());
236+ TypedDataSource <Object > vlenObjSource = new TypedDataSource <>(channel , reader , vlenDs , Object .class );
237+ HdfData [] vlenData = (HdfData []) vlenObjSource .readScalar ();
238+ assertEquals (7 , vlenData [0 ].getInstance (Integer .class ).intValue ());
239+ assertEquals (8 , vlenData [1 ].getInstance (Integer .class ).intValue ());
240+ assertEquals (9 , vlenData [2 ].getInstance (Integer .class ).intValue ());
241+
242+ // array
243+ HdfDataset arrayDs = reader .getDataset ("/array" ).orElseThrow ();
244+ TypedDataSource <HdfArray > arraySource = new TypedDataSource <>(channel , reader , arrayDs , HdfArray .class );
245+ HdfArray hdfArray = arraySource .readScalar ();
246+ assertEquals ("[10, 20, 30]" , hdfArray .getInstance (String .class ));
247+ TypedDataSource <String > arrayStrSource = new TypedDataSource <>(channel , reader , arrayDs , String .class );
248+ assertEquals ("[10, 20, 30]" , arrayStrSource .readScalar ());
249+ TypedDataSource <HdfData []> arrayDataSource = new TypedDataSource <>(channel , reader , arrayDs , HdfData [].class );
250+ HdfData [] arrayData = arrayDataSource .readScalar ();
251+ assertEquals (10 , arrayData [0 ].getInstance (Integer .class ).intValue ());
252+ assertEquals (20 , arrayData [1 ].getInstance (Integer .class ).intValue ());
253+ assertEquals (30 , arrayData [2 ].getInstance (Integer .class ).intValue ());
254+
255+ assertEquals ("HdfSuperblock{version=0, freeSpaceVersion=0, rootGroupVersion=0, sharedHeaderVersion=0, sizeOfOffsets=8, sizeOfLengths=8, groupLeafNodeK=4, groupInternalNodeK=16, baseAddress=0, freeSpaceAddress=<Undefined>, endOfFileAddress=11144, driverInformationAddress=<Undefined>}" , reader .getSuperblock ().toString ());
256+ }
257+ }
258+ }
0 commit comments