FastListソース分析
12010 ワード
FastListは、HikariCPデータベース接続プールで使用されるカスタムリストです.実は特に何もありません.主にrange checkを消してデータを取るのがもっと速いです.
(1)クラス定義
ArrayListから引き継ぎ、
(2)メンバー変数
clazz:listに格納されているデータ型class.
ElementData:データを格納するソース配列.
size:リストの現在のサイズ
(3)コンストラクタ
データ型と初期の配列容量を渡します.デフォルトの容量サイズは32で、sizeとは異なります.
(4)add要素の追加
addメソッドを書き直しました.
配列に要素を挿入し、sizeを1増やし、配列が境界を越えている場合は、元の容量の2倍に拡張します.最後に、拡張された配列をソース配列に割り当てます.
(5)get指定された位置の要素を取得する
getメソッドを書き直しました.
配列のindex下付きを直接取得し,境界を越えて異常を直接放出する.
(6)最後の要素を除去する
size-1の位置要素を取り出し、空にし、sizeを1に減らします.
(7)指定した要素を除去する
配列の最后の面から前を探して、最初の同じ要素に出会った时に直接削除して、copyの元の配列、sizeは1を減らします.注意するのは、判断が等しいことが最も基本的な==であることです.
(8)すべての要素をクリア
最初から最後まで(size)を空に設定し、size=0にします.
(9)大きさを返し、空かどうかを判断し、多く言わない
(10)最後にソースをそのまま貼り付けましょう
(1)クラス定義
public final class FastList extends ArrayList
ArrayListから引き継ぎ、
(2)メンバー変数
private final Class> clazz;
private T[] elementData;
private int size;
clazz:listに格納されているデータ型class.
ElementData:データを格納するソース配列.
size:リストの現在のサイズ
(3)コンストラクタ
@SuppressWarnings("unchecked")
public FastList(Class> clazz)
{
this.elementData = (T[]) Array.newInstance(clazz, 32);
this.clazz = clazz;
}
@SuppressWarnings("unchecked")
public FastList(Class> clazz, int capacity)
{
this.elementData = (T[]) Array.newInstance(clazz, capacity);
this.clazz = clazz;
}
データ型と初期の配列容量を渡します.デフォルトの容量サイズは32で、sizeとは異なります.
(4)add要素の追加
@Override
public boolean add(T element)
{
try {
elementData[size++] = element;
}
catch (ArrayIndexOutOfBoundsException e) {
// overflow-conscious code
final int oldCapacity = elementData.length;
final int newCapacity = oldCapacity << 1;
@SuppressWarnings("unchecked")
final T[] newElementData = (T[]) Array.newInstance(clazz, newCapacity);
System.arraycopy(elementData, 0, newElementData, 0, oldCapacity);
newElementData[size - 1] = element;
elementData = newElementData;
}
return true;
}
addメソッドを書き直しました.
配列に要素を挿入し、sizeを1増やし、配列が境界を越えている場合は、元の容量の2倍に拡張します.最後に、拡張された配列をソース配列に割り当てます.
(5)get指定された位置の要素を取得する
@Override
public T get(int index)
{
return elementData[index];
}
getメソッドを書き直しました.
配列のindex下付きを直接取得し,境界を越えて異常を直接放出する.
(6)最後の要素を除去する
public T removeLast()
{
T element = elementData[--size];
elementData[size] = null;
return element;
}
size-1の位置要素を取り出し、空にし、sizeを1に減らします.
(7)指定した要素を除去する
@Override
public boolean remove(Object element)
{
for (int index = size - 1; index >= 0; index--) {
if (element == elementData[index]) {
final int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
}
elementData[--size] = null;
return true;
}
}
return false;
}
配列の最后の面から前を探して、最初の同じ要素に出会った时に直接削除して、copyの元の配列、sizeは1を減らします.注意するのは、判断が等しいことが最も基本的な==であることです.
(8)すべての要素をクリア
@Override
public void clear()
{
for (int i = 0; i < size; i++) {
elementData[i] = null;
}
size = 0;
}
最初から最後まで(size)を空に設定し、size=0にします.
(9)大きさを返し、空かどうかを判断し、多く言わない
@Override
public int size()
{
return size;
}
@Override
public boolean isEmpty()
{
return size == 0;
}
(10)最後にソースをそのまま貼り付けましょう
/*
* Copyright (C) 2013, 2014 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaxxer.hikari.util;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
/**
* Fast list without range checking.
*
* @author Brett Wooldridge
*/
public final class FastList extends ArrayList
{
private static final long serialVersionUID = -4598088075242913858L;
private final Class> clazz;
private T[] elementData;
private int size;
/**
* Construct a FastList with a default size of 32.
* @param clazz the Class stored in the collection
*/
@SuppressWarnings("unchecked")
public FastList(Class> clazz)
{
this.elementData = (T[]) Array.newInstance(clazz, 32);
this.clazz = clazz;
}
/**
* Construct a FastList with a specified size.
* @param clazz the Class stored in the collection
* @param capacity the initial size of the FastList
*/
@SuppressWarnings("unchecked")
public FastList(Class> clazz, int capacity)
{
this.elementData = (T[]) Array.newInstance(clazz, capacity);
this.clazz = clazz;
}
/**
* Add an element to the tail of the FastList.
*
* @param element the element to add
*/
@Override
public boolean add(T element)
{
try {
elementData[size++] = element;
}
catch (ArrayIndexOutOfBoundsException e) {
// overflow-conscious code
final int oldCapacity = elementData.length;
final int newCapacity = oldCapacity << 1;
@SuppressWarnings("unchecked")
final T[] newElementData = (T[]) Array.newInstance(clazz, newCapacity);
System.arraycopy(elementData, 0, newElementData, 0, oldCapacity);
newElementData[size - 1] = element;
elementData = newElementData;
}
return true;
}
/**
* Get the element at the specified index.
*
* @param index the index of the element to get
* @return the element, or ArrayIndexOutOfBounds is thrown if the index is invalid
*/
@Override
public T get(int index)
{
return elementData[index];
}
/**
* Remove the last element from the list. No bound check is performed, so if this
* method is called on an empty list and ArrayIndexOutOfBounds exception will be
* thrown.
*
* @return the last element of the list
*/
public T removeLast()
{
T element = elementData[--size];
elementData[size] = null;
return element;
}
/**
* This remove method is most efficient when the element being removed
* is the last element. Equality is identity based, not equals() based.
* Only the first matching element is removed.
*
* @param element the element to remove
*/
@Override
public boolean remove(Object element)
{
for (int index = size - 1; index >= 0; index--) {
if (element == elementData[index]) {
final int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
}
elementData[--size] = null;
return true;
}
}
return false;
}
/**
* Clear the FastList.
*/
@Override
public void clear()
{
for (int i = 0; i < size; i++) {
elementData[i] = null;
}
size = 0;
}
/**
* Get the current number of elements in the FastList.
*
* @return the number of current elements
*/
@Override
public int size()
{
return size;
}
/** {@inheritDoc} */
@Override
public boolean isEmpty()
{
return size == 0;
}
/** {@inheritDoc} */
@Override
public T set(int index, T element)
{
T old = elementData[index];
elementData[index] = element;
return old;
}
/** {@inheritDoc} */
@Override
public T remove(int index)
{
if (size == 0) {
return null;
}
final T old = elementData[index];
final int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
}
elementData[--size] = null;
return old;
}
/** {@inheritDoc} */
@Override
public boolean contains(Object o)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public Iterator iterator()
{
return new Iterator() {
private int index;
@Override
public boolean hasNext()
{
return index < size;
}
@Override
public T next()
{
if (index < size) {
return elementData[index++];
}
throw new NoSuchElementException("No more elements in FastList");
}
};
}
/** {@inheritDoc} */
@Override
public Object[] toArray()
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public E[] toArray(E[] a)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public boolean containsAll(Collection> c)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public boolean addAll(Collection extends T> c)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public boolean addAll(int index, Collection extends T> c)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public boolean removeAll(Collection> c)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public boolean retainAll(Collection> c)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public void add(int index, T element)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public int indexOf(Object o)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public int lastIndexOf(Object o)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public ListIterator listIterator()
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public ListIterator listIterator(int index)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public List subList(int fromIndex, int toIndex)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public void trimToSize()
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public void ensureCapacity(int minCapacity)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
public Object clone()
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
@Override
protected void removeRange(int fromIndex, int toIndex)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
public void forEach(Consumer super T> action)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
public Spliterator spliterator()
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
public boolean removeIf(Predicate super T> filter)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
public void replaceAll(UnaryOperator operator)
{
throw new UnsupportedOperationException();
}
/** {@inheritDoc} */
public void sort(Comparator super T> c)
{
throw new UnsupportedOperationException();
}
}