Java实现简易的PythonList

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值