Skip to content

Commit

Permalink
Java: use lombok to simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed May 20, 2024
1 parent ac16535 commit a00b45c
Show file tree
Hide file tree
Showing 67 changed files with 913 additions and 2,980 deletions.
389 changes: 52 additions & 337 deletions lib/xdrgen/generators/java.rb

Large diffs are not rendered by default.

128 changes: 55 additions & 73 deletions lib/xdrgen/generators/java/XdrString.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,72 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.nio.charset.StandardCharsets;
import lombok.Value;
import org.stellar.sdk.Base64Factory;

@Value
public class XdrString implements XdrElement {
private byte[] bytes;
byte[] bytes;

public XdrString(byte[] bytes) {
this.bytes = bytes;
}
public XdrString(byte[] bytes) {
this.bytes = bytes;
}

public XdrString(String text) {
this.bytes = text.getBytes(Charset.forName("UTF-8"));
}
public XdrString(String text) {
this.bytes = text.getBytes(StandardCharsets.UTF_8);
}

@Override
public void encode(XdrDataOutputStream stream) throws IOException {
stream.writeInt(this.bytes.length);
stream.write(this.bytes, 0, this.bytes.length);
}
@Override
public void encode(XdrDataOutputStream stream) throws IOException {
stream.writeInt(this.bytes.length);
stream.write(this.bytes, 0, this.bytes.length);
}

public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length "+size+" exceeds max size "+maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
return new XdrString(bytes);
public static XdrString decode(XdrDataInputStream stream, int maxSize) throws IOException {
int size = stream.readInt();
if (size > maxSize) {
throw new InvalidClassException("String length " + size + " exceeds max size " + maxSize);
}
byte[] bytes = new byte[size];
stream.read(bytes);
return new XdrString(bytes);
}

public byte[] getBytes() {
return this.bytes;
}
@Override
public String toXdrBase64() throws IOException {
return Base64Factory.getInstance().encodeToString(toXdrByteArray());
}

@Override
public String toXdrBase64() throws IOException {
return Base64Factory.getInstance().encodeToString(toXdrByteArray());
}

@Override
public byte[] toXdrByteArray() throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
encode(xdrDataOutputStream);
return byteArrayOutputStream.toByteArray();
}

public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException {
byte[] bytes = Base64Factory.getInstance().decode(xdr);
return fromXdrByteArray(bytes, maxSize);
}

public static XdrString fromXdrBase64(String xdr) throws IOException {
return fromXdrBase64(xdr, Integer.MAX_VALUE);
}

public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream, maxSize);
}

public static XdrString fromXdrByteArray(byte[] xdr) throws IOException {
return fromXdrByteArray(xdr, Integer.MAX_VALUE);
}
@Override
public byte[] toXdrByteArray() throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XdrDataOutputStream xdrDataOutputStream = new XdrDataOutputStream(byteArrayOutputStream);
encode(xdrDataOutputStream);
return byteArrayOutputStream.toByteArray();
}

@Override
public int hashCode() {
return Arrays.hashCode(this.bytes);
}
public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException {
byte[] bytes = Base64Factory.getInstance().decode(xdr);
return fromXdrByteArray(bytes, maxSize);
}

@Override
public boolean equals(Object object) {
if (object == null || !(object instanceof XdrString)) {
return false;
}
public static XdrString fromXdrBase64(String xdr) throws IOException {
return fromXdrBase64(xdr, Integer.MAX_VALUE);
}

XdrString other = (XdrString) object;
return Arrays.equals(this.bytes, other.bytes);
}
public static XdrString fromXdrByteArray(byte[] xdr, int maxSize) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xdr);
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream, maxSize);
}

@Override
public String toString() {
return new String(bytes, Charset.forName("UTF-8"));
}
}
public static XdrString fromXdrByteArray(byte[] xdr) throws IOException {
return fromXdrByteArray(xdr, Integer.MAX_VALUE);
}

@Override
public String toString() {
return new String(bytes, StandardCharsets.UTF_8);
}
}
30 changes: 4 additions & 26 deletions lib/xdrgen/generators/java/XdrUnsignedHyperInteger.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.stellar.sdk.xdr;
package <%= @namespace %>;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Objects;
import lombok.Value;
import org.stellar.sdk.Base64Factory;

