-
Notifications
You must be signed in to change notification settings - Fork 127
/
Data.java
200 lines (181 loc) · 6.62 KB
/
Data.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2024 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang;
import java.nio.charset.StandardCharsets;
/**
* A data container.
*
* @since 0.1
*/
@Versionized
@SuppressWarnings("PMD.TooManyMethods")
public interface Data {
/**
* Attach data to the object.
* @param data Data.
* @todo #2931:60min Change the data storage architecture. Current implementation allows the
* presence of two methods for data manipulations: {@link Data#attach(byte[])} to set data and
* {@link Data#delta()} to get data; which does not seem to be object oriented. It also
* requires every object to have reserved place for possible injected data. In our case, every
* {@link PhDefault} has {@code PhDefault#data} variable. It would be much better to have this
* data only inside some decorator. The main difficulty here is - child attributes of
* decorated object should know that their \rho is decorated and contains data.
*/
void attach(byte[] data);
/**
* Take the data.
* @return The data
*/
byte[] delta();
/**
* Makes a {@link Phi} out of a primitive Java object, like {@link String} or {@link Integer}.
*
* <p>This is more convenient than making EOstring, then making EObytes fill it up with data and
* then injecting bytes to string.
* This class is used in Java tests mostly for the sake of brevity.
* In auto-generated Java code we use EOstring and then wrap it with {@link PhData}.
*
* @since 0.1
*/
final class ToPhi implements Phi {
/**
* Phi object.
*/
private final Phi object;
/**
* Ctor.
* @param obj Data
*/
public ToPhi(final Object obj) {
this.object = Data.ToPhi.toPhi(obj);
}
@Override
public boolean equals(final Object obj) {
return this.object.equals(obj);
}
@Override
public int hashCode() {
return this.object.hashCode();
}
@Override
public Phi copy() {
return this.object.copy();
}
@Override
public Phi take(final String name) {
return this.object.take(name);
}
@Override
public boolean put(final int pos, final Phi obj) {
return this.object.put(pos, obj);
}
@Override
public boolean put(final String name, final Phi obj) {
return this.object.put(name, obj);
}
@Override
public String locator() {
return this.object.locator();
}
@Override
public String forma() {
return this.object.forma();
}
@Override
public String φTerm() {
return this.object.φTerm();
}
@Override
public String toString() {
return this.object.toString();
}
@Override
public void attach(final byte[] data) {
this.object.attach(data);
}
@Override
public byte[] delta() {
return this.object.delta();
}
/**
* Convert to Phi object.
* @param obj Object to convert
* @return Constructed Phi
* @checkstyle CyclomaticComplexityCheck (100 lines)
*/
@SuppressWarnings("PMD.CognitiveComplexity")
private static Phi toPhi(final Object obj) {
final Phi phi;
final Phi eolang = Phi.Φ.take("org").take("eolang");
if (obj instanceof Boolean) {
if (obj.equals(true)) {
phi = eolang.take("true");
} else {
phi = eolang.take("false");
}
} else if (obj instanceof Phi[]) {
final Phi tuple = eolang.take("tuple");
Phi argument = tuple.take("empty");
Phi tup;
for (final Phi element : (Phi[]) obj) {
tup = tuple.copy();
tup.put(0, argument);
tup.put(1, element);
argument = tup;
}
phi = argument;
} else if (obj instanceof byte[]) {
phi = eolang.take("bytes").copy();
phi.attach((byte[]) obj);
} else if (obj instanceof Number) {
final double value = ((Number) obj).doubleValue();
if (Double.isNaN(value)) {
phi = eolang.take("nan");
} else if (value == Double.POSITIVE_INFINITY) {
phi = eolang.take("positive-infinity");
} else if (value == Double.NEGATIVE_INFINITY) {
phi = eolang.take("negative-infinity");
} else {
phi = eolang.take("number").copy();
final Phi bts = eolang.take("bytes").copy();
bts.attach(new BytesOf(value).take());
phi.put(0, bts);
}
} else if (obj instanceof String) {
phi = eolang.take("string").copy();
final Phi bts = eolang.take("bytes").copy();
bts.attach(((String) obj).getBytes(StandardCharsets.UTF_8));
phi.put(0, bts);
} else {
throw new IllegalArgumentException(
String.format(
"Unknown type of data: %s",
obj.getClass().getCanonicalName()
)
);
}
return phi;
}
}
}