Skip to content

Commit 62f8e5b

Browse files
committed
refactor: Refactor MarkerModifier to use a withLock method
This commit refactors the `MarkerModifier` class to use a `withLock` method. This reduces code repetition and improves readability.
1 parent 201db21 commit 62f8e5b

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

library/src/main/java/com/google/maps/android/clustering/view/ClusterRendererMultipleItems.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public void setAnimationType(AnimationType type) {
117117
};
118118
}
119119

120+
/**
121+
* Sets the interpolator for the animation.
122+
*
123+
* @param interpolator the interpolator to use for the animation.
124+
*/
120125
public void setAnimationInterpolator(TimeInterpolator interpolator) {
121126
animationInterp = interpolator;
122127
}
@@ -665,6 +670,15 @@ private class MarkerModifier extends Handler implements MessageQueue.IdleHandler
665670
*/
666671
private boolean mListenerAdded;
667672

673+
private void withLock(Runnable runnable) {
674+
lock.lock();
675+
try {
676+
runnable.run();
677+
} finally {
678+
lock.unlock();
679+
}
680+
}
681+
668682
private MarkerModifier() {
669683
super(Looper.getMainLooper());
670684
}
@@ -675,17 +689,14 @@ private MarkerModifier() {
675689
* @param priority whether this operation should have priority.
676690
*/
677691
public void add(boolean priority, CreateMarkerTask c) {
678-
lock.lock();
679-
try {
692+
withLock(() -> {
680693
sendEmptyMessage(BLANK);
681694
if (priority) {
682695
mOnScreenCreateMarkerTasks.add(c);
683696
} else {
684697
mCreateMarkerTasks.add(c);
685698
}
686-
} finally {
687-
lock.unlock();
688-
}
699+
});
689700
}
690701

691702
/**
@@ -695,17 +706,14 @@ public void add(boolean priority, CreateMarkerTask c) {
695706
* @param m the markerWithPosition to remove.
696707
*/
697708
public void remove(boolean priority, Marker m) {
698-
lock.lock();
699-
try {
709+
withLock(() -> {
700710
sendEmptyMessage(BLANK);
701711
if (priority) {
702712
mOnScreenRemoveMarkerTasks.add(m);
703713
} else {
704714
mRemoveMarkerTasks.add(m);
705715
}
706-
} finally {
707-
lock.unlock();
708-
}
716+
});
709717
}
710718

711719
/**
@@ -716,8 +724,7 @@ public void remove(boolean priority, Marker m) {
716724
* @param to the position to animate to.
717725
*/
718726
public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {
719-
lock.lock();
720-
try {
727+
withLock(() -> {
721728
AnimationTask task = new AnimationTask(marker, from, to, lock);
722729

723730
for (AnimationTask existingTask : ongoingAnimations) {
@@ -729,9 +736,7 @@ public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {
729736

730737
mAnimationTasks.add(task);
731738
ongoingAnimations.add(task);
732-
} finally {
733-
lock.unlock();
734-
}
739+
});
735740
}
736741

737742
/**
@@ -743,8 +748,7 @@ public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {
743748
* @param to the position to animate to.
744749
*/
745750
public void animateThenRemove(MarkerWithPosition marker, LatLng from, LatLng to) {
746-
lock.lock();
747-
try {
751+
withLock(() -> {
748752
AnimationTask animationTask = new AnimationTask(marker, from, to, lock);
749753
for (AnimationTask existingTask : ongoingAnimations) {
750754
if (existingTask.marker.getId().equals(animationTask.marker.getId())) {
@@ -756,9 +760,7 @@ public void animateThenRemove(MarkerWithPosition marker, LatLng from, LatLng to)
756760
ongoingAnimations.add(animationTask);
757761
animationTask.removeOnAnimationComplete(mClusterManager.getMarkerManager());
758762
mAnimationTasks.add(animationTask);
759-
} finally {
760-
lock.unlock();
761-
}
763+
});
762764
}
763765

764766
@Override

0 commit comments

Comments
 (0)