Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-2307: [java] list of primitive #2389

Merged
merged 4 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 89 additions & 45 deletions lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,30 +317,16 @@ public String toString() {
}
}

/** Default implementation of an array. */
@SuppressWarnings(value = "unchecked")
public static class Array<T> extends AbstractList<T> implements GenericArray<T>, Comparable<GenericArray<T>> {
private static final Object[] EMPTY = new Object[0];
public static abstract class AbstractArray<T> extends AbstractList<T>
implements GenericArray<T>, Comparable<GenericArray<T>> {
private final Schema schema;
private int size;
private Object[] elements = EMPTY;

public Array(int capacity, Schema schema) {
if (schema == null || !Type.ARRAY.equals(schema.getType()))
throw new AvroRuntimeException("Not an array schema: " + schema);
this.schema = schema;
if (capacity != 0)
elements = new Object[capacity];
}
protected int size = 0;

public Array(Schema schema, Collection<T> c) {
public AbstractArray(Schema schema) {
if (schema == null || !Type.ARRAY.equals(schema.getType()))
throw new AvroRuntimeException("Not an array schema: " + schema);
this.schema = schema;
if (c != null) {
elements = new Object[c.size()];
addAll(c);
}
}

@Override
Expand All @@ -354,22 +340,26 @@ public int size() {
}

@Override
public void clear() {
// Let GC do its work
Arrays.fill(elements, 0, size, null);
public void reset() {
size = 0;
}

@Override
public void reset() {
size = 0;
public int compareTo(GenericArray<T> that) {
return GenericData.get().compare(this, that, this.getSchema());
}

@Override
public void prune() {
if (size < elements.length) {
Arrays.fill(elements, size, elements.length, null);
public boolean equals(final Object o) {
if (!(o instanceof Collection)) {
return false;
}
return GenericData.get().compare(this, o, this.getSchema()) == 0;
}

@Override
public int hashCode() {
return super.hashCode();
}

@Override
Expand All @@ -384,7 +374,7 @@ public boolean hasNext() {

@Override
public T next() {
return (T) elements[position++];
return AbstractArray.this.get(position++);
}

@Override
Expand All @@ -394,6 +384,57 @@ public void remove() {
};
}

@Override
public void reverse() {
int left = 0;
int right = size - 1;

while (left < right) {
this.swap(left, right);

left++;
right--;
}
}

protected abstract void swap(int index1, int index2);
}

/** Default implementation of an array. */
@SuppressWarnings(value = "unchecked")
public static class Array<T> extends AbstractArray<T> {
private static final Object[] EMPTY = new Object[0];

private Object[] elements = EMPTY;

public Array(int capacity, Schema schema) {
super(schema);
if (capacity != 0)
elements = new Object[capacity];
}

public Array(Schema schema, Collection<T> c) {
super(schema);
if (c != null) {
elements = new Object[c.size()];
addAll(c);
}
}

@Override
public void clear() {
// Let GC do its work
Arrays.fill(elements, 0, size, null);
size = 0;
}

@Override
public void prune() {
if (size < elements.length) {
Arrays.fill(elements, size, elements.length, null);
}
}

@Override
public T get(int i) {
if (i >= size)
Expand Down Expand Up @@ -442,23 +483,10 @@ public T peek() {
}

@Override
public int compareTo(GenericArray<T> that) {
return GenericData.get().compare(this, that, this.getSchema());
}

@Override
public void reverse() {
int left = 0;
int right = elements.length - 1;

while (left < right) {
Object tmp = elements[left];
elements[left] = elements[right];
elements[right] = tmp;

left++;
right--;
}
protected void swap(final int index1, final int index2) {
Object tmp = elements[index1];
elements[index1] = elements[index2];
elements[index2] = tmp;
}
}

Expand Down Expand Up @@ -1499,8 +1527,24 @@ public Object newArray(Object old, int size, Schema schema) {
} else if (old instanceof Collection) {
((Collection<?>) old).clear();
return old;
} else
} else {
if (schema.getElementType().getType() == Type.INT) {
return new PrimitivesArrays.IntArray(size, schema);
}
if (schema.getElementType().getType() == Type.BOOLEAN) {
return new PrimitivesArrays.BooleanArray(size, schema);
}
if (schema.getElementType().getType() == Type.LONG) {
return new PrimitivesArrays.LongArray(size, schema);
}
if (schema.getElementType().getType() == Type.FLOAT) {
return new PrimitivesArrays.FloatArray(size, schema);
}
if (schema.getElementType().getType() == Type.DOUBLE) {
return new PrimitivesArrays.DoubleArray(size, schema);
}
return new GenericData.Array<Object>(size, schema);
}
}

/**
Expand Down
Loading