1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package eu.simuline.octave.io;
17
18 import static eu.simuline.octave.io.OctaveIO.readerReadLine;
19
20 import java.io.BufferedReader;
21 import java.io.Reader;
22 import java.util.Map;
23
24 import eu.simuline.octave.exception.OctaveParseException;
25 import eu.simuline.octave.exec.ReadFunctor;
26 import eu.simuline.octave.type.OctaveObject;
27
28
29
30
31
32
33
34
35 final class DataReadFunctor implements ReadFunctor {
36
37
38
39
40 private final String name;
41
42
43
44
45 private OctaveObject data;
46
47
48
49
50 DataReadFunctor(final String name) {
51 this.name = name;
52 }
53
54
55
56
57 @Override
58 public void doReads(final Reader reader) {
59 final BufferedReader bufferedReader = new BufferedReader(reader);
60 final String createByOctaveLine = readerReadLine(bufferedReader);
61 if (createByOctaveLine == null ||
62 !createByOctaveLine.startsWith("# Created by Octave")) {
63 throw new OctaveParseException
64 ("Not created by Octave?: '" + createByOctaveLine + "'");
65 }
66 final Map<String, OctaveObject> map =
67 OctaveIO.readWithName(bufferedReader);
68 if (map.size() != 1) {
69 throw new OctaveParseException
70 ("Expected map of size 1 but got " + map + "'");
71 }
72 if (!map.containsKey(this.name)) {
73 throw new OctaveParseException
74 ("Expected variable named '" + this.name +
75 "' but got '" + map + "'");
76 }
77 this.data = map.get(this.name);
78 }
79
80
81
82
83 public OctaveObject getData() {
84 return this.data;
85 }
86
87 }