Coverage Report - eu.simuline.octave.io.impl.SparseBooleanReader
 
Classes in this File Line Coverage Branch Coverage Complexity
SparseBooleanReader
77%
21/27
62%
5/8
4.667
 
 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.BufferedReader;
 19  
 
 20  
 import eu.simuline.octave.exception.OctaveParseException;
 21  
 
 22  
 import eu.simuline.octave.io.OctaveIO;
 23  
 
 24  
 import eu.simuline.octave.io.spi.OctaveDataReader;
 25  
 import eu.simuline.octave.type.OctaveSparseBoolean;
 26  
 import eu.simuline.octave.util.StringUtil;
 27  
 
 28  
 /**
 29  
  * The reader for the octave type "sparse bool matrix" 
 30  
  * reading an {@link OctaveSparseBoolean} from a {@link BufferedReader}. 
 31  
  *
 32  
  * <pre>
 33  
  * # name: x
 34  
  * # type: sparse bool matrix
 35  
  * # nnz: 1
 36  
  * # rows: 1
 37  
  * # columns: 1
 38  
  * 1 1 1
 39  
  * </pre>
 40  
  */
 41  16
 public final class SparseBooleanReader extends OctaveDataReader {
 42  
 
 43  
     @Override
 44  
     public String octaveType() {
 45  2
         return "sparse bool matrix";
 46  
     }
 47  
 
 48  
     /**
 49  
      * @throws OctaveParseException
 50  
      */
 51  
     @Override
 52  
     @SuppressWarnings("checkstyle:magicnumber")
 53  
     // 3 is clear from SparseBooleanWriter 
 54  
     // **** seems buggy: headline "# type: sparse bool matrix\n" not read. 
 55  
     public OctaveSparseBoolean read(final BufferedReader reader) {
 56  28
         final int nnz     = parseHeader("# nnz: ", 
 57  14
                                         OctaveIO.readerReadLine(reader));
 58  28
         final int rows    = parseHeader("# rows: ", 
 59  14
                                         OctaveIO.readerReadLine(reader));
 60  28
         final int columns = parseHeader("# columns: ", 
 61  14
                                         OctaveIO.readerReadLine(reader));
 62  
 
 63  14
         final OctaveSparseBoolean sparse = 
 64  
             new OctaveSparseBoolean(rows, columns, nnz);
 65  28
         for (int n = 0; n < nnz; ++n) {
 66  14
             final String line = OctaveIO.readerReadLine(reader);
 67  14
             final String[] split = line.split(" ");
 68  14
             if (split.length != 3) { // NOPMD 
 69  0
                 throw new OctaveParseException("split.length != 3");
 70  
             }
 71  
             try {
 72  14
                 final int row       = Integer.parseInt(split[0]);
 73  14
                 final int column    = Integer.parseInt(split[1]);
 74  14
                 final boolean value = StringUtil.parseBoolean(split[2]);
 75  14
                 sparse.set(value, row, column);
 76  0
             } catch (final NumberFormatException e) {
 77  0
                 throw new OctaveParseException(e);
 78  14
             }
 79  
         }
 80  
 
 81  14
         return sparse;
 82  
     }
 83  
 
 84  
     private int parseHeader(final String prefix, final String line) {
 85  42
         if (line == null || !line.startsWith(prefix)) {
 86  0
             throw new OctaveParseException
 87  
                 ("Expected a line that should start with '" + prefix + 
 88  
                  "', got '" + line + "'");
 89  
         }
 90  
         try {
 91  42
             return Integer.parseInt(line.substring(prefix.length()));
 92  0
         } catch (final NumberFormatException e) {
 93  0
             throw new OctaveParseException(e);
 94  
         }
 95  
     }
 96  
 
 97  
 }