Use private object as a lock instead of synchronized method

Change-Id: I0b9a935d8c97ddfb18ab7950a9d6bf542863072a
main
Tadashi G. Takaoka 2013-04-10 18:34:01 +09:00
parent 99b93d17d5
commit 951ea3c06d
1 changed files with 155 additions and 133 deletions

View File

@ -34,15 +34,20 @@ 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() {
synchronized (mExpandableArrayOfActivePointers) {
return mArraySize; return mArraySize;
} }
}
public synchronized void add(final Element pointer) { public void add(final Element pointer) {
synchronized (mExpandableArrayOfActivePointers) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize; final int arraySize = mArraySize;
if (arraySize < expandableArray.size()) { if (arraySize < expandableArray.size()) {
@ -52,8 +57,10 @@ public final class PointerTrackerQueue {
} }
mArraySize = arraySize + 1; mArraySize = arraySize + 1;
} }
}
public synchronized void remove(final Element pointer) { public void remove(final Element pointer) {
synchronized (mExpandableArrayOfActivePointers) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize; final int arraySize = mArraySize;
int newSize = 0; int newSize = 0;
@ -73,13 +80,16 @@ public final class PointerTrackerQueue {
} }
mArraySize = newSize; mArraySize = newSize;
} }
public synchronized Element getOldestElement() {
return (mArraySize == 0) ? null : mExpandableArrayOfActivePointers.get(0);
} }
public synchronized void releaseAllPointersOlderThan(final Element pointer, public Element getOldestElement() {
final long eventTime) { synchronized (mExpandableArrayOfActivePointers) {
return (mArraySize == 0) ? null : mExpandableArrayOfActivePointers.get(0);
}
}
public void releaseAllPointersOlderThan(final Element pointer, final long eventTime) {
synchronized (mExpandableArrayOfActivePointers) {
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "releaseAllPoniterOlderThan: " + pointer + " " + this); Log.d(TAG, "releaseAllPoniterOlderThan: " + pointer + " " + this);
} }
@ -119,13 +129,14 @@ public final class PointerTrackerQueue {
} }
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);
@ -140,7 +151,8 @@ public final class PointerTrackerQueue {
final Element element = expandableArray.get(index); final Element element = expandableArray.get(index);
if (element == pointer) { if (element == pointer) {
if (count > 0) { if (count > 0) {
Log.w(TAG, "Found duplicated element in releaseAllPointersExcept: " + pointer); Log.w(TAG, "Found duplicated element in releaseAllPointersExcept: "
+ pointer);
} }
count++; count++;
} else { } else {
@ -155,8 +167,10 @@ public final class PointerTrackerQueue {
} }
mArraySize = newSize; mArraySize = newSize;
} }
}
public synchronized boolean hasModifierKeyOlderThan(final Element pointer) { public boolean hasModifierKeyOlderThan(final Element pointer) {
synchronized (mExpandableArrayOfActivePointers) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize; final int arraySize = mArraySize;
for (int index = 0; index < arraySize; index++) { for (int index = 0; index < arraySize; index++) {
@ -170,8 +184,10 @@ public final class PointerTrackerQueue {
} }
return false; return false;
} }
}
public synchronized boolean isAnyInSlidingKeyInput() { public boolean isAnyInSlidingKeyInput() {
synchronized (mExpandableArrayOfActivePointers) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize; final int arraySize = mArraySize;
for (int index = 0; index < arraySize; index++) { for (int index = 0; index < arraySize; index++) {
@ -182,8 +198,10 @@ public final class PointerTrackerQueue {
} }
return false; return false;
} }
}
public synchronized void cancelAllPointerTracker() { public void cancelAllPointerTracker() {
synchronized (mExpandableArrayOfActivePointers) {
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize; final int arraySize = mArraySize;
for (int index = 0; index < arraySize; index++) { for (int index = 0; index < arraySize; index++) {
@ -191,18 +209,22 @@ public final class PointerTrackerQueue {
element.cancelTracking(); element.cancelTracking();
} }
} }
}
@Override @Override
public synchronized String toString() { public String toString() {
synchronized (mExpandableArrayOfActivePointers) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers; final ArrayList<Element> expandableArray = mExpandableArrayOfActivePointers;
final int arraySize = mArraySize; final int arraySize = mArraySize;
for (int index = 0; index < arraySize; index++) { for (int index = 0; index < arraySize; index++) {
final Element element = expandableArray.get(index); final Element element = expandableArray.get(index);
if (sb.length() > 0) if (sb.length() > 0) {
sb.append(" "); sb.append(" ");
}
sb.append(element.toString()); sb.append(element.toString());
} }
return "[" + sb.toString() + "]"; return "[" + sb.toString() + "]";
} }
}
} }