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