Skip to content

Commit 201db21

Browse files
committed
refactor: simplify switch and improve locking in ClusterRendererMultipleItems
- Replaced a traditional switch statement with an enhanced switch expression in `setAnimationType`. - Added a `setAnimationInterpolator` method to allow custom interpolators. - Ensured `lock.unlock()` is always called in `finally` blocks within `MarkerModifier` and `AnimationTask` for better resource management. - Added `@Override` annotation to `ViewModifier.run()`.
1 parent dbd633f commit 201db21

File tree

1 file changed

+67
-59
lines changed

1 file changed

+67
-59
lines changed

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

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -107,31 +107,19 @@ public enum AnimationType {
107107
}
108108

109109
public void setAnimationType(AnimationType type) {
110-
switch (type) {
111-
case EASE_IN, ACCELERATE:
112-
animationInterp = new AccelerateInterpolator();
113-
break;
114-
case EASE_OUT:
115-
animationInterp = new DecelerateInterpolator();
116-
break;
117-
case EASE_IN_OUT:
118-
animationInterp = new AccelerateDecelerateInterpolator();
119-
break;
120-
case FAST_OUT_SLOW_IN:
121-
animationInterp = new FastOutSlowInInterpolator();
122-
break;
123-
case BOUNCE:
124-
animationInterp = new BounceInterpolator();
125-
break;
126-
case DECELERATE:
127-
animationInterp = new DecelerateInterpolator();
128-
break;
129-
default:
130-
animationInterp = new LinearInterpolator();
131-
break;
132-
}
110+
animationInterp = switch (type) {
111+
case LINEAR -> new LinearInterpolator();
112+
case EASE_IN, ACCELERATE -> new AccelerateInterpolator();
113+
case EASE_OUT, DECELERATE -> new DecelerateInterpolator();
114+
case EASE_IN_OUT -> new AccelerateDecelerateInterpolator();
115+
case FAST_OUT_SLOW_IN -> new FastOutSlowInInterpolator();
116+
case BOUNCE -> new BounceInterpolator();
117+
};
133118
}
134119

120+
public void setAnimationInterpolator(TimeInterpolator interpolator) {
121+
animationInterp = interpolator;
122+
}
135123

