Coverage Report - eu.simuline.octave.io.DataWriteFunctor
 
Classes in this File Line Coverage Branch Coverage Complexity
DataWriteFunctor
95%
20/21
75%
3/4
3
 
 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;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.Writer;
 20  
 import java.util.Map;
 21  
 
 22  
 import eu.simuline.octave.exception.OctaveIOException;
 23  
 import eu.simuline.octave.exec.WriteFunctor;
 24  
 import eu.simuline.octave.type.OctaveObject;
 25  
 
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 /**
 30  
  * Write data from {@link OctaveObject}s into a {@link Map}. 
 31  
  */
 32  
 // ER: Very strange: whereas this write functor writes a map, 
 33  
 // the according read functor reads a single variable only: 
 34  
 // see OctaveIO 
 35  
 final class DataWriteFunctor implements WriteFunctor {
 36  
 
 37  
     private static final String MSG_IOE_UNEXP = "Unexpected IOException";
 38  
 
 39  2
     private static final Log LOG = LogFactory.getLog(DataWriteFunctor.class);
 40  
 
 41  
     /**
 42  
      * A map from variable names to according octave objects. 
 43  
      */
 44  
     private final Map<String, OctaveObject> varName2Value;
 45  
 
 46  
     /**
 47  
      * @param varName2Value
 48  
      *    A map from variable names to according octave objects. 
 49  
      */
 50  504
     DataWriteFunctor(final Map<String, OctaveObject> varName2Value) {
 51  504
         this.varName2Value = varName2Value;
 52  504
     }
 53  
 
 54  
     /**
 55  
      * Writes the name-value pairs of map {@link #varName2Value} 
 56  
      * to <code>writer</code>. 
 57  
      *
 58  
      * @param writer
 59  
      *    the writer this variable to value configuration is to be written to. 
 60  
      */
 61  
     public void doWrites(final Writer writer) {
 62  
         try {
 63  
             // Enter octave in "read data from input mode"
 64  504
             LOG.trace("write: 'load(\"-text\", \"-\")' " + 
 65  
                       "to start read data from input mode");
 66  504
             writer.write("load(\"-text\", \"-\")\n");
 67  
             // Push the data into octave
 68  
             for (final Map.Entry<String, OctaveObject> entry 
 69  504
                      : this.varName2Value.entrySet()) {
 70  
 
 71  520
                 final String        name = entry.getKey();
 72  520
                 final OctaveObject value = entry.getValue();
 73  520
                 if (LOG.isTraceEnabled()) {
 74  0
                     LOG.trace("write: variable '" + name + 
 75  
                               "', value=<<<" + value + ">>>");
 76  
                 }
 77  520
                 OctaveIO.write(writer, name, value);
 78  520
             }
 79  
             // Exit octave from read data mode
 80  504
             LOG.trace("write: '# name:' to exit octave from read data mode");
 81  504
             writer.write("# name: \n");
 82  504
             writer.flush();
 83  4
         } catch (final IOException e) {
 84  
             // Will happen when we write to a dead octave process
 85  4
             LOG.debug(MSG_IOE_UNEXP, e);
 86  4
             throw new OctaveIOException(MSG_IOE_UNEXP, e);
 87  500
         }
 88  500
     }
 89  
 
 90  
 }