View Javadoc
1   /*
2    * Copyright 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.BufferedReader;
19  
20  import eu.simuline.octave.io.OctaveIO;
21  import eu.simuline.octave.io.spi.OctaveDataReader;
22  import eu.simuline.octave.type.OctaveComplex;
23  import eu.simuline.octave.util.StringUtil;
24  
25  /**
26   * The reader for the octave type "complex scalar" with 'double' components 
27   * reading an {@link OctaveComplex} from a {@link BufferedReader}. 
28   *
29   * Format is:
30   *
31   * <pre>
32   * # type: complex scalar
33   * (1.2,3.4)
34   * </pre>
35   */
36  public final class ComplexScalarReader extends OctaveDataReader {
37  
38      @Override
39      public String octaveType() {
40          return "complex scalar";
41      }
42  
43      // **** parsing of complex numbers shall be done in StringUtil centralized 
44      @Override
45      public OctaveComplex read(final BufferedReader reader) {
46          final String line = OctaveIO.readerReadLine(reader);
47          final int commaIndex = line.indexOf(',');
48          final double real = StringUtil
49  	    .parseDouble(line.substring(1, commaIndex));
50          final double imag = StringUtil
51  	    .parseDouble(line.substring(commaIndex + 1, line.length() - 1));
52          final OctaveComplexplex.html#OctaveComplex">OctaveComplex complex = new OctaveComplex(1, 1);
53          complex.setReal(real, 1, 1);
54          complex.setImag(imag, 1, 1);
55          return complex;
56      }
57  
58  }