-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathAccountEntryExtensionV1.java
More file actions
224 lines (202 loc) · 7.41 KB
/
AccountEntryExtensionV1.java
File metadata and controls
224 lines (202 loc) · 7.41 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten
package org.stellar.sdk.xdr;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.stellar.sdk.Base64Factory;
/**
* AccountEntryExtensionV1's original definition in the XDR file is:
*
* <pre>
* struct AccountEntryExtensionV1
* {
* Liabilities liabilities;
*
* union switch (int v)
* {
* case 0:
* void;
* case 2:
* AccountEntryExtensionV2 v2;
* }
* ext;
* };
* </pre>
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public class AccountEntryExtensionV1 implements XdrElement {
private Liabilities liabilities;
private AccountEntryExtensionV1Ext ext;
public void encode(XdrDataOutputStream stream) throws IOException {
liabilities.encode(stream);
ext.encode(stream);
}
public static AccountEntryExtensionV1 decode(XdrDataInputStream stream, int maxDepth)
throws IOException {
if (maxDepth <= 0) {
throw new IOException("Maximum decoding depth reached");
}
maxDepth -= 1;
AccountEntryExtensionV1 decodedAccountEntryExtensionV1 = new AccountEntryExtensionV1();
decodedAccountEntryExtensionV1.liabilities = Liabilities.decode(stream, maxDepth);
decodedAccountEntryExtensionV1.ext = AccountEntryExtensionV1Ext.decode(stream, maxDepth);
return decodedAccountEntryExtensionV1;
}
public static AccountEntryExtensionV1 decode(XdrDataInputStream stream) throws IOException {
return decode(stream, XdrDataInputStream.DEFAULT_MAX_DEPTH);
}
public static AccountEntryExtensionV1 fromXdrBase64(String xdr) throws IOException {
byte[] bytes = Base64Factory.getInstance().decode(xdr);
return fromXdrByteArray(bytes);
}
public static AccountEntryExtensionV1 fromXdrByteArray(byte[] xdr) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
xdrDataInputStream.setMaxInputLen(xdr.length);
return decode(xdrDataInputStream);
}
@Override
public String toJson() {
return XdrElement.gson.toJson(toJsonObject());
}
public static AccountEntryExtensionV1 fromJson(String json) {
return fromJsonObject(XdrElement.gson.fromJson(json, Object.class));
}
Object toJsonObject() {
LinkedHashMap<String, Object> jsonMap = new LinkedHashMap<>();
jsonMap.put("liabilities", liabilities.toJsonObject());
jsonMap.put("ext", ext.toJsonObject());
return jsonMap;
}
@SuppressWarnings("unchecked")
static AccountEntryExtensionV1 fromJsonObject(Object json) {
java.util.Map<String, Object> jsonMap = (java.util.Map<String, Object>) json;
AccountEntryExtensionV1 instance = new AccountEntryExtensionV1();
instance.liabilities = Liabilities.fromJsonObject(jsonMap.get("liabilities"));
instance.ext = AccountEntryExtensionV1Ext.fromJsonObject(jsonMap.get("ext"));
return instance;
}
/**
* AccountEntryExtensionV1Ext's original definition in the XDR file is:
*
* <pre>
* union switch (int v)
* {
* case 0:
* void;
* case 2:
* AccountEntryExtensionV2 v2;
* }
* </pre>
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public static class AccountEntryExtensionV1Ext implements XdrElement {
private Integer discriminant;
private AccountEntryExtensionV2 v2;
public void encode(XdrDataOutputStream stream) throws IOException {
stream.writeInt(discriminant);
switch (discriminant) {
case 0:
break;
case 2:
v2.encode(stream);
break;
}
}
public static AccountEntryExtensionV1Ext decode(XdrDataInputStream stream, int maxDepth)
throws IOException {
if (maxDepth <= 0) {
throw new IOException("Maximum decoding depth reached");
}
maxDepth -= 1;
AccountEntryExtensionV1Ext decodedAccountEntryExtensionV1Ext =
new AccountEntryExtensionV1Ext();
Integer discriminant = stream.readInt();
decodedAccountEntryExtensionV1Ext.setDiscriminant(discriminant);
switch (decodedAccountEntryExtensionV1Ext.getDiscriminant()) {
case 0:
break;
case 2:
decodedAccountEntryExtensionV1Ext.v2 = AccountEntryExtensionV2.decode(stream, maxDepth);
break;
default:
throw new IOException("Unknown discriminant value: " + discriminant);
}
return decodedAccountEntryExtensionV1Ext;
}
public static AccountEntryExtensionV1Ext decode(XdrDataInputStream stream) throws IOException {
return decode(stream, XdrDataInputStream.DEFAULT_MAX_DEPTH);
}
public static AccountEntryExtensionV1Ext fromXdrBase64(String xdr) throws IOException {
byte[] bytes = Base64Factory.getInstance().decode(xdr);
return fromXdrByteArray(bytes);
}
public static AccountEntryExtensionV1Ext fromXdrByteArray(byte[] xdr) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
xdrDataInputStream.setMaxInputLen(xdr.length);
return decode(xdrDataInputStream);
}
@Override
public String toJson() {
return XdrElement.gson.toJson(toJsonObject());
}
public static AccountEntryExtensionV1Ext fromJson(String json) {
return fromJsonObject(XdrElement.gson.fromJson(json, Object.class));
}
Object toJsonObject() {
if (discriminant == 0) {
return "v0";
}
if (discriminant == 2) {
LinkedHashMap<String, Object> jsonMap = new LinkedHashMap<>();
jsonMap.put("v2", v2.toJsonObject());
return jsonMap;
}
throw new IllegalArgumentException("Unknown discriminant: " + discriminant);
}
@SuppressWarnings("unchecked")
static AccountEntryExtensionV1Ext fromJsonObject(Object json) {
if (json instanceof String) {
String strVal = (String) json;
if (!(strVal.equals("v0"))) {
throw new IllegalArgumentException(
"Unexpected string '" + strVal + "' for AccountEntryExtensionV1Ext");
}
AccountEntryExtensionV1Ext instance = new AccountEntryExtensionV1Ext();
instance.discriminant = Integer.parseInt(strVal.substring(1));
return instance;
}
java.util.Map<String, Object> jsonMap = (java.util.Map<String, Object>) json;
if (jsonMap.containsKey("$schema")) {
jsonMap = new LinkedHashMap<>(jsonMap);
jsonMap.remove("$schema");
}
if (jsonMap.size() != 1) {
throw new IllegalArgumentException(
"Expected a single-key object for AccountEntryExtensionV1Ext, got: " + json);
}
String key = jsonMap.keySet().iterator().next();
Integer discriminant = Integer.parseInt(key.substring(1));
if (key.equals("v2")) {
AccountEntryExtensionV1Ext instance = new AccountEntryExtensionV1Ext();
instance.discriminant = discriminant;
instance.v2 = AccountEntryExtensionV2.fromJsonObject(jsonMap.get("v2"));
return instance;
}
throw new IllegalArgumentException(
"Unknown key '" + key + "' for AccountEntryExtensionV1Ext");
}
}
}