From 4f47febc066409b4ebece17f8f65e29ed8d6ae69 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Wed, 21 Jun 2023 18:18:27 +0800 Subject: [PATCH] Convert AnimationUtils, ConcurrentPool, IntIdGenerator and SimpleAnimatorListener to kotlin --- .../hippo/ehviewer/yorozuya/AnimationUtils.kt | 23 ++++------ .../hippo/ehviewer/yorozuya/ConcurrentPool.kt | 43 +++++++++---------- .../hippo/ehviewer/yorozuya/IntIdGenerator.kt | 41 ++++++++---------- .../yorozuya/SimpleAnimatorListener.java | 27 +++--------- 4 files changed, 54 insertions(+), 80 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/yorozuya/AnimationUtils.kt b/app/src/main/java/com/hippo/ehviewer/yorozuya/AnimationUtils.kt index 75bbca8c2..14e8b641c 100644 --- a/app/src/main/java/com/hippo/ehviewer/yorozuya/AnimationUtils.kt +++ b/app/src/main/java/com/hippo/ehviewer/yorozuya/AnimationUtils.kt @@ -13,20 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.hippo.ehviewer.yorozuya -package com.hippo.ehviewer.yorozuya; +import android.view.animation.Interpolator +import androidx.interpolator.view.animation.FastOutLinearInInterpolator +import androidx.interpolator.view.animation.FastOutSlowInInterpolator +import androidx.interpolator.view.animation.LinearOutSlowInInterpolator -import android.view.animation.Interpolator; - -import androidx.interpolator.view.animation.FastOutLinearInInterpolator; -import androidx.interpolator.view.animation.FastOutSlowInInterpolator; -import androidx.interpolator.view.animation.LinearOutSlowInInterpolator; - -public final class AnimationUtils { - public static final Interpolator FAST_SLOW_INTERPOLATOR = new LinearOutSlowInInterpolator(); - public static final Interpolator SLOW_FAST_INTERPOLATOR = new FastOutLinearInInterpolator(); - public static final Interpolator SLOW_FAST_SLOW_INTERPOLATOR = new FastOutSlowInInterpolator(); - - private AnimationUtils() { - } +object AnimationUtils { + val FAST_SLOW_INTERPOLATOR: Interpolator = LinearOutSlowInInterpolator() + val SLOW_FAST_INTERPOLATOR: Interpolator = FastOutLinearInInterpolator() + val SLOW_FAST_SLOW_INTERPOLATOR: Interpolator = FastOutSlowInInterpolator() } diff --git a/app/src/main/java/com/hippo/ehviewer/yorozuya/ConcurrentPool.kt b/app/src/main/java/com/hippo/ehviewer/yorozuya/ConcurrentPool.kt index f866b3dbb..7069555c8 100644 --- a/app/src/main/java/com/hippo/ehviewer/yorozuya/ConcurrentPool.kt +++ b/app/src/main/java/com/hippo/ehviewer/yorozuya/ConcurrentPool.kt @@ -13,38 +13,35 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.hippo.ehviewer.yorozuya -package com.hippo.ehviewer.yorozuya; +class ConcurrentPool(size: Int) { + private val mArray: Array + private val mMaxSize: Int + private var mSize: Int -public class ConcurrentPool { - - private final T[] mArray; - private final int mMaxSize; - private int mSize; - - @SuppressWarnings("unchecked") - public ConcurrentPool(int size) { - if (size <= 0) { - throw new IllegalStateException("Pool size must > 0, it is " + size); - } - mArray = (T[]) new Object[size]; - mMaxSize = size; - mSize = 0; + init { + check(size > 0) { "Pool size must > 0, it is $size" } + mArray = arrayOfNulls(size) as Array + mMaxSize = size + mSize = 0 } - public synchronized void push(T t) { + @Synchronized + fun push(t: T?) { if (t != null && mSize < mMaxSize) { - mArray[mSize++] = t; + mArray[mSize++] = t } } - public synchronized T pop() { - if (mSize > 0) { - T t = mArray[--mSize]; - mArray[mSize] = null; - return t; + @Synchronized + fun pop(): T? { + return if (mSize > 0) { + val t = mArray[--mSize] + mArray[mSize] = null + t } else { - return null; + null } } } diff --git a/app/src/main/java/com/hippo/ehviewer/yorozuya/IntIdGenerator.kt b/app/src/main/java/com/hippo/ehviewer/yorozuya/IntIdGenerator.kt index 877a24a3b..5aebe83dc 100644 --- a/app/src/main/java/com/hippo/ehviewer/yorozuya/IntIdGenerator.kt +++ b/app/src/main/java/com/hippo/ehviewer/yorozuya/IntIdGenerator.kt @@ -13,39 +13,34 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.hippo.ehviewer.yorozuya -package com.hippo.ehviewer.yorozuya; +import java.util.concurrent.atomic.AtomicInteger -import java.util.concurrent.atomic.AtomicInteger; +class IntIdGenerator { + private val mId = AtomicInteger() -public final class IntIdGenerator { - - public static final int INVALID_ID = -1; - - private final AtomicInteger mId = new AtomicInteger(); - - public IntIdGenerator() { + constructor() + constructor(init: Int) { + setNextId(init) } - public IntIdGenerator(int init) { - setNextId(init); + fun nextId(): Int { + var id: Int + while (mId.getAndIncrement().also { id = it } == INVALID_ID); + return id } - @SuppressWarnings("StatementWithEmptyBody") - public int nextId() { - int id; - while ((id = mId.getAndIncrement()) == INVALID_ID) ; - return id; + fun setNextId(id: Int) { + checkInValidId(id) + mId.set(id) } - public void setNextId(int id) { - checkInValidId(id); - mId.set(id); + private fun checkInValidId(id: Int) { + check(INVALID_ID != id) { "Can't set INVALID_ID" } } - private void checkInValidId(int id) { - if (INVALID_ID == id) { - throw new IllegalStateException("Can't set INVALID_ID"); - } + companion object { + const val INVALID_ID = -1 } } diff --git a/app/src/main/java/com/hippo/ehviewer/yorozuya/SimpleAnimatorListener.java b/app/src/main/java/com/hippo/ehviewer/yorozuya/SimpleAnimatorListener.java index 92a8dcd99..3acd8a4f8 100644 --- a/app/src/main/java/com/hippo/ehviewer/yorozuya/SimpleAnimatorListener.java +++ b/app/src/main/java/com/hippo/ehviewer/yorozuya/SimpleAnimatorListener.java @@ -13,26 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.hippo.ehviewer.yorozuya -package com.hippo.ehviewer.yorozuya; +import android.animation.Animator -import android.animation.Animator; - -public abstract class SimpleAnimatorListener implements Animator.AnimatorListener { - - @Override - public void onAnimationStart(Animator animation) { - } - - @Override - public void onAnimationEnd(Animator animation) { - } - - @Override - public void onAnimationCancel(Animator animation) { - } - - @Override - public void onAnimationRepeat(Animator animation) { - } +abstract class SimpleAnimatorListener : Animator.AnimatorListener { + override fun onAnimationStart(animation: Animator) {} + override fun onAnimationEnd(animation: Animator) {} + override fun onAnimationCancel(animation: Animator) {} + override fun onAnimationRepeat(animation: Animator) {} }