Coverage Report - eu.simuline.octave.io.impl.AbstractPrimitiveMatrixWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractPrimitiveMatrixWriter
100%
19/19
80%
8/10
2
 
 1  
 
 2  
 package eu.simuline.octave.io.impl;
 3  
 
 4  
 import eu.simuline.octave.io.spi.OctaveDataWriter;
 5  
 import eu.simuline.octave.type.matrix.AbstractGenericMatrix;
 6  
 
 7  
 import java.io.IOException;
 8  
 import java.io.Writer;
 9  
 
 10  
 /**
 11  
  * Common Writer class for primitive java types: Boolean, Double, Integer.... 
 12  
  *
 13  
  * @param <T>
 14  
  *    the type to be written out 
 15  
  *    which has to extend {@link AbstractGenericMatrix}. 
 16  
  */
 17  16
 abstract class AbstractPrimitiveMatrixWriter
 18  
     <T extends AbstractGenericMatrix<?,?>> 
 19  
     extends OctaveDataWriter<T> {
 20  
 
 21  
     protected static final String NDIMS    = "# ndims: ";
 22  
     protected static final String NROWS    = "# rows: ";
 23  
     protected static final String NCOLUMNS = "# columns: ";
 24  
 
 25  
     // prevents instantiation (as does keyword abstract); for pmd only 
 26  8
     protected AbstractPrimitiveMatrixWriter() {
 27  8
     }
 28  
 
 29  
     // **** indicates that it would be a good idea 
 30  
     // to merge reader and writer 
 31  
     // and to have one OctaveObject for each octave type. 
 32  
     protected abstract String octaveMatrixType();
 33  
 
 34  
     // **** indicates that it would be a good idea 
 35  
     // to merge reader and writer 
 36  
     // and to have one OctaveObject for each octave type. 
 37  
     protected abstract String octaveScalarType();
 38  
 
 39  
     // is used for integer types only, for logical and floating point, 
 40  
     // it is overwritten 
 41  
     public void write(final Writer writer, 
 42  
                       final T octaveMatrix) throws IOException {
 43  16
         if (octaveMatrix.getSizeLength() == 2 && 
 44  16
             octaveMatrix.getSize(1) == 1 && 
 45  4
             octaveMatrix.getSize(2) == 1) {
 46  
 
 47  4
             writer.write("# type: " + octaveScalarType() + "\n");
 48  4
             writer.write(octaveMatrix.getPlainString(0) + "\n");
 49  
         } else {
 50  12
             writer.write("# type: " + octaveMatrixType() + "\n");
 51  
             // **** note: unlike for floating types and bool, 
 52  
             // for integer types, 
 53  
             // there is no special case for 2 dimensions, i.e. matrices 
 54  
             // using saveData2d(writer, octaveMatrix);
 55  12
             saveDataVectorized(writer, octaveMatrix);
 56  
         }
 57  16
     }
 58  
 
 59  
     protected void saveDataVectorized(final Writer writer, 
 60  
                                       final T octaveMatrix) 
 61  
         throws IOException {
 62  
 
 63  224
         writer.write(NDIMS + octaveMatrix.getSizeLength() + "\n");
 64  1008
         for (int idx = 1; idx <= octaveMatrix.getSizeLength(); idx++) {
 65  784
             writer.write(" " + octaveMatrix.getSize(idx));
 66  
         }
 67  
 
 68  224
         int len = octaveMatrix.dataSize();
 69  3656
         for (int idx = 0; idx < len; idx++) {
 70  3432
             writer.write("\n " + octaveMatrix.getPlainString(idx));
 71  
         }
 72  
 
 73  224
         writer.write("\n");
 74  224
     }
 75  
 
 76  
 }