import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Objects;
import java.util.function.Consumer;
class PythonList implements Cloneable {
private ArrayList<Object> innerList;
private final static Class __klass; /* ArrayList */
private int size;
static {
__klass = ArrayList.class;
}
public PythonList() {
this(0);
}
public PythonList(int cap) {
if (cap < 0) throw new NegativeArraySizeException();
innerList = new ArrayList<>(cap);
size = 0;
}
private void CheckNull(Object e) {
if (e == null) throw new NullPointerException();
}
private boolean addProxyMethod(Object e) {
try {
Method __add = __klass.getMethod("add", Object.class);
__add.invoke(innerList, e);
++size;
return true;
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException noSuchMethodException) {
noSuchMethodException.printStackTrace();
}
return false;
}
private boolean addProxyMethod(int index, Object e) throws IndexOutOfBoundsException {
rangeCheckForAdd(index);
innerList.add(index, e);
++size;
return true;
}
private void rangeCheckForAdd(int index) {
if (index < 0 || index > innerList.size()) throw new IndexOutOfBoundsException();
}
private boolean removeProxyMethod(int index) {
innerList.remove(index);
--size;
return true;
}
public boolean add(Object ele) {
try {
CheckNull(ele);
} catch (NullPointerException e) {
e.printStackTrace();
return false;
}
return addProxyMethod(ele);
}
public boolean add(int index, Object ele) {
try {
CheckNull(ele);
} catch (NullPointerException e) {
e.printStackTrace();
return false;
}
return doAdd(index, ele);
}
public boolean remove(int index) {
CheckNull(index);
if (index < 0 || index >= innerList.size()) {
return false;
}
return doRemove(index);
}
public void forEach(Consumer<Object> action) {
Objects.requireNonNull(action);
@SuppressWarnings("unchecked") final int size = this.size;
for (int i = 0; i < size; i++) {
action.accept(innerList.get(i));
}
}
private boolean doAdd(int index, Object ele) {
return addProxyMethod(index, ele);
}
private boolean doRemove(int index) {
return removeProxyMethod(index);
}
public PythonList clone() {
PythonList newList = new PythonList();
newList.innerList = (ArrayList<Object>) this.innerList.clone();
newList.size = this.size;
return newList;
}
public void clear() {
innerList.clear();
size = 0;
}
@Override
public String toString() {
if(size == 0) {
return String.format("{ele=[], size=%d, type=%s}",size,this.getClass().getName());
}
StringBuilder innerListMsg = new StringBuilder();
final int size = this.size;
for (int i = 0; i < size; ++i) {
innerListMsg.append(innerList.get(i).toString());
innerListMsg.append(",");
}
final int length = innerListMsg.length();
innerListMsg.delete(length - 1, length);
return String.format("{ele=[%s], size=%d, type=%s}",innerListMsg.toString(),size,this.getClass().getName());
}
}
public class testPythonList {
@Test
public void testAddUnit() {
PythonList list = new PythonList();
list.add(1);
list.add("ab");
list.add(12.3111);
list.add(2, "a");
list.forEach(x -> System.out.print(x + " "));
System.out.println(list);
list.remove(2);
System.out.println(list);
PythonList list2 = list.clone();
list2.add(2,"graker");
System.out.println(list);
System.out.println(list2);
list.clear();
System.out.println(list);
System.exit(0);
}
}
Java实现简易的PythonList
最新推荐文章于 2022-07-23 11:41:36 发布