/**
Expand All @@ -13,10 +13,11 @@ import org.stellar.sdk.Base64Factory;
* @see <a href="https://datatracker.ietf.org/doc/html/rfc4506#section-4.5">XDR: External Data
* Representation Standard</a>
*/
@Value
public class XdrUnsignedHyperInteger implements XdrElement {
public static final BigInteger MAX_VALUE = new BigInteger("18446744073709551615");
public static final BigInteger MIN_VALUE = BigInteger.ZERO;
private final BigInteger number;
BigInteger number;

public XdrUnsignedHyperInteger(BigInteger number) {
if (number.compareTo(MIN_VALUE) < 0 || number.compareTo(MAX_VALUE) > 0) {
Expand Down Expand Up @@ -55,10 +56,6 @@ public class XdrUnsignedHyperInteger implements XdrElement {
return paddedBytes;
}

public BigInteger getNumber() {
return number;
}

@Override
public String toXdrBase64() throws IOException {
return Base64Factory.getInstance().encodeToString(toXdrByteArray());
Expand All @@ -82,23 +79,4 @@ public class XdrUnsignedHyperInteger implements XdrElement {
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream);
}

@Override
public int hashCode() {
return Objects.hash(this.number);
}

@Override
public boolean equals(Object object) {
if (!(object instanceof XdrUnsignedHyperInteger)) {
return false;
}

XdrUnsignedHyperInteger other = (XdrUnsignedHyperInteger) object;
return Objects.equals(this.number, other.number);
}

public String toString() {
return "XdrUnsignedInteger(number=" + this.getNumber() + ")";
}
}
30 changes: 4 additions & 26 deletions lib/xdrgen/generators/java/XdrUnsignedInteger.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.stellar.sdk.xdr;
package <%= @namespace %>;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Objects;
import lombok.Value;
import org.stellar.sdk.Base64Factory;

/**
Expand All @@ -12,10 +12,11 @@ import org.stellar.sdk.Base64Factory;
* @see <a href="https://datatracker.ietf.org/doc/html/rfc4506#section-4.2">XDR: External Data
* Representation Standard</a>
*/
@Value
public class XdrUnsignedInteger implements XdrElement {
public static final long MAX_VALUE = (1L << 32) - 1;
public static final long MIN_VALUE = 0;
private final Long number;
Long number;

public XdrUnsignedInteger(Long number) {
if (number < MIN_VALUE || number > MAX_VALUE) {
Expand All @@ -32,10 +33,6 @@ public class XdrUnsignedInteger implements XdrElement {
this.number = number.longValue();
}

public Long getNumber() {
return number;
}

public static XdrUnsignedInteger decode(XdrDataInputStream stream) throws IOException {
int intValue = stream.readInt();
long uint32Value = Integer.toUnsignedLong(intValue);
Expand Down Expand Up @@ -70,23 +67,4 @@ public class XdrUnsignedInteger implements XdrElement {
XdrDataInputStream xdrDataInputStream = new XdrDataInputStream(byteArrayInputStream);
return decode(xdrDataInputStream);
}

@Override
public int hashCode() {
return Objects.hash(this.number);
}

@Override
public boolean equals(Object object) {
if (!(object instanceof XdrUnsignedInteger)) {
return false;
}

XdrUnsignedInteger other = (XdrUnsignedInteger) object;
return Objects.equals(this.number, other.number);
}

public String toString() {
return "XdrUnsignedInteger(number=" + this.getNumber() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.io.IOException;

import static MyXDR.Constants.*;
import org.stellar.sdk.Base64Factory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -20,16 +19,16 @@
* </pre>
*/
public enum AccountFlags implements XdrElement {
AUTH_REQUIRED_FLAG(1),
;
private int mValue;
AUTH_REQUIRED_FLAG(1);

private final int value;

AccountFlags(int value) {
mValue = value;
this.value = value;
}

public int getValue() {
return mValue;
return value;
}

public static AccountFlags decode(XdrDataInputStream stream) throws IOException {
Expand Down
Loading

0 comments on commit a00b45c

Please sign in to comment.