⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intbuffer.java

📁 java 的源代码
💻 JAVA
字号:
package com.reddragon2046.base.utilities.data.adapters;

import com.reddragon2046.base.utilities.data.*;
import java.util.Iterator;
import java.util.NoSuchElementException;

// Referenced classes of package com.reddragon2046.base.utilities.data.adapters:
//            AbstractArrayAdapter, IntArray, IntIterator, AbstractArrayIterator,
//            Algorithms

public class IntBuffer extends AbstractArrayAdapter
{

    public IntBuffer()
    {
        clear();
    }

    public IntBuffer(int size)
    {
        if(size < 0)
        {
            throw new IllegalArgumentException("Attempt to create an IntBuffer with a negative size");
        } else
        {
            length = size;
            storage = new int[length];
            return;
        }
    }

    public IntBuffer(int size, int object)
    {
        this(size);
        for(int i = 0; i < length; i++)
            storage[i] = object;

    }

    public IntBuffer(int array[])
    {
        this(array, true);
    }

    IntBuffer(int array[], boolean copyBuffer)
    {
        synchronized(array)
        {
            length = array.length;
            if(copyBuffer)
            {
                storage = new int[length];
                System.arraycopy(array, 0, storage, 0, length);
            } else
            {
                storage = array;
            }
        }
    }

    public IntBuffer(IntBuffer array)
    {
        this(array.storage, true);
        length = array.length;
    }

    public synchronized Object clone()
    {
        return new IntBuffer(this);
    }

    public boolean equals(Object object)
    {
        return (object instanceof IntBuffer) && equals((IntBuffer)object) || (object instanceof IntArray) && equals((IntArray)object);
    }

    public synchronized boolean equals(IntBuffer buffer)
    {
        return IntArray.sliceEquals(storage, length, buffer.storage, buffer.length);
    }

    public boolean equals(IntArray array)
    {
        return equals(array.m_array);
    }

    public synchronized boolean equals(int array[])
    {
        return IntArray.sliceEquals(storage, length, array, array.length);
    }

    public synchronized String toString()
    {
        return Algorithms.Printing.toString(this, "IntBuffer");
    }

    public synchronized void copy(IntBuffer buffer)
    {
        if(this == buffer)
            return;
        synchronized(buffer)
        {
            if(buffer.length > storage.length)
            {
                storage = buffer.get();
            } else
            {
                System.arraycopy(buffer.storage, 0, storage, 0, buffer.length);
                for(int i = buffer.length; i < length; i++)
                    storage[i] = 0;

            }
            length = buffer.length;
        }
    }

    public synchronized void copyTo(int array[])
    {
        synchronized(array)
        {
            System.arraycopy(storage, 0, array, 0, Math.min(length, array.length));
        }
    }

    public synchronized int[] get()
    {
        int data[] = new int[length];
        copyTo(data);
        return data;
    }

    public synchronized int hashCode()
    {
        return Algorithms.Hashing.orderedHash(begin(), length);
    }

    public boolean isEmpty()
    {
        return size() == 0;
    }

    public int size()
    {
        return length;
    }

    public int capacity()
    {
        return storage.length;
    }

    public Object get(int index)
    {
        return new Integer(intAt(index));
    }

    public synchronized int intAt(int index)
    {
        AbstractArrayAdapter.checkIndex(index, length);
        return storage[index];
    }

    public synchronized void put(int index, Object object)
    {
        put(index, asInt(object));
    }

    public synchronized void put(int index, int object)
    {
        AbstractArrayAdapter.checkIndex(index, length);
        storage[index] = object;
    }

    public synchronized void clear()
    {
        storage = new int[10];
        length = 0;
    }

    public synchronized Object remove(int index)
    {
        AbstractArrayAdapter.checkIndex(index, length);
        Object retval = new Integer(storage[index]);
        System.arraycopy(storage, index + 1, storage, index, length - index - 1);
        storage[--length] = 0;
        return retval;
    }

