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

📄 shortbuffer.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, ShortArray, ShortIterator, AbstractArrayIterator,
//            Algorithms

public class ShortBuffer extends AbstractArrayAdapter
{

    public ShortBuffer()
    {
        clear();
    }

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

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

    }

    public ShortBuffer(short array[])
    {
        this(array, true);
    }

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

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

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

    public boolean equals(Object object)
    {
        return (object instanceof ShortBuffer) && equals((ShortBuffer)object) || (object instanceof ShortArray) && equals((ShortArray)object);
    }

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

    public boolean equals(ShortArray array)
    {
        return equals(array.array);
    }

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

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

    public synchronized void copy(ShortBuffer 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(short array[])
    {
        synchronized(array)
        {
            System.arraycopy(storage, 0, array, 0, Math.min(length, array.length));
        }
    }

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

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

    public int size()
    {
        return length;
    }

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

    public int maxSize()
    {
        return 0x7fffffff;
    }

    public Object get(int index)
    {
        return new Short(shortAt(index));
    }

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

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

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

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

    public synchronized Object remove(int index)
    {
        AbstractArrayAdapter.checkIndex(index, length);
        Object retval = new Short(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("ShortBuffer is empty");
        } else
        {
            Object r = new Short(storage[--length]);
            storage[length] = 0;
            return r;
        }
    }

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

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

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

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

    public ShortIterator insert(ShortIterator pos, Object object)
    {
        return insert(pos, asShort(object));
    }

    public ShortIterator insert(ShortIterator pos, short object)
    {
        insert(pos.getInternalIndex(), object);
        return new ShortIterator(this, pos.getInternalIndex());
    }

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

    public synchronized void insert(int index, short object)
    {
        AbstractArrayAdapter.checkIndex(index, length + 1);
        if(length != storage.length)
        {
            if(index != length)
                System.arraycopy(storage, index, storage, index + 1, length - index);
        } else
        {
            short 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(ShortIterator pos, int n, Object object)
    {
        insert(pos, n, asShort(object));
    }

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

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

    public synchronized void insert(int index, int n, short 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
        {
            short 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(ShortIterator 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
        {
            short 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(ShortBuffer array)
    {
        synchronized(array)
        {
            int oldLength = length;
            short oldStorage[] = storage;
            length = array.length;
            storage = array.storage;
            array.length = oldLength;
            array.storage = oldStorage;
        }
    }

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

    public synchronized BidirectionalIterator end()
    {
        return new ShortIterator(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)
        {
            short tmp[] = new short[n];
            if(length > 0)
                System.arraycopy(storage, 0, tmp, 0, length);
            storage = tmp;
        }
    }

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

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

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

    public synchronized int remove(Object object, int count)
    {
        short tmp = asShort(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)
    {
        asShort(object);
        if(last < first)
        {
            return 0;
        } else
        {
            AbstractArrayAdapter.checkRange(first, last, length);
            ShortIterator firstx = new ShortIterator(this, first);
            ShortIterator lastx = new ShortIterator(this, last + 1);
            ShortIterator finish = (ShortIterator)Algorithms.Removing.remove(firstx, lastx, object);
            return remove(finish.getInternalIndex(), last);
        }
    }

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

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

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

    public synchronized int replace(int first, int last, short oldValue, short 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(asShort(object));
    }

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

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

    public synchronized int count(int first, int last, short 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(asShort(object));
    }

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

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

    public synchronized int indexOf(int first, int last, short 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, (short)0);
    }

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

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

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

    static short asShort(Object object)
    {
        return ((Number)object).shortValue();
    }

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

⌨️ 快捷键说明

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