1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package eu.simuline.octave.type;
17
18 import java.util.Map;
19 import java.util.TreeMap;
20
21 import eu.simuline.octave.exception.OctaveClassCastException;
22 import eu.simuline.octave.type.cast.Cast;
23
24
25
26
27
28 public final class OctaveStruct implements OctaveObject {
29
30
31 private static final int PRIME = 31;
32
33
34 private final Map<String, OctaveObject> data;
35
36
37
38
39 public OctaveStruct() {
40
41
42 this.data = new TreeMap<String, OctaveObject>();
43 }
44
45
46
47
48
49
50
51 public OctaveStruct(final Map<String, OctaveObject> data) {
52 this.data = data;
53 }
54
55
56
57
58
59 @SuppressWarnings("PMD.AvoidThrowingNullPointerException")
60 public void set(final String name, final OctaveObject value) {
61 if (value == null) {
62 throw new NullPointerException
63 ("Cannot set field with null-name in octave struct. ");
64 }
65 this.data.put(name, value);
66 }
67
68
69
70
71
72
73
74
75
76
77 public OctaveObject get(final String key) {
78 final OctaveObject value = this.data.get(key);
79 return (value == null) ? null : value.shallowCopy();
80 }
81
82
83
84
85
86
87
88
89
90
91 public <T extends OctaveObject> T get(final Class<T> castClass,
92 final String key) {
93 return Cast.cast(castClass, get(key));
94 }
95
96
97
98
99 public Map<String, OctaveObject> getData() {
100 return this.data;
101 }
102
103 @Override
104 public OctaveStruct shallowCopy() {
105 return new OctaveStruct(this.data);
106 }
107
108 @Override
109 public int hashCode() {
110 int result = 1;
111 result = PRIME * result + ((this.data == null) ? 0 : data.hashCode());
112 return result;
113 }
114
115 @Override
116 public boolean equals(final Object obj) {
117 if (this == obj) {
118 return true;
119 }
120 if (obj == null) {
121 return false;
122 }
123 if (getClass() != obj.getClass()) {
124 return false;
125 }
126 final OctaveStruct/../../eu/simuline/octave/type/OctaveStruct.html#OctaveStruct">OctaveStruct other = (OctaveStruct) obj;
127 if (this.data == null) {
128 return other.data == null;
129
130
131
132 } else {
133 return this.data.equals(other.data);
134
135
136
137 }
138
139 }
140
141 }