Merge "Use private object as a lock instead of synchronized method"

main
Tadashi G. Takaoka 2013-04-10 09:56:27 +00:00 committed by Android (Google) Code Review
commit c7d4158979
1 changed files with 155 additions and 133 deletions

View File

@ -34,175 +34,197 @@ public final class PointerTrackerQueue {
} }
private static final int INITIAL_CAPACITY = 10; private static final int INITIAL_CAPACITY = 10;
// Note: {@link #mExpandableArrayOfActivePointers} and {@link #mArraySize} are synchronized by
// {@link #mExpandableArrayOfActivePointers}
private final ArrayList<Element> mExpandableArrayOfActivePointers = private final ArrayList<Element> mExpandableArrayOfActivePointers =
CollectionUtils.newArrayList(INITIAL_CAPACITY); CollectionUtils.newArrayList(INITIAL_CAPACITY);
private int mArraySize = 0; private int mArraySize = 0;
public synchronized int size() { public int size() {
return mArraySize; synchronized (mExpandableArrayOfActivePointers) {
} return mArraySize;
public synchronized void add(final Element pointer) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
if (arraySize < expandableArray.size()) {
expandableArray.set(arraySize, pointer);
} else {
expandableArray.add(pointer);
} }
mArraySize = arraySize + 1;
} }
public synchronized void remove(final Element pointer) { public void add(final Element pointer) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; synchronized (mExpandableArrayOfActivePointers) {
final int arraySize = mArraySize; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
int newSize = 0; final int arraySize = mArraySize;
for (int index = 0; index < arraySize; index++) { if (arraySize < expandableArray.size()) {
final Element element = expandableArray.get(index); expandableArray.set(arraySize, pointer);
if (element == pointer) { } else {
expandableArray.add(pointer);
}
mArraySize = arraySize + 1;
}
}
public void remove(final Element pointer) {
synchronized (mExpandableArrayOfActivePointers) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
int newSize = 0;
for (int index = 0; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
if (newSize != index) {
Log.w(TAG, "Found duplicated element in remove: " + pointer);
}
continue; // Remove this element from the expandableArray.
}
if (newSize != index) { if (newSize != index) {
Log.w(TAG, "Found duplicated element in remove: " + pointer); // Shift this element toward the beginning of the expandableArray.
expandableArray.set(newSize, element);
} }
continue; // Remove this element from the expandableArray.
}
if (newSize != index) {
// Shift this element toward the beginning of the expandableArray.
expandableArray.set(newSize, element);
}
newSize++;
}
mArraySize = newSize;
}
public synchronized Element getOldestElement() {
return (mArraySize == 0) ? null : mExpandableArrayOfActivePointers.get(0);
}
public synchronized void releaseAllPointersOlderThan(final Element pointer,
final long eventTime) {
if (DEBUG) {
Log.d(TAG, "releaseAllPoniterOlderThan: " + pointer + " " + this);
}
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
int newSize, index;
for (newSize = index = 0; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
break; // Stop releasing elements.
}
if (!element.isModifier()) {
element.onPhantomUpEvent(eventTime);
continue; // Remove this element from the expandableArray.
}
if (newSize != index) {
// Shift this element toward the beginning of the expandableArray.
expandableArray.set(newSize, element);
}
newSize++;
}
// Shift rest of the expandableArray.
int count = 0;
for (; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
if (count > 0) {
Log.w(TAG, "Found duplicated element in releaseAllPointersOlderThan: "
+ pointer);
}
count++;
}
if (newSize != index) {
expandableArray.set(newSize, expandableArray.get(index));
newSize++; newSize++;
} }
mArraySize = newSize;
}
}
public Element getOldestElement() {
synchronized (mExpandableArrayOfActivePointers) {
return (mArraySize == 0) ? null : mExpandableArrayOfActivePointers.get(0);
}
}
public void releaseAllPointersOlderThan(final Element pointer, final long eventTime) {
synchronized (mExpandableArrayOfActivePointers) {
if (DEBUG) {
Log.d(TAG, "releaseAllPoniterOlderThan: " + pointer + " " + this);
}
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
int newSize, index;
for (newSize = index = 0; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
break; // Stop releasing elements.
}
if (!element.isModifier()) {
element.onPhantomUpEvent(eventTime);
continue; // Remove this element from the expandableArray.
}
if (newSize != index) {
// Shift this element toward the beginning of the expandableArray.
expandableArray.set(newSize, element);
}
newSize++;
}
// Shift rest of the expandableArray.
int count = 0;
for (; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
if (count > 0) {
Log.w(TAG, "Found duplicated element in releaseAllPointersOlderThan: "
+ pointer);
}
count++;
}
if (newSize != index) {
expandableArray.set(newSize, expandableArray.get(index));
newSize++;
}
}
mArraySize = newSize;
} }
mArraySize = newSize;
} }
public void releaseAllPointers(final long eventTime) { public void releaseAllPointers(final long eventTime) {
releaseAllPointersExcept(null, eventTime); releaseAllPointersExcept(null, eventTime);
} }
public synchronized void releaseAllPointersExcept(final Element pointer, public void releaseAllPointersExcept(final Element pointer, final long eventTime) {
final long eventTime) { synchronized (mExpandableArrayOfActivePointers) {
if (DEBUG) { if (DEBUG) {
if (pointer == null) { if (pointer == null) {
Log.d(TAG, "releaseAllPoniters: " + this); Log.d(TAG, "releaseAllPoniters: " + this);
} else { } else {
Log.d(TAG, "releaseAllPoniterExcept: " + pointer + " " + this); Log.d(TAG, "releaseAllPoniterExcept: " + pointer + " " + this);
}
}
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize;
int newSize = 0, count = 0;
for (int index = 0; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
if (count > 0) {
Log.w(TAG, "Found duplicated element in releaseAllPointersExcept: " + pointer);
} }
count++;
} else {
element.onPhantomUpEvent(eventTime);
continue; // Remove this element from the expandableArray.
} }
if (newSize != index) { final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
// Shift this element toward the beginning of the expandableArray. final int arraySize = mArraySize;
expandableArray.set(newSize, element); int newSize = 0, count = 0;
for (int index = 0; index < arraySize; index++) {
final Element element = expandableArray.get(index);
if (element == pointer) {
if (count > 0) {
Log.w(TAG, "Found duplicated element in releaseAllPointersExcept: "
+ pointer);
}
count++;
} else {
element.onPhantomUpEvent(eventTime);
continue; // Remove this element from the expandableArray.
}
if (newSize != index) {
// Shift this element toward the beginning of the expandableArray.
expandableArray.set(newSize, element);
}
newSize++;
} }
newSize++; mArraySize = newSize;
} }
mArraySize = newSize;
} }
public synchronized boolean hasModifierKeyOlderThan(final Element pointer) { public boolean hasModifierKeyOlderThan(final Element pointer) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; synchronized (mExpandableArrayOfActivePointers) {
final int arraySize = mArraySize; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
for (int index = 0; index < arraySize; index++) { final int arraySize = mArraySize;
final Element element = expandableArray.get(index); for (int index = 0; index < arraySize; index++) {
if (element == pointer) { final Element element = expandableArray.get(index);
return false; // Stop searching modifier key. if (element == pointer) {
} return false; // Stop searching modifier key.
if (element.isModifier()) { }
return true; if (element.isModifier()) {
return true;
}
} }
return false;
} }
return false;
} }
public synchronized boolean isAnyInSlidingKeyInput() { public boolean isAnyInSlidingKeyInput() {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; synchronized (mExpandableArrayOfActivePointers) {
final int arraySize = mArraySize; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
for (int index = 0; index < arraySize; index++) { final int arraySize = mArraySize;
final Element element = expandableArray.get(index); for (int index = 0; index < arraySize; index++) {
if (element.isInSlidingKeyInput()) { final Element element = expandableArray.get(index);
return true; if (element.isInSlidingKeyInput()) {
return true;
}
} }
return false;
} }
return false;
} }
public synchronized void cancelAllPointerTracker() { public void cancelAllPointerTracker() {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; synchronized (mExpandableArrayOfActivePointers) {
final int arraySize = mArraySize; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
for (int index = 0; index < arraySize; index++) { final int arraySize = mArraySize;
final Element element = expandableArray.get(index); for (int index = 0; index < arraySize; index++) {
element.cancelTracking(); final Element element = expandableArray.get(index);
element.cancelTracking();
}
} }
} }
@Override @Override
public synchronized String toString() { public String toString() {
final StringBuilder sb = new StringBuilder(); synchronized (mExpandableArrayOfActivePointers) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; final StringBuilder sb = new StringBuilder();
final int arraySize = mArraySize; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
for (int index = 0; index < arraySize; index++) { final int arraySize = mArraySize;
final Element element = expandableArray.get(index); for (int index = 0; index < arraySize; index++) {
if (sb.length() > 0) final Element element = expandableArray.get(index);
sb.append(" "); if (sb.length() > 0) {
sb.append(element.toString()); sb.append(" ");
}
sb.append(element.toString());
}
return "[" + sb.toString() + "]";
} }
return "[" + sb.toString() + "]";
} }
} }