    public synchronized int remove(int first, int last)
    {
        if(last < first)
            return 0;
        AbstractArrayAdapter.checkRange(first, last, length);
        int amount = (last - first) + 1;
        System.arraycopy(storage, last + 1, storage, first, length - last - 1);
        for(int i = length - amount; i < length; i++)
            storage[i] = 0;

        length -= amount;
        return amount;
    }

    public synchronized Object popBack()
    {
        if(length == 0)
        {
            throw new NoSuchElementException("IntBuffer is empty");
        } else
        {
            Object r = new Integer(storage[--length]);
            storage[length] = 0;
            return r;
        }
    }

    public synchronized boolean add(Object object)
    {
        add(asInt(object));
        return true;
    }

    public synchronized void add(int object)
    {
        if(length == storage.length)
        {
            int tmp[] = getNextStorage(1);
            copyTo(tmp);
            storage = tmp;
        }
        storage[length++] = object;
    }

    public void pushBack(Object object)
    {
        add(asInt(object));
    }

    public void pushBack(int object)
    {
        add(object);
    }

    public IntIterator insert(IntIterator pos, Object object)
    {
        return insert(pos, asInt(object));
    }

    public IntIterator insert(IntIterator pos, int object)
    {
        int index = pos.getInternalIndex();
        insert(index, object);
        return new IntIterator(this, index);
    }

    public void insert(int index, Object object)
    {
        insert(index, asInt(object));
    }

    public synchronized void insert(int index, int object)
    {
        AbstractArrayAdapter.checkIndex(index, length + 1);
        if(length != storage.length)
        {
            if(index != length)
                System.arraycopy(storage, index, storage, index + 1, length - index);
        } else
        {
            int tmp[] = getNextStorage(1);
            System.arraycopy(storage, 0, tmp, 0, index);
            System.arraycopy(storage, index, tmp, index + 1, length - index);
            storage = tmp;
        }
        storage[index] = object;
        length++;
    }

    public void insert(IntIterator pos, int n, Object object)
    {
        insert(pos, n, asInt(object));
    }

    public void insert(IntIterator pos, int n, int object)
    {
        insert(pos.getInternalIndex(), n, object);
    }

    public void insert(int index, int n, Object object)
    {
        insert(index, n, asInt(object));
    }

    public synchronized void insert(int index, int n, int object)
    {
        if(n < 0)
            throw new IllegalArgumentException("Attempt to insert a negative number of objects.");
        if(n == 0)
            return;
        AbstractArrayAdapter.checkIndex(index, length + 1);
        if(storage.length - length >= n)
        {
            System.arraycopy(storage, index, storage, index + n, length - index);
        } else
        {
            int tmp[] = getNextStorage(n);
            System.arraycopy(storage, 0, tmp, 0, index);
            System.arraycopy(storage, index, tmp, index + n, length - index);
            storage = tmp;
        }
        for(int i = index; i < index + n; i++)
            storage[i] = object;

        length += n;
    }

    public void insert(IntIterator pos, ForwardIterator first, ForwardIterator last)
    {
        insert(pos.getInternalIndex(), first, last);
    }

    public synchronized void insert(int index, ForwardIterator first, ForwardIterator last)
    {
        int n = first.distance(last);
        if(n == 0)
            return;
        ForwardIterator firstx = (ForwardIterator)first.clone();
        if(storage.length - length >= n)
        {
            System.arraycopy(storage, index, storage, index + n, length - index);
        } else
        {
            int tmp[] = getNextStorage(n);
            System.arraycopy(storage, 0, tmp, 0, index);
            System.arraycopy(storage, index, tmp, index + n, length - index);
            storage = tmp;
        }
        length += n;
        for(int i = index; i < index + n; i++)
            put(i, firstx.next());

    }

    public synchronized void swap(IntBuffer array)
    {
        synchronized(array)
        {
            int oldLength = length;
            int oldStorage[] = storage;
            length = array.length;
            storage = array.storage;
            array.length = oldLength;
            array.storage = oldStorage;
        }
    }

    public synchronized BidirectionalIterator begin()
    {
        return new IntIterator(this, 0);
    }

    public synchronized BidirectionalIterator end()
    {
        return new IntIterator(this, length);
    }

    public synchronized void trimToSize()
    {
        if(length < storage.length)
            storage = get();
    }

