View Javadoc
1   /*
2    * Copyright 2008, 2009 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.OctaveIO;
22  import eu.simuline.octave.io.spi.OctaveDataWriter;
23  import eu.simuline.octave.type.OctaveCell;
24  import eu.simuline.octave.type.OctaveObject;
25  
26  /**
27   * The writer for the octave type "cell" 
28   * writing an {@link OctaveCell} to a {@link Writer}. 
29   */
30  public final class CellWriter extends OctaveDataWriter<OctaveCell> {
31  
32      // **** same as in StructWriter 
33      static final String TYPE_CELL = "# type: cell";
34  
35      // **** same as in AbstractPrimitive... 
36      static final String NROWS    = "# rows: ";
37      static final String NCOLUMNS = "# columns: ";
38  
39  
40      @Override
41      public Class<OctaveCell> javaType() {
42          return OctaveCell.class;
43      }
44  
45      @Override
46      public void write(final Writer writer,
47  		      final OctaveCell octaveCell) throws IOException {
48          final int rows    = octaveCell.getSize(1);
49          final int columns = octaveCell.getSize(2);
50          writer.write("# type: cell\n");
51          writer.write("# rows: " + rows + "\n");
52          writer.write("# columns: " + columns + "\n");
53          for (int c = 1; c <= columns; ++c) {
54              for (int r = 1; r <= rows; ++r) {
55                  final OctaveObject value = octaveCell.get(r, c);
56                  OctaveIO.write(writer, "<cell-element>", value);
57              }
58              writer.write("\n");
59          }
60      }
61  
62  }