Coverage Report - eu.simuline.octave.io.impl.CellReader
 
Classes in this File Line Coverage Branch Coverage Complexity
CellReader
85%
23/27
68%
11/16
7
 
 1  
 /*
 2  
  * Copyright 2008, 2012 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  
 import eu.simuline.octave.io.OctaveIO;
 22  
 import eu.simuline.octave.io.spi.OctaveDataReader;
 23  
 import eu.simuline.octave.type.OctaveCell;
 24  
 import eu.simuline.octave.type.OctaveObject;
 25  
 
 26  
 //import static eu.simuline.octave.io.impl.CellWriter.TYPE_CELL;
 27  
 import static eu.simuline.octave.io.impl.CellWriter.NROWS;
 28  
 import static eu.simuline.octave.io.impl.CellWriter.NCOLUMNS;
 29  
 
 30  
 /**
 31  
  * The reader for the octave type "cell" 
 32  
  * reading in an {@link OctaveCell} from a {@link BufferedReader}. 
 33  
  */
 34  18
 public final class CellReader extends OctaveDataReader {
 35  
 
 36  
     @Override
 37  
     public String octaveType() {
 38  2
         return "cell";
 39  
     }
 40  
 
 41  
     @Override
 42  
     public OctaveCell read(final BufferedReader reader) {
 43  268
         String line = OctaveIO.readerReadLine(reader);
 44  268
         String token = NROWS;
 45  
 
 46  268
         if (!line.startsWith(token)) {
 47  0
             throw new OctaveParseException
 48  
                 ("Expected <" + token + ">, but got <" + line + ">");
 49  
         }
 50  268
         final int nrows = Integer.parseInt(line.substring(token.length()));
 51  
 
 52  268
         line = OctaveIO.readerReadLine(reader);
 53  268
         token = NCOLUMNS;
 54  268
         if (!line.startsWith(token)) {
 55  0
             throw new OctaveParseException
 56  
                 ("Expected <" + token + ">, but got <" + line + ">");
 57  
         }
 58  268
         final int ncols = Integer.parseInt(line.substring(token.length()));
 59  
 
 60  268
         final OctaveCell octaveCell = new OctaveCell(nrows, ncols);
 61  
 
 62  560
         for (int col = 1; col <= ncols; col++) {
 63  602
             for (int row = 1; row <= nrows; row++) {
 64  
                 // Work around differences in number of line feeds 
 65  
                 // in octave 3.4 and 3.6: 
 66  
                 // keep reading until line is non-empty
 67  
                 do {
 68  378
                     line = OctaveIO.readerReadLine(reader);
 69  378
                 } while ("".equals(line));
 70  310
                 token = "# name: <cell-element>";
 71  310
                 if (!line.equals(token)) {
 72  0
                     throw new OctaveParseException
 73  
                         ("Expected <" + token + ">, but got <" + line + ">");
 74  
                 }
 75  310
                 final OctaveObject octaveType = OctaveIO.read(reader);
 76  310
                 octaveCell.set(octaveType, row, col);
 77  
             }
 78  292
             line = OctaveIO.readerReadLine(reader);
 79  292
             token = "";
 80  292
             if (line == null || !line.equals(token)) {
 81  0
                 throw new OctaveParseException
 82  
                     ("Expected <" + token + ">, but got <" + line + ">");
 83  
             }
 84  
         }
 85  
 
 86  268
         return octaveCell;
 87  
     }
 88  
 
 89  
 }