Coverage Report - eu.simuline.octave.io.impl.ScalarStructReader
 
Classes in this File Line Coverage Branch Coverage Complexity
ScalarStructReader
81%
18/22
64%
9/14
6.5
 
 1  
 /*
 2  
  * Copyright 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  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import eu.simuline.octave.exception.OctaveParseException;
 23  
 import eu.simuline.octave.io.OctaveIO;
 24  
 import eu.simuline.octave.io.spi.OctaveDataReader;
 25  
 import eu.simuline.octave.type.OctaveObject;
 26  
 import eu.simuline.octave.type.OctaveStruct;
 27  
 
 28  
 /**
 29  
  * The reader for the octave type "scalar struct" 
 30  
  * (which is an encoding similar to "struct" 
 31  
  * introduced in octave 3.6, optimized to the 1x1 struct) 
 32  
  * reading an {@link OctaveObject} from a {@link BufferedReader}.
 33  
  */
 34  6
 public final class ScalarStructReader extends OctaveDataReader {
 35  
     private static final String NAME = "# name: ";
 36  
     private static final String LENGTH = "# length: ";
 37  
     private static final String N_DIMS2 = "# ndims: 2";
 38  
     private static final String V_DIMS2 = " 1 1";
 39  
 
 40  
     @Override
 41  
     public String octaveType() {
 42  2
         return "scalar struct";
 43  
     }
 44  
 
 45  
     @Override
 46  
     public OctaveStruct read(final BufferedReader reader) {
 47  
         // **** this i cannot see in Writer 
 48  
         // # ndims: 2
 49  4
         String line = OctaveIO.readerReadLine(reader);
 50  4
         if (!N_DIMS2.equals(line)) {
 51  0
             throw new OctaveParseException
 52  
                 ("JavaOctave does not support matrix structs, read=" + line);
 53  
         }
 54  
 
 55  
         // 1 1
 56  4
         line = OctaveIO.readerReadLine(reader);
 57  4
         if (!V_DIMS2.equals(line)) {
 58  0
             throw new OctaveParseException
 59  
                 ("JavaOctave does not support matrix structs, read=" + line);
 60  
         }
 61  
 
 62  
         // # length: 4
 63  4
         line = OctaveIO.readerReadLine(reader);
 64  4
         if (line == null || !line.startsWith(LENGTH)) {
 65  0
             throw new OctaveParseException
 66  
                 ("Expected <" + LENGTH + "> got <" + line + ">. ");
 67  
        }
 68  4
         final int length = Integer.parseInt(line.substring(LENGTH.length()));
 69  
         // only used during conversion
 70  
 
 71  4
         final Map<String, OctaveObject> data =
 72  
             new HashMap<String, OctaveObject>();
 73  
 
 74  246
         for (int i = 0; i < length; i++) {
 75  
             // # name: elemmatrix
 76  
             // Work around differences in number of line feeds 
 77  
             // in octave 3.4 and 3.6
 78  
             // keep reading until line is non-empty
 79  
             do {
 80  718
                 line = OctaveIO.readerReadLine(reader);
 81  718
             } while ("".equals(line));
 82  242
             if (!line.startsWith(NAME)) {
 83  0
                 throw new OctaveParseException
 84  
                     ("Expected <" + NAME + "> got <" + line + ">. ");
 85  
             }
 86  242
             final String subname = line.substring(NAME.length());
 87  
 
 88  
 
 89  
 
 90  
 
 91  
             // data...
 92  242
             final OctaveObject value = OctaveIO.read(reader);
 93  242
             data.put(subname, value);
 94  
         } // for
 95  
 
 96  4
         return new OctaveStruct(data);
 97  
     }
 98  
 
 99  
 }