Coverage Report - eu.simuline.octave.io.impl.SparseBooleanWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
SparseBooleanWriter
100%
13/13
75%
3/4
2
 
 1  
 /*
 2  
  * Copyright 2010 Ange Optimization ApS
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package eu.simuline.octave.io.impl;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.Writer;
 20  
 
 21  
 import eu.simuline.octave.io.spi.OctaveDataWriter;
 22  
 import eu.simuline.octave.type.OctaveSparseBoolean;
 23  
 
 24  
 /**
 25  
  * The writer for the octave type "sparse bool matrix" 
 26  
  * writing an {@link OctaveSparseBoolean} to a {@link Writer}. 
 27  
  *
 28  
  * <pre>
 29  
  * # name: x
 30  
  * # type: sparse bool matrix
 31  
  * # nnz: 1
 32  
  * # rows: 1
 33  
  * # columns: 1
 34  
  * 1 1 1
 35  
  * </pre>
 36  
  */
 37  14
 public final class SparseBooleanWriter 
 38  
     extends OctaveDataWriter<OctaveSparseBoolean> {
 39  
 
 40  
     @Override
 41  
     public Class<OctaveSparseBoolean> javaType() {
 42  2
         return OctaveSparseBoolean.class;
 43  
     }
 44  
 
 45  
     @Override
 46  
     public void write(final Writer writer,
 47  
                       final OctaveSparseBoolean octaveSparseBoolean) 
 48  
         throws IOException {
 49  12
         final int nnz = octaveSparseBoolean.getNnz();
 50  12
         writer.write("# type: sparse bool matrix\n");
 51  12
         writer.write("# nnz: " + nnz + "\n");
 52  12
         writer.write("# rows: " + octaveSparseBoolean.getRows() + "\n");
 53  12
         writer.write("# columns: " + octaveSparseBoolean.getColumns() + "\n");
 54  12
         final int[] rowIndexes = octaveSparseBoolean.getRowIndexes();
 55  12
         final int[] columnIndexes = octaveSparseBoolean.getColumnIndexes();
 56  12
         final boolean[] data = octaveSparseBoolean.getData();
 57  26
         for (int n = 0; n < nnz; ++n) {
 58  14
             writer.write(rowIndexes[n] + " " + 
 59  
                          columnIndexes[n] + " " + 
 60  
                          (data[n] ? "1" : "0") + "\n");
 61  
         }
 62  12
     }
 63  
 
 64  
 }