    public synchronized void ensureCapacity(int n)
    {
        if(n < 0)
            throw new IllegalArgumentException("Attempt to reserve a negative size.");
        if(storage.length < n)
        {
            int tmp[] = new int[n];
            if(length > 0)
                System.arraycopy(storage, 0, tmp, 0, length);
            storage = tmp;
        }
    }

    public synchronized Object popFront()
    {
        if(length == 0)
        {
            throw new NoSuchElementException("IntBuffer is empty");
        } else
        {
            Object result = new Integer(storage[0]);
            remove(0);
            return result;
        }
    }

    public void pushFront(Object object)
    {
        insert(0, object);
    }

    public void pushFront(int object)
    {
        insert(0, object);
    }

    public synchronized int remove(Object object, int count)
    {
        int tmp = asInt(object);
        int removed = 0;
        while(count > 0)
        {
            int i = indexOf(tmp);
            if(i < 0)
                break;
            count--;
            removed++;
            remove(i);
        }
        return removed;
    }

    public synchronized int remove(int first, int last, Object object)
    {
        asInt(object);
        if(last < first)
        {
            return 0;
        } else
        {
            AbstractArrayAdapter.checkRange(first, last, length);
            IntIterator firstx = new IntIterator(this, first);
            IntIterator lastx = new IntIterator(this, last + 1);
            IntIterator finish = (IntIterator)Algorithms.Removing.remove(firstx, lastx, object);
            return remove(finish.getInternalIndex(), last);
        }
    }

    public int replace(Object oldValue, Object newValue)
    {
        return replace(asInt(oldValue), asInt(newValue));
    }

    public int replace(int oldValue, int newValue)
    {
        return replace(0, length, oldValue, newValue);
    }

    public int replace(int first, int last, Object oldValue, Object newValue)
    {
        return replace(first, last, asInt(oldValue), asInt(newValue));
    }

    public synchronized int replace(int first, int last, int oldValue, int newValue)
    {
        AbstractArrayAdapter.checkRange(first, last - 1, length);
        int n = 0;
        for(; first < last; first++)
            if(storage[first] == oldValue)
            {
                storage[first] = newValue;
                n++;
            }

        return n;
    }

    public int count(Object object)
    {
        return count(asInt(object));
    }

    public int count(int object)
    {
        return count(0, length - 1, object);
    }

    public int count(int first, int last, Object object)
    {
        return count(first, last, asInt(object));
    }

    public synchronized int count(int first, int last, int object)
    {
        AbstractArrayAdapter.checkRange(first, last, length);
        int n = 0;
        for(; first < last; first++)
            if(storage[first] == object)
                n++;

        return n;
    }

    public int indexOf(Object object)
    {
        return indexOf(asInt(object));
    }

    public int indexOf(int object)
    {
        return indexOf(0, length - 1, object);
    }

    public int indexOf(int first, int last, Object object)
    {
        return indexOf(first, last, asInt(object));
    }

    public synchronized int indexOf(int first, int last, int object)
    {
        if(last < first)
            return -1;
        AbstractArrayAdapter.checkRange(first, last, length);
        for(; first <= last; first++)
            if(storage[first] == object)
                return first;

        return -1;
    }

    public synchronized void setSize(int newSize)
    {
        if(newSize < 0)
            throw new IllegalArgumentException("Attempt to become a negative size.");
        if(length > newSize)
            remove(newSize, length - 1);
        else
        if(length < newSize)
            insert(length, newSize - length, 0);
    }

    public boolean contains(Object object)
    {
        return contains(asInt(object));
    }

    public boolean contains(int object)
    {
        return indexOf(object) != -1;
    }

    private int[] getNextStorage(int n)
    {
        int newSize = Math.max(AbstractArrayAdapter.getNextSize(length), length + n);
        int tmp[] = new int[newSize];
        return tmp;
    }

    static int asInt(Object object)
    {
        return ((Number)object).intValue();
    }

    static final int defaultValue = 0;
    int storage[];
    int length;
    static final long serialVersionUID = 0x2059c6ebbbb5278L;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -