Skip to content
Open
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
56 changes: 55 additions & 1 deletion src/main/java/com/eclipsesource/v8/utils/MemoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
package com.eclipsesource.v8.utils;

import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import com.eclipsesource.v8.ReferenceHandler;
import com.eclipsesource.v8.V8;
Expand All @@ -30,7 +33,7 @@ public class MemoryManager {

private MemoryManagerReferenceHandler memoryManagerReferenceHandler;
private V8 v8;
private ArrayList<V8Value> references = new ArrayList<V8Value>();
private SimpleArrayList<V8Value> references = new SimpleArrayList<V8Value>();
private boolean releasing = false;
private boolean released = false;

Expand Down Expand Up @@ -135,4 +138,55 @@ public void v8HandleDisposed(final V8Value object) {
}
}

/**
* Custom implementation of ArrayList that overrides equality to instead of compare element by element would only</BR>
* compare references and ranges.
* @param <E> – the type of elements in this list
*/
private static class SimpleArrayList<E> extends ArrayList<E> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why override equals here when it just mirrors ArrayList.equals? What benefit does this custom impl add?


@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}

if (!(o instanceof List)) {
return false;
}

final int expectedModCount = modCount;
// ArrayList can be subclassed and given arbitrary behavior, but we can
// still deal with the common case where o is ArrayList precisely
boolean equal = equalsRange((List<?>) o, 0, this.size());

checkForComodification(expectedModCount);
return equal;
}

boolean equalsRange(List<?> other, int from, int to) {
final Object[] es = this.toArray();
if (to > es.length) {
throw new ConcurrentModificationException();
}
Iterator<?> oit = other.iterator();
for (; from < to; from++) {
if (!oit.hasNext() || !Objects.equals(es[from], oit.next())) {
return false;
}
}
return !oit.hasNext();
}

private void checkForComodification(final int expectedModCount) {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}

public int size() {
return super.size();
}
}

}