136124
/**
137125
* Markers that are currently on the map.
@@ -459,6 +447,7 @@ public void setMapZoom(float zoom) {
459447
}
460448

461449
@SuppressLint("NewApi")
450+
@Override
462451
public void run() {
463452
final MarkerModifier markerModifier = new MarkerModifier();
464453
final float zoom = mMapZoom;
@@ -687,13 +676,16 @@ private MarkerModifier() {
687676
*/
688677
public void add(boolean priority, CreateMarkerTask c) {
689678
lock.lock();
690-
sendEmptyMessage(BLANK);
691-
if (priority) {
692-
mOnScreenCreateMarkerTasks.add(c);
693-
} else {
694-
mCreateMarkerTasks.add(c);
679+
try {
680+
sendEmptyMessage(BLANK);
681+
if (priority) {
682+
mOnScreenCreateMarkerTasks.add(c);
683+
} else {
684+
mCreateMarkerTasks.add(c);
685+
}
686+
} finally {
687+
lock.unlock();
695688
}
696-
lock.unlock();
697689
}
698690

699691
/**
@@ -704,13 +696,16 @@ public void add(boolean priority, CreateMarkerTask c) {
704696
*/
705697
public void remove(boolean priority, Marker m) {
706698
lock.lock();
707-
sendEmptyMessage(BLANK);
708-
if (priority) {
709-
mOnScreenRemoveMarkerTasks.add(m);
710-
} else {
711-
mRemoveMarkerTasks.add(m);
699+
try {
700+
sendEmptyMessage(BLANK);
701+
if (priority) {
702+
mOnScreenRemoveMarkerTasks.add(m);
703+
} else {
704+
mRemoveMarkerTasks.add(m);
705+
}
706+
} finally {
707+
lock.unlock();
712708
}
713-
lock.unlock();
714709
}
715710

716711
/**
@@ -722,18 +717,21 @@ public void remove(boolean priority, Marker m) {
722717
*/
723718
public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {
724719
lock.lock();
725-
AnimationTask task = new AnimationTask(marker, from, to, lock);
720+
try {
721+
AnimationTask task = new AnimationTask(marker, from, to, lock);
726722

727-
for (AnimationTask existingTask : ongoingAnimations) {
728-
if (existingTask.marker.getId().equals(task.marker.getId())) {
729-
existingTask.cancel();
730-
break;
723+
for (AnimationTask existingTask : ongoingAnimations) {
724+
if (existingTask.marker.getId().equals(task.marker.getId())) {
725+
existingTask.cancel();
726+
break;
727+
}
731728
}
732-
}
733729

734-
mAnimationTasks.add(task);
735-
ongoingAnimations.add(task);
736-
lock.unlock();
730+
mAnimationTasks.add(task);
731+
ongoingAnimations.add(task);
732+
} finally {
733+
lock.unlock();
734+
}
737735
}
738736

739737
/**
@@ -746,18 +744,21 @@ public void animate(MarkerWithPosition marker, LatLng from, LatLng to) {
746744
*/
747745
public void animateThenRemove(MarkerWithPosition marker, LatLng from, LatLng to) {
748746
lock.lock();
749-
AnimationTask animationTask = new AnimationTask(marker, from, to, lock);
750-
for (AnimationTask existingTask : ongoingAnimations) {
751-
if (existingTask.marker.getId().equals(animationTask.marker.getId())) {
752-
existingTask.cancel();
753-
break;
747+
try {
748+
AnimationTask animationTask = new AnimationTask(marker, from, to, lock);
749+
for (AnimationTask existingTask : ongoingAnimations) {
750+
if (existingTask.marker.getId().equals(animationTask.marker.getId())) {
751+
existingTask.cancel();
752+
break;
753+
}
754754
}
755-
}
756755

757-
ongoingAnimations.add(animationTask);
758-
animationTask.removeOnAnimationComplete(mClusterManager.getMarkerManager());
759-
mAnimationTasks.add(animationTask);
760-
lock.unlock();
756+
ongoingAnimations.add(animationTask);
757+
animationTask.removeOnAnimationComplete(mClusterManager.getMarkerManager());
758+
mAnimationTasks.add(animationTask);
759+
} finally {
760+
lock.unlock();
761+
}
761762
}
762763

763764
@Override
@@ -821,9 +822,13 @@ private void removeMarker(Marker m) {
821822
* @return true if there is still work to be processed.
822823
*/
823824
public boolean isBusy() {
825+
lock.lock();
824826
try {
825-
lock.lock();
826-
return !(mCreateMarkerTasks.isEmpty() && mOnScreenCreateMarkerTasks.isEmpty() && mOnScreenRemoveMarkerTasks.isEmpty() && mRemoveMarkerTasks.isEmpty() && mAnimationTasks.isEmpty());
827+
return !(mCreateMarkerTasks.isEmpty()
828+
&& mOnScreenCreateMarkerTasks.isEmpty()
829+
&& mOnScreenRemoveMarkerTasks.isEmpty()
830+
&& mRemoveMarkerTasks.isEmpty()
831+
&& mAnimationTasks.isEmpty());
827832
} finally {
828833
lock.unlock();
829834
}
@@ -1257,11 +1262,11 @@ public void cancel() {
12571262
new Handler(Looper.getMainLooper()).post(this::cancel);
12581263
return;
12591264
}
1265+
lock.lock();
12601266
try {
12611267
markerWithPosition.position = to;
12621268
mRemoveOnComplete = false;
12631269
valueAnimator.cancel();
1264-
lock.lock();
12651270
ongoingAnimations.remove(this);
12661271
} finally {
12671272
lock.unlock();
@@ -1279,8 +1284,11 @@ public void onAnimationEnd(Animator animation) {
12791284

12801285
// Remove the task from the queue
12811286
lock.lock();
1282-
ongoingAnimations.remove(this);
1283-
lock.unlock();
1287+
try {
1288+
ongoingAnimations.remove(this);
1289+
} finally {
1290+
lock.unlock();
1291+
}
12841292
}
12851293

12861294
public void removeOnAnimationComplete(MarkerManager markerManager) {

0 commit comments

Comments
 (0)