From 07543e93efbdda85f874f8e04c0d8b5a82bd1725 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Mon, 22 Dec 2025 07:19:28 +0100 Subject: [PATCH] Kotlin ObjectPool --- .../charting/charts/BarLineChartBase.java | 40 ++-- .../mikephil/charting/charts/Chart.java | 16 +- .../charting/charts/HorizontalBarChart.java | 6 +- .../mikephil/charting/charts/PieChart.java | 22 +- .../charting/charts/PieRadarChartBase.java | 26 +-- .../charting/jobs/AnimatedMoveViewJob.kt | 25 +- .../charting/jobs/AnimatedViewPortJob.kt | 16 +- .../mikephil/charting/jobs/AnimatedZoomJob.kt | 60 ++--- .../mikephil/charting/jobs/MoveViewJob.kt | 24 +- .../mikephil/charting/jobs/ViewPortJob.kt | 15 +- .../github/mikephil/charting/jobs/ZoomJob.kt | 46 ++-- .../mikephil/charting/utils/CanvasUtils.kt | 2 +- .../github/mikephil/charting/utils/FSize.java | 81 ------- .../github/mikephil/charting/utils/FSize.kt | 76 ++++++ .../utils/HorizontalViewPortHandler.java | 9 - .../utils/HorizontalViewPortHandler.kt | 3 + .../mikephil/charting/utils/MPPointD.java | 56 ----- .../mikephil/charting/utils/MPPointD.kt | 42 ++++ .../mikephil/charting/utils/MPPointF.java | 99 -------- .../mikephil/charting/utils/MPPointF.kt | 85 +++++++ .../mikephil/charting/utils/ObjectPool.java | 216 ------------------ .../mikephil/charting/utils/ObjectPool.kt | 182 +++++++++++++++ .../mikephil/charting/utils/Transformer.java | 8 +- .../github/mikephil/charting/utils/Utils.java | 51 ++--- .../charting/utils/ViewPortHandler.kt | 7 +- .../mikephil/charting/test/ObjectPoolTest.kt | 10 +- 26 files changed, 573 insertions(+), 650 deletions(-) delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java index df391f506..77d15a120 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -596,7 +596,7 @@ public void zoomIn() { MPPointF center = mViewPortHandler.getContentCenter(); - mViewPortHandler.zoomIn(center.x, -center.y, mZoomMatrixBuffer); + mViewPortHandler.zoomIn(center.getX(), -center.getY(), mZoomMatrixBuffer); mViewPortHandler.refresh(mZoomMatrixBuffer, this, false); MPPointF.recycleInstance(center); @@ -615,7 +615,7 @@ public void zoomOut() { MPPointF center = mViewPortHandler.getContentCenter(); - mViewPortHandler.zoomOut(center.x, -center.y, mZoomMatrixBuffer); + mViewPortHandler.zoomOut(center.getX(), -center.getY(), mZoomMatrixBuffer); mViewPortHandler.refresh(mZoomMatrixBuffer, this, false); MPPointF.recycleInstance(center); @@ -669,7 +669,7 @@ public void zoom(float scaleX, float scaleY, float x, float y) { */ public void zoom(float scaleX, float scaleY, float xValue, float yValue, AxisDependency axis) { - Runnable job = ZoomJob.getInstance(mViewPortHandler, scaleX, scaleY, xValue, yValue, getTransformer(axis), axis, this); + Runnable job = ZoomJob.Companion.getInstance(mViewPortHandler, scaleX, scaleY, xValue, yValue, getTransformer(axis), axis, this); addViewportJob(job); } @@ -681,7 +681,7 @@ public void zoomToCenter(float scaleX, float scaleY) { MPPointF center = getCenterOffsets(); Matrix save = mZoomMatrixBuffer; - mViewPortHandler.zoom(scaleX, scaleY, center.x, -center.y, save); + mViewPortHandler.zoom(scaleX, scaleY, center.getX(), -center.getY(), save); mViewPortHandler.refresh(save, this, false); } @@ -692,10 +692,10 @@ public void zoomAndCenterAnimated(float scaleX, float scaleY, float xValue, floa MPPointD origin = getValuesByTouchPoint(mViewPortHandler.contentLeft(), mViewPortHandler.contentTop(), axis); - Runnable job = AnimatedZoomJob.getInstance(mViewPortHandler, this, getTransformer(axis), getAxis(axis), mXAxis.mAxisRange, scaleX, scaleY, mViewPortHandler.getScaleX(), mViewPortHandler.getScaleY(), xValue, yValue, (float) origin.x, (float) origin.y, duration); + Runnable job = AnimatedZoomJob.Companion.getInstance(mViewPortHandler, this, getTransformer(axis), getAxis(axis), mXAxis.mAxisRange, scaleX, scaleY, mViewPortHandler.getScaleX(), mViewPortHandler.getScaleY(), xValue, yValue, (float) origin.getX(), (float) origin.getY(), duration); addViewportJob(job); - MPPointD.recycleInstance(origin); + MPPointD.Companion.recycleInstance(origin); } protected Matrix mFitScreenMatrixBuffer = new Matrix(); @@ -797,7 +797,7 @@ public void setVisibleYRange(float minYRange, float maxYRange, AxisDependency ax */ public void moveViewToX(float xValue) { - Runnable job = MoveViewJob.getInstance(mViewPortHandler, xValue, 0f, getTransformer(AxisDependency.LEFT), this); + Runnable job = MoveViewJob.Companion.getInstance(mViewPortHandler, xValue, 0f, getTransformer(AxisDependency.LEFT), this); addViewportJob(job); } @@ -813,7 +813,7 @@ public void moveViewTo(float xValue, float yValue, AxisDependency axis) { float yInView = getAxisRange(axis) / mViewPortHandler.getScaleY(); - Runnable job = MoveViewJob.getInstance(mViewPortHandler, xValue, yValue + yInView / 2f, getTransformer(axis), this); + Runnable job = MoveViewJob.Companion.getInstance(mViewPortHandler, xValue, yValue + yInView / 2f, getTransformer(axis), this); addViewportJob(job); } @@ -831,11 +831,11 @@ public void moveViewToAnimated(float xValue, float yValue, AxisDependency axis, float yInView = getAxisRange(axis) / mViewPortHandler.getScaleY(); - Runnable job = AnimatedMoveViewJob.getInstance(mViewPortHandler, xValue, yValue + yInView / 2f, getTransformer(axis), this, (float) bounds.x, (float) bounds.y, duration); + Runnable job = AnimatedMoveViewJob.Companion.getInstance(mViewPortHandler, xValue, yValue + yInView / 2f, getTransformer(axis), this, (float) bounds.getX(), (float) bounds.getY(), duration); addViewportJob(job); - MPPointD.recycleInstance(bounds); + MPPointD.Companion.recycleInstance(bounds); } /** @@ -848,7 +848,7 @@ public void centerViewToY(float yValue, AxisDependency axis) { float valsInView = getAxisRange(axis) / mViewPortHandler.getScaleY(); - Runnable job = MoveViewJob.getInstance(mViewPortHandler, 0f, yValue + valsInView / 2f, getTransformer(axis), this); + Runnable job = MoveViewJob.Companion.getInstance(mViewPortHandler, 0f, yValue + valsInView / 2f, getTransformer(axis), this); addViewportJob(job); } @@ -865,7 +865,7 @@ public void centerViewTo(float xValue, float yValue, AxisDependency axis) { float yInView = getAxisRange(axis) / mViewPortHandler.getScaleY(); float xInView = getXAxis().mAxisRange / mViewPortHandler.getScaleX(); - Runnable job = MoveViewJob.getInstance(mViewPortHandler, xValue - xInView / 2f, yValue + yInView / 2f, getTransformer(axis), this); + Runnable job = MoveViewJob.Companion.getInstance(mViewPortHandler, xValue - xInView / 2f, yValue + yInView / 2f, getTransformer(axis), this); addViewportJob(job); } @@ -883,11 +883,11 @@ public void centerViewToAnimated(float xValue, float yValue, AxisDependency axis float yInView = getAxisRange(axis) / mViewPortHandler.getScaleY(); float xInView = getXAxis().mAxisRange / mViewPortHandler.getScaleX(); - Runnable job = AnimatedMoveViewJob.getInstance(mViewPortHandler, xValue - xInView / 2f, yValue + yInView / 2f, getTransformer(axis), this, (float) bounds.x, (float) bounds.y, duration); + Runnable job = AnimatedMoveViewJob.Companion.getInstance(mViewPortHandler, xValue - xInView / 2f, yValue + yInView / 2f, getTransformer(axis), this, (float) bounds.getX(), (float) bounds.getY(), duration); addViewportJob(job); - MPPointD.recycleInstance(bounds); + MPPointD.Companion.recycleInstance(bounds); } /** @@ -969,7 +969,7 @@ public MPPointF getPosition(Entry e, AxisDependency axis) { getTransformer(axis).pointValuesToPixel(mGetPositionBuffer); - return MPPointF.getInstance(mGetPositionBuffer[0], mGetPositionBuffer[1]); + return MPPointF.Companion.getInstance(mGetPositionBuffer[0], mGetPositionBuffer[1]); } /** @@ -1209,7 +1209,7 @@ public void setKeepPositionOnRotation(boolean keepPositionOnRotation) { * getPixelForValues(...). */ public MPPointD getValuesByTouchPoint(float x, float y, AxisDependency axis) { - MPPointD result = MPPointD.getInstance(0, 0); + MPPointD result = MPPointD.Companion.getInstance(0, 0); getValuesByTouchPoint(x, y, axis, result); return result; } @@ -1252,7 +1252,7 @@ public IBarLineScatterCandleBubbleDataSet getDataSetByTouchPoint(float x, float /** * buffer for storing lowest visible x point */ - protected MPPointD posForGetLowestVisibleX = MPPointD.getInstance(0, 0); + protected MPPointD posForGetLowestVisibleX = MPPointD.Companion.getInstance(0, 0); /** * Returns the lowest x-index (value on the x-axis) that is still visible on the chart. @@ -1260,13 +1260,13 @@ public IBarLineScatterCandleBubbleDataSet getDataSetByTouchPoint(float x, float @Override public float getLowestVisibleX() { getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentLeft(), mViewPortHandler.contentBottom(), posForGetLowestVisibleX); - return (float) Math.max(mXAxis.mAxisMinimum, posForGetLowestVisibleX.x); + return (float) Math.max(mXAxis.mAxisMinimum, posForGetLowestVisibleX.getX()); } /** * buffer for storing highest visible x point */ - protected MPPointD posForGetHighestVisibleX = MPPointD.getInstance(0, 0); + protected MPPointD posForGetHighestVisibleX = MPPointD.Companion.getInstance(0, 0); /** * Returns the highest x-index (value on the x-axis) that is still visible @@ -1275,7 +1275,7 @@ public float getLowestVisibleX() { @Override public float getHighestVisibleX() { getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentRight(), mViewPortHandler.contentBottom(), posForGetHighestVisibleX); - return (float) Math.min(mXAxis.mAxisMaximum, posForGetHighestVisibleX.x); + return (float) Math.min(mXAxis.mAxisMaximum, posForGetHighestVisibleX.getX()); } /** diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java index a216f2632..6f55fc899 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java @@ -390,17 +390,17 @@ protected void onDraw(@NonNull Canvas canvas) { switch (mInfoPaint.getTextAlign()) { case LEFT: - pt.x = 0; - canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint); + pt.setX(0); + canvas.drawText(mNoDataText, pt.getX(), pt.getY(), mInfoPaint); break; case RIGHT: - pt.x *= 2.0F; - canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint); + pt.setX(pt.getX() * 2.0F); + canvas.drawText(mNoDataText, pt.getX(), pt.getY(), mInfoPaint); break; default: - canvas.drawText(mNoDataText, pt.x, pt.y, mInfoPaint); + canvas.drawText(mNoDataText, pt.getX(), pt.getY(), mInfoPaint); break; } } @@ -437,8 +437,8 @@ protected void drawDescription(Canvas c) { x = getWidth() - mViewPortHandler.offsetRight() - mDescription.getXOffset(); y = getHeight() - mViewPortHandler.offsetBottom() - mDescription.getYOffset(); } else { - x = position.x; - y = position.y; + x = position.getX(); + y = position.getY(); } c.drawText(mDescription.text, x, y, mDescPaint); @@ -1015,7 +1015,7 @@ public float getXRange() { * Returns the center point of the chart (the whole View) in pixels. */ public MPPointF getCenter() { - return MPPointF.getInstance(getWidth() / 2f, getHeight() / 2f); + return MPPointF.Companion.getInstance(getWidth() / 2f, getHeight() / 2f); } /** diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java index b4cff03a1..cc1ac21f7 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java @@ -263,7 +263,7 @@ public MPPointF getPosition(Entry e, AxisDependency axis) { getTransformer(axis).pointValuesToPixel(vals); - return MPPointF.getInstance(vals[0], vals[1]); + return MPPointF.Companion.getInstance(vals[0], vals[1]); } /** @@ -287,14 +287,14 @@ public Highlight getHighlightByTouchPoint(float x, float y) { public float getLowestVisibleX() { getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentLeft(), mViewPortHandler.contentBottom(), posForGetLowestVisibleX); - return (float) Math.max(mXAxis.mAxisMinimum, posForGetLowestVisibleX.y); + return (float) Math.max(mXAxis.mAxisMinimum, posForGetLowestVisibleX.getY()); } @Override public float getHighestVisibleX() { getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(mViewPortHandler.contentLeft(), mViewPortHandler.contentTop(), posForGetHighestVisibleX); - return (float) Math.min(mXAxis.mAxisMaximum, posForGetHighestVisibleX.y); + return (float) Math.min(mXAxis.mAxisMaximum, posForGetHighestVisibleX.getY()); } /** diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java index 2e4641828..3dcaa9521 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieChart.java @@ -80,7 +80,7 @@ public class PieChart extends PieRadarChartBase { */ private CharSequence mCenterText = ""; - private final MPPointF mCenterTextOffset = MPPointF.getInstance(0, 0); + private final MPPointF mCenterTextOffset = MPPointF.Companion.getInstance(0, 0); /** * indicates the size of the hole in the center of the piechart, @@ -170,10 +170,10 @@ public void calculateOffsets() { // create the circle box that will contain the pie-chart (the bounds of // the pie-chart) - mCircleBox.set(c.x - radius + shift, - c.y - radius + shift, - c.x + radius - shift, - c.y + radius - shift); + mCircleBox.set(c.getX() - radius + shift, + c.getY() - radius + shift, + c.getX() + radius - shift, + c.getY() + radius - shift); MPPointF.recycleInstance(c); } @@ -207,10 +207,10 @@ protected float[] getMarkerPosition(Highlight highlight) { // calculate the text position float x = (float) (r * Math.cos(Math.toRadians((rotationAngle + mAbsoluteAngles[entryIndex] - offset) - * mAnimator.getPhaseY())) + center.x); + * mAnimator.getPhaseY())) + center.getX()); float y = (float) (r * Math.sin(Math.toRadians((rotationAngle + mAbsoluteAngles[entryIndex] - offset) - * mAnimator.getPhaseY())) + center.y); + * mAnimator.getPhaseY())) + center.getY()); MPPointF.recycleInstance(center); return new float[]{x, y}; @@ -496,7 +496,7 @@ public RectF getCircleBox() { * */ public MPPointF getCenterCircleBox() { - return MPPointF.getInstance(mCircleBox.centerX(), mCircleBox.centerY()); + return MPPointF.Companion.getInstance(mCircleBox.centerX(), mCircleBox.centerY()); } /** @@ -529,8 +529,8 @@ public void setCenterTextSizePixels(float sizePixels) { * */ public void setCenterTextOffset(float x, float y) { - mCenterTextOffset.x = UtilsKtKt.convertDpToPixel(x); - mCenterTextOffset.y = UtilsKtKt.convertDpToPixel(y); + mCenterTextOffset.setX(UtilsKtKt.convertDpToPixel(x)); + mCenterTextOffset.setY(UtilsKtKt.convertDpToPixel(y)); } /** @@ -538,7 +538,7 @@ public void setCenterTextOffset(float x, float y) { * */ public MPPointF getCenterTextOffset() { - return MPPointF.getInstance(mCenterTextOffset.x, mCenterTextOffset.y); + return MPPointF.Companion.getInstance(mCenterTextOffset.getX(), mCenterTextOffset.getY()); } /** diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java index bc07f4138..991c7904e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/PieRadarChartBase.java @@ -149,10 +149,10 @@ public void calculateOffsets() { MPPointF reference = getPosition(center, getRadius(), getAngleForPoint(bottomX, bottomY)); - float distReference = distanceToCenter(reference.x, reference.y); + float distReference = distanceToCenter(reference.getX(), reference.getY()); float minOffset = UtilsKtKt.convertDpToPixel(5f); - if (bottomY >= center.y && getHeight() - legendWidth > getWidth()) { + if (bottomY >= center.getY() && getHeight() - legendWidth > getWidth()) { xLegendOffset = legendWidth; } else if (distLegend < distReference) { @@ -258,13 +258,13 @@ public float getAngleForPoint(float x, float y) { MPPointF c = getCenterOffsets(); - double tx = x - c.x, ty = y - c.y; + double tx = x - c.getX(), ty = y - c.getY(); double length = Math.sqrt(tx * tx + ty * ty); double r = Math.acos(ty / length); float angle = (float) Math.toDegrees(r); - if (x > c.x) + if (x > c.getX()) angle = 360f - angle; // add 90° because chart starts EAST @@ -287,14 +287,14 @@ public float getAngleForPoint(float x, float y) { */ public MPPointF getPosition(MPPointF center, float dist, float angle) { - MPPointF p = MPPointF.getInstance(0, 0); + MPPointF p = MPPointF.Companion.getInstance(0, 0); getPosition(center, dist, angle, p); return p; } public void getPosition(MPPointF center, float dist, float angle, MPPointF outputPoint) { - outputPoint.x = (float) (center.x + dist * Math.cos(Math.toRadians(angle))); - outputPoint.y = (float) (center.y + dist * Math.sin(Math.toRadians(angle))); + outputPoint.setX((float) (center.getX() + dist * Math.cos(Math.toRadians(angle)))); + outputPoint.setY((float) (center.getY() + dist * Math.sin(Math.toRadians(angle)))); } /** @@ -309,16 +309,16 @@ public float distanceToCenter(float x, float y) { float xDist; float yDist; - if (x > c.x) { - xDist = x - c.x; + if (x > c.getX()) { + xDist = x - c.getX(); } else { - xDist = c.x - x; + xDist = c.getX() - x; } - if (y > c.y) { - yDist = y - c.y; + if (y > c.getY()) { + yDist = y - c.getY(); } else { - yDist = c.y - y; + yDist = c.getY() - y; } // pythagoras diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt index 08f02edf8..e02d34157 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt @@ -4,13 +4,12 @@ import android.animation.ValueAnimator import android.annotation.SuppressLint import android.view.View import com.github.mikephil.charting.utils.ObjectPool -import com.github.mikephil.charting.utils.ObjectPool.Poolable import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.ViewPortHandler @SuppressLint("NewApi") class AnimatedMoveViewJob( - viewPortHandler: ViewPortHandler?, + viewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, trans: Transformer?, @@ -18,34 +17,32 @@ class AnimatedMoveViewJob( xOrigin: Float, yOrigin: Float, duration: Long -) : AnimatedViewPortJob(viewPortHandler, xValue, yValue, trans, v, xOrigin, yOrigin, duration) { +) : AnimatedViewPortJob(viewPortHandler, xValue, yValue, trans, v, xOrigin, yOrigin, duration) { override fun onAnimationUpdate(animation: ValueAnimator) { pts[0] = xOrigin + (xValue - xOrigin) * phase pts[1] = yOrigin + (yValue - yOrigin) * phase - transformer!!.pointValuesToPixel(pts) - viewPortHandler!!.centerViewPort(pts, view!!) + mTrans?.pointValuesToPixel(pts) + mViewPortHandler.centerViewPort(pts, view) } override fun recycleSelf() { recycleInstance(this) } - protected override fun instantiate(): Poolable { - return AnimatedMoveViewJob(null, 0f, 0f, null, null, 0f, 0f, 0) + override fun instantiate(): AnimatedMoveViewJob { + return AnimatedMoveViewJob(ViewPortHandler(), 0f, 0f, null, null, 0f, 0f, 0) } companion object { - private val pool: ObjectPool + private val pool = ObjectPool.create(4, AnimatedMoveViewJob(ViewPortHandler(), 0f, 0f, null, null, 0f, 0f, 0)) init { - pool = ObjectPool.create(4, AnimatedMoveViewJob(null, 0f, 0f, null, null, 0f, 0f, 0)) as ObjectPool - pool.setReplenishPercentage(0.5f) + pool.replenishPercentage = 0.5f } - @JvmStatic fun getInstance( - viewPortHandler: ViewPortHandler?, + viewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, trans: Transformer?, @@ -55,10 +52,10 @@ class AnimatedMoveViewJob( duration: Long ): AnimatedMoveViewJob { val result: AnimatedMoveViewJob = pool.get() - result.viewPortHandler = viewPortHandler + result.mViewPortHandler = viewPortHandler result.xValue = xValue result.yValue = yValue - result.transformer = trans + result.mTrans = trans result.view = v result.xOrigin = xOrigin result.yOrigin = yOrigin diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt index daa1ca338..94c88c469 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt @@ -10,8 +10,8 @@ import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.ViewPortHandler @SuppressLint("NewApi") -abstract class AnimatedViewPortJob( - viewPortHandler: ViewPortHandler?, +abstract class AnimatedViewPortJob>( + viewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, trans: Transformer?, @@ -19,8 +19,8 @@ abstract class AnimatedViewPortJob( var xOrigin: Float, var yOrigin: Float, duration: Long -) : ViewPortJob(viewPortHandler, xValue, yValue, trans, v), AnimatorUpdateListener, Animator.AnimatorListener { - protected var animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "phase", 0f, 1f) +) : ViewPortJob(viewPortHandler, xValue, yValue, trans, v), AnimatorUpdateListener, Animator.AnimatorListener { + protected val animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "phase", 0f, 1f) var phase: Float = 0f @@ -52,6 +52,7 @@ abstract class AnimatedViewPortJob( try { recycleSelf() } catch (_: IllegalArgumentException) { + // don't worry about it. } } @@ -59,10 +60,13 @@ abstract class AnimatedViewPortJob( try { recycleSelf() } catch (_: IllegalArgumentException) { + // don't worry about it. } } - override fun onAnimationRepeat(animation: Animator) = Unit + override fun onAnimationRepeat(animation: Animator) { + } - override fun onAnimationUpdate(animation: ValueAnimator) = Unit + override fun onAnimationUpdate(animation: ValueAnimator) { + } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt index 89b239931..6aa6e1156 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt @@ -8,16 +8,15 @@ import android.view.View import com.github.mikephil.charting.charts.BarLineChartBase import com.github.mikephil.charting.components.YAxis import com.github.mikephil.charting.utils.ObjectPool -import com.github.mikephil.charting.utils.ObjectPool.Poolable import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.ViewPortHandler @SuppressLint("NewApi") open class AnimatedZoomJob @SuppressLint("NewApi") constructor( - viewPortHandler: ViewPortHandler?, + viewPortHandler: ViewPortHandler, v: View?, trans: Transformer?, - axis: YAxis?, + axis: YAxis, xAxisRange: Float, scaleX: Float, scaleY: Float, @@ -28,8 +27,8 @@ open class AnimatedZoomJob @SuppressLint("NewApi") constructor( protected var zoomOriginX: Float, protected var zoomOriginY: Float, duration: Long -) : AnimatedViewPortJob(viewPortHandler, scaleX, scaleY, trans, v, xOrigin, yOrigin, duration), Animator.AnimatorListener { - protected var yAxis: YAxis? = null +) : AnimatedViewPortJob(viewPortHandler, scaleX, scaleY, trans, v, xOrigin, yOrigin, duration), Animator.AnimatorListener { + protected var yAxis: YAxis protected var xAxisRange: Float @@ -46,46 +45,49 @@ open class AnimatedZoomJob @SuppressLint("NewApi") constructor( val scaleY = yOrigin + (yValue - yOrigin) * phase val save = mOnAnimationUpdateMatrixBuffer - viewPortHandler!!.setZoom(scaleX, scaleY, save) - viewPortHandler!!.refresh(save, view!!, false) + mViewPortHandler.setZoom(scaleX, scaleY, save) + mViewPortHandler.refresh(save, view, false) - val valsInView = yAxis?.let { it.mAxisRange / viewPortHandler!!.scaleY } - val xsInView = xAxisRange / viewPortHandler!!.scaleX + val valsInView = yAxis.mAxisRange / mViewPortHandler.scaleY + val xsInView = xAxisRange / mViewPortHandler.scaleX pts[0] = zoomOriginX + ((zoomCenterX - xsInView / 2f) - zoomOriginX) * phase - valsInView?.let { pts[1] = zoomOriginY + ((zoomCenterY + it / 2f) - zoomOriginY) * phase } + pts[1] = zoomOriginY + ((zoomCenterY + valsInView / 2f) - zoomOriginY) * phase - transformer!!.pointValuesToPixel(pts) + mTrans?.pointValuesToPixel(pts) - viewPortHandler!!.translate(pts, save) - viewPortHandler!!.refresh(save, view!!, true) + mViewPortHandler.translate(pts, save) + mViewPortHandler.refresh(save, view, true) } override fun onAnimationEnd(animation: Animator) { - (view as BarLineChartBase<*>).calculateOffsets() - view!!.postInvalidate() + (view as? BarLineChartBase<*>)?.calculateOffsets() + view?.postInvalidate() } - override fun onAnimationCancel(animation: Animator) = Unit + override fun onAnimationCancel(animation: Animator) { + } - override fun onAnimationRepeat(animation: Animator) = Unit + override fun onAnimationRepeat(animation: Animator) { + } - override fun recycleSelf() = Unit + override fun recycleSelf() { + } + + override fun onAnimationStart(animation: Animator) { + } - override fun onAnimationStart(animation: Animator) = Unit - protected override fun instantiate(): Poolable { - return AnimatedZoomJob(null, null, null, null, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0) + override fun instantiate(): AnimatedZoomJob { + return AnimatedZoomJob(ViewPortHandler(), null, null, YAxis(), 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0) } companion object { - private val pool: ObjectPool = - ObjectPool.create(8, AnimatedZoomJob(null, null, null, null, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0)) as ObjectPool + private val pool = ObjectPool.create(8, AnimatedZoomJob(ViewPortHandler(), null, null, YAxis(), 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0)) - @JvmStatic fun getInstance( - viewPortHandler: ViewPortHandler?, + viewPortHandler: ViewPortHandler, v: View?, - trans: Transformer?, + trans: Transformer, axis: YAxis, xAxisRange: Float, scaleX: Float, @@ -99,10 +101,10 @@ open class AnimatedZoomJob @SuppressLint("NewApi") constructor( duration: Long ): AnimatedZoomJob { val result: AnimatedZoomJob = pool.get() - result.viewPortHandler = viewPortHandler + result.mViewPortHandler = viewPortHandler result.xValue = scaleX result.yValue = scaleY - result.transformer = trans + result.mTrans = trans result.view = v result.xOrigin = xOrigin result.yOrigin = yOrigin @@ -113,7 +115,7 @@ open class AnimatedZoomJob @SuppressLint("NewApi") constructor( result.zoomOriginX = zoomOriginX result.zoomOriginY = zoomOriginY result.resetAnimator() - result.animator.setDuration(duration) + result.animator.duration = duration return result } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt index 202f2c1cf..8501957db 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt @@ -2,40 +2,38 @@ package com.github.mikephil.charting.jobs import android.view.View import com.github.mikephil.charting.utils.ObjectPool -import com.github.mikephil.charting.utils.ObjectPool.Poolable import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.ViewPortHandler -class MoveViewJob(viewPortHandler: ViewPortHandler?, xValue: Float, yValue: Float, trans: Transformer?, v: View?) : - ViewPortJob(viewPortHandler, xValue, yValue, trans, v) { +open class MoveViewJob(viewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, trans: Transformer?, v: View?) : + ViewPortJob(viewPortHandler, xValue, yValue, trans, v) { override fun run() { pts[0] = xValue pts[1] = yValue - transformer!!.pointValuesToPixel(pts) - viewPortHandler!!.centerViewPort(pts, view!!) + mTrans?.pointValuesToPixel(pts) + mViewPortHandler.centerViewPort(pts, view) recycleInstance(this) } - protected override fun instantiate(): Poolable { - return MoveViewJob(viewPortHandler, xValue, yValue, transformer, view) + override fun instantiate(): MoveViewJob { + return MoveViewJob(mViewPortHandler, xValue, yValue, mTrans, view) } companion object { - private val pool: ObjectPool = ObjectPool.create(2, MoveViewJob(null, 0f, 0f, null, null)) as ObjectPool + private val pool = ObjectPool.create(2, MoveViewJob(ViewPortHandler(), 0f, 0f, null, null)) init { - pool.setReplenishPercentage(0.5f) + pool.replenishPercentage = 0.5f } - @JvmStatic - fun getInstance(viewPortHandler: ViewPortHandler?, xValue: Float, yValue: Float, trans: Transformer?, v: View?): MoveViewJob { + fun getInstance(viewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, trans: Transformer?, v: View?): MoveViewJob { val result: MoveViewJob = pool.get() - result.viewPortHandler = viewPortHandler + result.mViewPortHandler = viewPortHandler result.xValue = xValue result.yValue = yValue - result.transformer = trans + result.mTrans = trans result.view = v return result } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt index b8bf0b9d5..55de1b21e 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt @@ -12,32 +12,27 @@ import com.github.mikephil.charting.utils.ViewPortHandler * This is especially important if viewport modifying methods are called on the chart * directly after initialization. */ -abstract class ViewPortJob( - @JvmField protected var viewPortHandler: ViewPortHandler?, xValue: Float, yValue: Float, +abstract class ViewPortJob>( + protected var mViewPortHandler: ViewPortHandler, xValue: Float, yValue: Float, trans: Transformer?, v: View? -) : Poolable(), Runnable { - @JvmField +) : Poolable(), Runnable { protected var pts: FloatArray = FloatArray(2) var xValue: Float = 0f protected set var yValue: Float = 0f protected set - @JvmField - protected var transformer: Transformer? - @JvmField + protected var mTrans: Transformer? protected var view: View? init { this.xValue = xValue this.yValue = yValue - this.transformer = trans + this.mTrans = trans this.view = v } protected fun recycle() { - viewPortHandler = null - transformer = null view = null } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt index 32bc50b1d..ec9fc45e5 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt @@ -5,52 +5,52 @@ import android.view.View import com.github.mikephil.charting.charts.BarLineChartBase import com.github.mikephil.charting.components.YAxis.AxisDependency import com.github.mikephil.charting.utils.ObjectPool -import com.github.mikephil.charting.utils.ObjectPool.Poolable import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.ViewPortHandler -class ZoomJob( - viewPortHandler: ViewPortHandler?, protected var scaleX: Float, protected var scaleY: Float, xValue: Float, yValue: Float, trans: Transformer?, +open class ZoomJob( + viewPortHandler: ViewPortHandler, protected var scaleX: Float, protected var scaleY: Float, xValue: Float, yValue: Float, trans: Transformer?, protected var axisDependency: AxisDependency?, v: View? -) : ViewPortJob(viewPortHandler, xValue, yValue, trans, v) { +) : ViewPortJob(viewPortHandler, xValue, yValue, trans, v) { protected var mRunMatrixBuffer: Matrix = Matrix() override fun run() { val save = mRunMatrixBuffer - viewPortHandler!!.zoom(scaleX, scaleY, save) - viewPortHandler!!.refresh(save, view!!, false) + mViewPortHandler.zoom(scaleX, scaleY, save) + mViewPortHandler.refresh(save, view, false) - val yValsInView = (view as BarLineChartBase<*>).getAxis(axisDependency).mAxisRange / viewPortHandler!!.scaleY - val xValsInView = (view as BarLineChartBase<*>).getXAxis().mAxisRange / viewPortHandler!!.scaleX + (view as? BarLineChartBase<*>)?.let { view -> + val yValsInView = view.getAxis(axisDependency).mAxisRange / mViewPortHandler.scaleY + val xValsInView = view.xAxis.mAxisRange / mViewPortHandler.scaleX - pts[0] = xValue - xValsInView / 2f - pts[1] = yValue + yValsInView / 2f + pts[0] = xValue - xValsInView / 2f + pts[1] = yValue + yValsInView / 2f - transformer!!.pointValuesToPixel(pts) + mTrans?.pointValuesToPixel(pts) - viewPortHandler!!.translate(pts, save) - viewPortHandler!!.refresh(save, view!!, false) + mViewPortHandler.translate(pts, save) + mViewPortHandler.refresh(save, view, false) - (view as BarLineChartBase<*>).calculateOffsets() - view!!.postInvalidate() + view.calculateOffsets() + view.postInvalidate() + } recycleInstance(this) } - protected override fun instantiate(): Poolable { - return ZoomJob(null, 0f, 0f, 0f, 0f, null, null, null) + override fun instantiate(): ZoomJob { + return ZoomJob(ViewPortHandler(), 0f, 0f, 0f, 0f, null, null, null) } companion object { - private val pool: ObjectPool = ObjectPool.create(1, ZoomJob(null, 0f, 0f, 0f, 0f, null, null, null)) as ObjectPool + private val pool = ObjectPool.create(1, ZoomJob(ViewPortHandler(), 0f, 0f, 0f, 0f, null, null, null)) init { - pool.setReplenishPercentage(0.5f) + pool.replenishPercentage = 0.5f } - @JvmStatic fun getInstance( - viewPortHandler: ViewPortHandler?, scaleX: Float, scaleY: Float, xValue: Float, yValue: Float, + viewPortHandler: ViewPortHandler, scaleX: Float, scaleY: Float, xValue: Float, yValue: Float, trans: Transformer?, axis: AxisDependency?, v: View? ): ZoomJob { val result: ZoomJob = pool.get() @@ -58,8 +58,8 @@ class ZoomJob( result.yValue = yValue result.scaleX = scaleX result.scaleY = scaleY - result.viewPortHandler = viewPortHandler - result.transformer = trans + result.mViewPortHandler = viewPortHandler + result.mTrans = trans result.axisDependency = axis result.view = v return result diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/CanvasUtils.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/CanvasUtils.kt index aa0f9615b..982c806c3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/CanvasUtils.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/CanvasUtils.kt @@ -28,7 +28,7 @@ fun Canvas.drawImage( ) { val width: Int = drawable.intrinsicWidth val height: Int = drawable.intrinsicHeight - val drawOffset = MPPointF.getInstance() + val drawOffset = MPPointF.getInstance(width.toFloat(), height.toFloat()) drawOffset.x = x - (width.toFloat() / 2) drawOffset.y = y - (height.toFloat() / 2) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.java deleted file mode 100644 index 47d89ec16..000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.java +++ /dev/null @@ -1,81 +0,0 @@ - -package com.github.mikephil.charting.utils; - -import java.util.List; - -import androidx.annotation.NonNull; - -/** - * Class for describing width and height dimensions in some arbitrary - * unit. Replacement for the android.Util.SizeF which is available only on API >= 21. - */ -public final class FSize extends ObjectPool.Poolable{ - - // TODO : Encapsulate width & height - - public float width; - public float height; - - private static final ObjectPool pool; - - static { - pool = ObjectPool.create(256, new FSize(0,0)); - pool.setReplenishPercentage(0.5f); - } - - - protected ObjectPool.Poolable instantiate(){ - return new FSize(0,0); - } - - public static FSize getInstance(final float width, final float height){ - FSize result = pool.get(); - result.width = width; - result.height = height; - return result; - } - - public static void recycleInstance(FSize instance){ - pool.recycle(instance); - } - - public static void recycleInstances(List instances){ - pool.recycle(instances); - } - - public FSize() { - } - - public FSize(final float width, final float height) { - this.width = width; - this.height = height; - } - - @Override - public boolean equals(final Object obj) { - if (obj == null) { - return false; - } - if (this == obj) { - return true; - } - if (obj instanceof FSize other) { - return width == other.width && height == other.height; - } - return false; - } - - @NonNull - @Override - public String toString() { - return width + "x" + height; - } - - /** - * {@inheritDoc} - */ - @Override - public int hashCode() { - return Float.floatToIntBits(width) ^ Float.floatToIntBits(height); - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt new file mode 100644 index 000000000..1cfb52c62 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/FSize.kt @@ -0,0 +1,76 @@ +package com.github.mikephil.charting.utils + +import com.github.mikephil.charting.utils.ObjectPool.Poolable +import kotlin.Any +import kotlin.Boolean +import kotlin.Int +import kotlin.String + +/** + * Class for describing width and height dimensions in some arbitrary + * unit. Replacement for the android.Util.SizeF which is available only on API >= 21. + */ +open class FSize : Poolable { + // TODO : Encapsulate width & height + var width: Float = 0f + var height: Float = 0f + + override fun instantiate(): FSize { + return FSize(0f, 0f) + } + + constructor() + + constructor(width: Float, height: Float) { + this.width = width + this.height = height + } + + override fun equals(other: Any?): Boolean { + if (other == null) { + return false + } + if (this === other) { + return true + } + if (other is FSize) { + return width == other.width && height == other.height + } + return false + } + + override fun toString(): String { + return width.toString() + "x" + height + } + + /** + * {@inheritDoc} + */ + override fun hashCode(): Int { + return width.toBits() xor height.toBits() + } + + companion object { + private val pool: ObjectPool = ObjectPool.create(256, FSize(0f, 0f)) + + init { + pool.replenishPercentage = 0.5f + } + + + fun getInstance(width: Float, height: Float): FSize { + val result: FSize = pool.get() + result.width = width + result.height = height + return result + } + + fun recycleInstance(instance: FSize) { + pool.recycle(instance) + } + + fun recycleInstances(instances: MutableList) { + pool.recycle(instances) + } + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.java deleted file mode 100644 index 68a2b2cd1..000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.java +++ /dev/null @@ -1,9 +0,0 @@ - -package com.github.mikephil.charting.utils; - -/** - * ViewPortHandler for HorizontalBarChart. - */ -public class HorizontalViewPortHandler extends ViewPortHandler { - -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt new file mode 100644 index 000000000..172e4655e --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/HorizontalViewPortHandler.kt @@ -0,0 +1,3 @@ +package com.github.mikephil.charting.utils + +class HorizontalViewPortHandler : ViewPortHandler() diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.java deleted file mode 100644 index ec7c77347..000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.java +++ /dev/null @@ -1,56 +0,0 @@ - -package com.github.mikephil.charting.utils; - -import java.util.List; - -import androidx.annotation.NonNull; - -/** - * Point encapsulating two double values. - * - * @author Philipp Jahoda - */ -public class MPPointD extends ObjectPool.Poolable { - - private static final ObjectPool pool; - - static { - pool = ObjectPool.create(64, new MPPointD(0,0)); - pool.setReplenishPercentage(0.5f); - } - - public static MPPointD getInstance(double x, double y){ - MPPointD result = pool.get(); - result.x = x; - result.y = y; - return result; - } - - public static void recycleInstance(MPPointD instance){ - pool.recycle(instance); - } - - public static void recycleInstances(List instances){ - pool.recycle(instances); - } - - public double x; - public double y; - - protected ObjectPool.Poolable instantiate(){ - return new MPPointD(0,0); - } - - private MPPointD(double x, double y) { - this.x = x; - this.y = y; - } - - /** - * returns a string representation of the object - */ - @NonNull - public String toString() { - return "MPPointD, x: " + x + ", y: " + y; - } -} \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt new file mode 100644 index 000000000..9b55cf42b --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointD.kt @@ -0,0 +1,42 @@ +package com.github.mikephil.charting.utils + +import com.github.mikephil.charting.utils.ObjectPool.Poolable + +/** + * Point encapsulating two double values. + */ +class MPPointD private constructor(var x: Double, var y: Double) : Poolable() { + override fun instantiate(): MPPointD { + return MPPointD(0.0, 0.0) + } + + /** + * returns a string representation of the object + */ + override fun toString(): String { + return "MPPointD, x: $x, y: $y" + } + + companion object { + private val pool: ObjectPool = ObjectPool.create(64, MPPointD(0.0, 0.0)) + + init { + pool.replenishPercentage = 0.5f + } + + fun getInstance(x: Double, y: Double): MPPointD { + val result: MPPointD = pool.get() + result.x = x + result.y = y + return result + } + + fun recycleInstance(instance: MPPointD) { + pool.recycle(instance) + } + + fun recycleInstances(instances: MutableList) { + pool.recycle(instances) + } + } +} \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.java deleted file mode 100644 index 45f47371f..000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.github.mikephil.charting.utils; - -import android.os.Parcel; -import android.os.Parcelable; - -import java.util.List; - -/** - * Created by Tony Patino on 6/24/16. - */ -public class MPPointF extends ObjectPool.Poolable { - - private static final ObjectPool pool; - - public float x; - public float y; - - static { - pool = ObjectPool.create(32, new MPPointF(0,0)); - pool.setReplenishPercentage(0.5f); - } - - public MPPointF() { - } - - public MPPointF(float x, float y) { - this.x = x; - this.y = y; - } - - public static MPPointF getInstance(float x, float y) { - MPPointF result = pool.get(); - result.x = x; - result.y = y; - return result; - } - - public static MPPointF getInstance() { - return pool.get(); - } - - public static MPPointF getInstance(MPPointF copy) { - MPPointF result = pool.get(); - result.x = copy.x; - result.y = copy.y; - return result; - } - - public static void recycleInstance(MPPointF instance){ - pool.recycle(instance); - } - - public static void recycleInstances(List instances){ - pool.recycle(instances); - } - - public static final Parcelable.Creator CREATOR = new Parcelable.Creator<>() { - /** - * Return a new point from the data in the specified parcel. - */ - public MPPointF createFromParcel(Parcel in) { - MPPointF r = new MPPointF(0, 0); - r.my_readFromParcel(in); - return r; - } - - /** - * Return an array of rectangles of the specified size. - */ - public MPPointF[] newArray(int size) { - return new MPPointF[size]; - } - }; - - /** - * Set the point's coordinates from the data stored in the specified - * parcel. To write a point to a parcel, call writeToParcel(). - * Provided to support older Android devices. - * - * @param in The parcel to read the point's coordinates from - */ - public void my_readFromParcel(Parcel in) { - x = in.readFloat(); - y = in.readFloat(); - } - - public float getX(){ - return this.x; - } - - public float getY(){ - return this.y; - } - - @Override - protected ObjectPool.Poolable instantiate() { - return new MPPointF(0,0); - } -} \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt new file mode 100644 index 000000000..849d44272 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/MPPointF.kt @@ -0,0 +1,85 @@ +package com.github.mikephil.charting.utils + +import android.os.Parcel +import android.os.Parcelable +import com.github.mikephil.charting.utils.ObjectPool.Poolable + +class MPPointF : Poolable { + var x: Float = 0f + var y: Float = 0f + + constructor() + + constructor(x: Float, y: Float) { + this.x = x + this.y = y + } + + /** + * Set the point's coordinates from the data stored in the specified + * parcel. To write a point to a parcel, call writeToParcel(). + * Provided to support older Android devices. + * + * @param in The parcel to read the point's coordinates from + */ + fun my_readFromParcel(`in`: Parcel) { + x = `in`.readFloat() + y = `in`.readFloat() + } + + override fun instantiate(): MPPointF { + return MPPointF(0f, 0f) + } + + companion object { + private var pool: ObjectPool = ObjectPool.Companion.create(32, MPPointF(0f, 0f)) + + init { + pool.replenishPercentage = 0.5f + } + + fun getInstance(x: Float, y: Float): MPPointF { + val result: MPPointF = pool.get() + result.x = x + result.y = y + return result + } + + val instance: MPPointF + get() = pool.get() + + fun getInstance(copy: MPPointF): MPPointF { + val result: MPPointF = pool.get() + result.x = copy.x + result.y = copy.y + return result + } + + @JvmStatic + fun recycleInstance(instance: MPPointF?) { + pool.recycle(instance) + } + + fun recycleInstances(instances: MutableList) { + pool.recycle(instances) + } + + val CREATOR: Parcelable.Creator = object : Parcelable.Creator { + /** + * Return a new point from the data in the specified parcel. + */ + override fun createFromParcel(`in`: Parcel): MPPointF { + val r = MPPointF(0f, 0f) + r.my_readFromParcel(`in`) + return r + } + + /** + * Return an array of rectangles of the specified size. + */ + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } + } +} \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.java deleted file mode 100644 index 6a66f6794..000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.java +++ /dev/null @@ -1,216 +0,0 @@ -package com.github.mikephil.charting.utils; - -import java.util.List; - -/** - * An object pool for recycling of object instances extending Poolable. - *

- *

- * Cost/Benefit : - * Cost - The pool can only contain objects extending Poolable. - * Benefit - The pool can very quickly determine if an object is elligable for storage without iteration. - * Benefit - The pool can also know if an instance of Poolable is already stored in a different pool instance. - * Benefit - The pool can grow as needed, if it is empty - * Cost - However, refilling the pool when it is empty might incur a time cost with sufficiently large capacity. Set the replenishPercentage to a lower number if this is a concern. - * Created by Tony Patino on 6/20/16. - */ -public class ObjectPool { - - private static int ids = 0; - - private int poolId; - private int desiredCapacity; - private Object[] objects; - private int objectsPointer; - private final T modelObject; - private float replenishPercentage; - - - /** - * Returns the id of the given pool instance. - * - * @return an integer ID belonging to this pool instance. - */ - public int getPoolId() { - return poolId; - } - - /** - * Returns an ObjectPool instance, of a given starting capacity, that recycles instances of a given Poolable object. - * - * @param withCapacity A positive integer value. - * @param object An instance of the object that the pool should recycle. - */ - public static synchronized ObjectPool create(int withCapacity, Poolable object) { - ObjectPool result = new ObjectPool(withCapacity, object); - result.poolId = ids; - ids++; - - return result; - } - - private ObjectPool(int withCapacity, T object) { - if (withCapacity <= 0) { - throw new IllegalArgumentException("Object Pool must be instantiated with a capacity greater than 0!"); - } - this.desiredCapacity = withCapacity; - this.objects = new Object[this.desiredCapacity]; - this.objectsPointer = 0; - this.modelObject = object; - this.replenishPercentage = 1.0f; - this.refillPool(); - } - - /** - * Set the percentage of the pool to replenish on empty. Valid values are between - * 0.00f and 1.00f - * - * @param percentage a value between 0 and 1, representing the percentage of the pool to replenish. - */ - public void setReplenishPercentage(float percentage) { - float p = percentage; - if (p > 1) { - p = 1; - } else if (p < 0f) { - p = 0f; - } - this.replenishPercentage = p; - } - - public float getReplenishPercentage() { - return replenishPercentage; - } - - private void refillPool() { - this.refillPool(this.replenishPercentage); - } - - private void refillPool(float percentage) { - int portionOfCapacity = (int) (desiredCapacity * percentage); - - if (portionOfCapacity < 1) { - portionOfCapacity = 1; - } else if (portionOfCapacity > desiredCapacity) { - portionOfCapacity = desiredCapacity; - } - - for (int i = 0; i < portionOfCapacity; i++) { - this.objects[i] = modelObject.instantiate(); - } - objectsPointer = portionOfCapacity - 1; - } - - /** - * Returns an instance of Poolable. If get() is called with an empty pool, the pool will be - * replenished. If the pool capacity is sufficiently large, this could come at a performance - * cost. - * - * @return An instance of Poolable object T - */ - public synchronized T get() { - - if (this.objectsPointer == -1 && this.replenishPercentage > 0.0f) { - this.refillPool(); - } - - T result = (T) objects[this.objectsPointer]; - objects[this.objectsPointer] = null; - result.currentOwnerId = Poolable.NO_OWNER; - this.objectsPointer--; - - return result; - } - - /** - * Recycle an instance of Poolable that this pool is capable of generating. - * The T instance passed must not already exist inside this or any other ObjectPool instance. - * - * @param object An object of type T to recycle - */ - public synchronized void recycle(T object) { - if (object.currentOwnerId != Poolable.NO_OWNER) { - if (object.currentOwnerId == this.poolId) { - throw new IllegalArgumentException("The object passed is already stored in this pool!"); - } else { - throw new IllegalArgumentException("The object to recycle already belongs to poolId " + object.currentOwnerId + ". Object cannot belong to two different pool instances simultaneously!"); - } - } - - this.objectsPointer++; - if (this.objectsPointer >= objects.length) { - this.resizePool(); - } - - object.currentOwnerId = this.poolId; - objects[this.objectsPointer] = object; - - } - - /** - * Recycle a List of Poolables that this pool is capable of generating. - * The T instances passed must not already exist inside this or any other ObjectPool instance. - * - * @param objects A list of objects of type T to recycle - */ - public synchronized void recycle(List objects) { - while (objects.size() + this.objectsPointer + 1 > this.desiredCapacity) { - this.resizePool(); - } - final int objectsListSize = objects.size(); - - // Not relying on recycle(T object) because this is more performant. - for (int i = 0; i < objectsListSize; i++) { - T object = objects.get(i); - if (object.currentOwnerId != Poolable.NO_OWNER) { - if (object.currentOwnerId == this.poolId) { - throw new IllegalArgumentException("The object passed is already stored in this pool!"); - } else { - throw new IllegalArgumentException("The object to recycle already belongs to poolId " + object.currentOwnerId + ". Object cannot belong to two different pool instances simultaneously!"); - } - } - object.currentOwnerId = this.poolId; - this.objects[this.objectsPointer + 1 + i] = object; - } - this.objectsPointer += objectsListSize; - } - - private void resizePool() { - final int oldCapacity = this.desiredCapacity; - this.desiredCapacity *= 2; - Object[] temp = new Object[this.desiredCapacity]; - for (int i = 0; i < oldCapacity; i++) { - temp[i] = this.objects[i]; - } - this.objects = temp; - } - - /** - * Returns the capacity of this object pool. Note : The pool will automatically resize - * to contain additional objects if the user tries to add more objects than the pool's - * capacity allows, but this comes at a performance cost. - * - * @return The capacity of the pool. - */ - public int getPoolCapacity() { - return this.objects.length; - } - - /** - * Returns the number of objects remaining in the pool, for diagnostic purposes. - * - * @return The number of objects remaining in the pool. - */ - public int getPoolCount() { - return this.objectsPointer + 1; - } - - - public static abstract class Poolable { - - public static int NO_OWNER = -1; - int currentOwnerId = NO_OWNER; - - protected abstract Poolable instantiate(); - - } -} \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt new file mode 100644 index 000000000..52301ae5e --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ObjectPool.kt @@ -0,0 +1,182 @@ +package com.github.mikephil.charting.utils + +import com.github.mikephil.charting.utils.ObjectPool.Poolable + +/** + * An object pool for recycling of object instances extending Poolable. + * + * + * Cost/Benefit : + * Cost - The pool can only contain objects extending Poolable. + * Benefit - The pool can very quickly determine if an object is eligible for storage without iteration. + * Benefit - The pool can also know if an instance of Poolable is already stored in a different pool instance. + * Benefit - The pool can grow as needed, if it is empty + * Cost - However, refilling the pool when it is empty might incur a time cost with sufficiently large capacity. Set the replenishPercentage to a lower number if this is a concern. + */ +class ObjectPool> private constructor(withCapacity: Int, poolObject: T?) { + /** + * Returns the id of the given pool instance. + * + * @return an integer ID belonging to this pool instance. + */ + var poolId: Int = 0 + private set + private var desiredCapacity: Int + private val objects = ArrayList(withCapacity) + private val modelObject: T? + var replenishPercentage: Float = 1.0f + set(value) { + var p = value + if (p > 1) { + p = 1f + } else if (p < 0f) { + p = 0f + } + field = p + } + + + init { + require(withCapacity > 0) { "Object Pool must be instantiated with a capacity greater than 0!" } + this.desiredCapacity = withCapacity + this.modelObject = poolObject + this.refillPool() + } + + private fun refillPool(percentage: Float = this.replenishPercentage) { + var portionOfCapacity = (desiredCapacity * percentage).toInt() + + if (portionOfCapacity < 1) { + portionOfCapacity = 1 + } else if (portionOfCapacity > desiredCapacity) { + portionOfCapacity = desiredCapacity + } + + this.objects.clear() + + repeat(portionOfCapacity) { + this.objects.add(modelObject!!.instantiate()) + } + } + + /** + * Returns an instance of Poolable. If get() is called with an empty pool, the pool will be + * replenished. If the pool capacity is sufficiently large, this could come at a performance + * cost. + * + * @return An instance of Poolable object T + */ + @Synchronized + fun get(): T { + if (objects.isEmpty() && this.replenishPercentage > 0.0f) { + this.refillPool() + } + + val result = objects.removeAt(objects.lastIndex) + result.currentOwnerId = Poolable.NO_OWNER + + return result + } + + /** + * Recycle an instance of Poolable that this pool is capable of generating. + * The T instance passed must not already exist inside this or any other ObjectPool instance. + * + * @param object An object of type T to recycle + */ + @Synchronized + fun recycle(`object`: T?) { + if (`object` == null) return + + if (`object`.currentOwnerId != Poolable.NO_OWNER) { + require(`object`.currentOwnerId != this.poolId) { "The object passed is already stored in this pool!" } + throw IllegalArgumentException("The object to recycle already belongs to poolId " + `object`.currentOwnerId + ". Object cannot belong to two different pool instances simultaneously!") + } + + `object`.currentOwnerId = this.poolId + objects.add(`object`) + + if (objects.size > desiredCapacity) { + resizePool() + } + } + + /** + * Recycle a List of Poolables that this pool is capable of generating. + * The T instances passed must not already exist inside this or any other ObjectPool instance. + * + * @param objects A list of objects of type T to recycle + */ + @Synchronized + fun recycle(objects: List) { + val objectsListSize = objects.size + + while (objectsListSize + this.objects.size > this.desiredCapacity) { + resizePool() + } + + // Not relying on recycle(T object) because this is more performant. + for (i in 0..> { + var currentOwnerId: Int = NO_OWNER + + abstract fun instantiate(): T + + companion object { + var NO_OWNER: Int = -1 + } + } + + companion object { + private var ids = 0 + + /** + * Returns an ObjectPool instance, of a given starting capacity, that recycles instances of a given Poolable object. + * + * @param withCapacity A positive integer value. + * @param object An instance of the object that the pool should recycle. + * @return + */ + @Synchronized + fun > create(withCapacity: Int, `object`: T): ObjectPool { + val result: ObjectPool = ObjectPool(withCapacity, `object`) + result.poolId = ids + ids++ + + return result + } + } +} \ No newline at end of file diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java index 661b883c3..f42ac6187 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Transformer.java @@ -355,7 +355,7 @@ public void pixelsToValue(float[] pixels) { */ public MPPointD getValuesByTouchPoint(float x, float y) { - MPPointD result = MPPointD.getInstance(0, 0); + MPPointD result = MPPointD.Companion.getInstance(0, 0); getValuesByTouchPoint(x, y, result); return result; } @@ -367,8 +367,8 @@ public void getValuesByTouchPoint(float x, float y, MPPointD outputPoint) { pixelsToValue(ptsBuffer); - outputPoint.x = ptsBuffer[0]; - outputPoint.y = ptsBuffer[1]; + outputPoint.setX(ptsBuffer[0]); + outputPoint.setY(ptsBuffer[1]); } /** @@ -385,7 +385,7 @@ public MPPointD getPixelForValues(float x, float y) { double xPx = ptsBuffer[0]; double yPx = ptsBuffer[1]; - return MPPointD.getInstance(xPx, yPx); + return MPPointD.Companion.getInstance(xPx, yPx); } public Matrix getValueMatrix() { diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java index 70b4f2635..1b5ec3a1b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java @@ -8,7 +8,6 @@ import android.graphics.drawable.Drawable; import android.text.StaticLayout; import android.text.TextPaint; -import android.util.DisplayMetrics; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.ViewConfiguration; @@ -103,7 +102,7 @@ public static float getLineSpacing(Paint paint, Paint.FontMetrics fontMetrics) { */ public static FSize calcTextSize(Paint paint, String demoText) { - FSize result = FSize.getInstance(0, 0); + FSize result = FSize.Companion.getInstance(0, 0); calcTextSize(paint, demoText, result); return result; } @@ -123,8 +122,8 @@ public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) Rect r = mCalcTextSizeRect; r.set(0, 0, 0, 0); paint.getTextBounds(demoText, 0, demoText.length(), r); - outputFSize.width = r.width(); - outputFSize.height = r.height(); + outputFSize.setWidth(r.width()); + outputFSize.setHeight(r.height()); } @@ -175,14 +174,14 @@ public static float roundToNextSignificant(double number) { * @return */ public static MPPointF getPosition(MPPointF center, float dist, float angle) { - MPPointF p = MPPointF.getInstance(0, 0); + MPPointF p = MPPointF.Companion.getInstance(0, 0); getPosition(center, dist, angle, p); return p; } public static void getPosition(MPPointF center, float dist, float angle, MPPointF outputPoint) { - outputPoint.x = (float) (center.x + dist * Math.cos(Math.toRadians(angle))); - outputPoint.y = (float) (center.y + dist * Math.sin(Math.toRadians(angle))); + outputPoint.setX((float) (center.getX() + dist * Math.cos(Math.toRadians(angle)))); + outputPoint.setY((float) (center.getY() + dist * Math.sin(Math.toRadians(angle)))); } public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, VelocityTracker tracker) { @@ -229,9 +228,9 @@ public static void drawImage(Canvas canvas, Drawable drawable, int x, int y) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); - MPPointF drawOffset = MPPointF.getInstance(); - drawOffset.x = x - (width / 2); - drawOffset.y = y - (height / 2); + MPPointF drawOffset = MPPointF.Companion.getInstance(); + drawOffset.setX(x - (width / 2)); + drawOffset.setY(y - (height / 2)); drawable.copyBounds(mDrawableBoundsCache); drawable.setBounds( @@ -242,7 +241,7 @@ public static void drawImage(Canvas canvas, Drawable drawable, int x, int y) { int saveId = canvas.save(); // translate to the correct position and draw - canvas.translate(drawOffset.x, drawOffset.y); + canvas.translate(drawOffset.getX(), drawOffset.getY()); drawable.draw(canvas); canvas.restoreToCount(saveId); } @@ -282,15 +281,15 @@ public static void drawXAxisValue(Canvas canvas, String text, float x, float y, float translateY = y; // Move the "outer" rect relative to the anchor, assuming its centered - if (anchor.x != 0.5f || anchor.y != 0.5f) { + if (anchor.getX() != 0.5f || anchor.getY() != 0.5f) { final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees( mDrawTextRectBuffer.width(), lineHeight, angleDegrees); - translateX -= rotatedSize.width * (anchor.x - 0.5f); - translateY -= rotatedSize.height * (anchor.y - 0.5f); - FSize.recycleInstance(rotatedSize); + translateX -= rotatedSize.getWidth() * (anchor.getX() - 0.5f); + translateY -= rotatedSize.getHeight() * (anchor.getY() - 0.5f); + FSize.Companion.recycleInstance(rotatedSize); } canvas.save(); @@ -301,10 +300,10 @@ public static void drawXAxisValue(Canvas canvas, String text, float x, float y, canvas.restore(); } else { - if (anchor.x != 0.f || anchor.y != 0.f) { + if (anchor.getX() != 0.f || anchor.getY() != 0.f) { - drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x; - drawOffsetY -= lineHeight * anchor.y; + drawOffsetX -= mDrawTextRectBuffer.width() * anchor.getX(); + drawOffsetY -= lineHeight * anchor.getY(); } drawOffsetX += x; @@ -353,15 +352,15 @@ public static void drawMultilineText(Canvas canvas, StaticLayout textLayout, float translateY = y; // Move the "outer" rect relative to the anchor, assuming its centered - if (anchor.x != 0.5f || anchor.y != 0.5f) { + if (anchor.getX() != 0.5f || anchor.getY() != 0.5f) { final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees( drawWidth, drawHeight, angleDegrees); - translateX -= rotatedSize.width * (anchor.x - 0.5f); - translateY -= rotatedSize.height * (anchor.y - 0.5f); - FSize.recycleInstance(rotatedSize); + translateX -= rotatedSize.getWidth() * (anchor.getX() - 0.5f); + translateY -= rotatedSize.getHeight() * (anchor.getY() - 0.5f); + FSize.Companion.recycleInstance(rotatedSize); } canvas.save(); @@ -373,10 +372,10 @@ public static void drawMultilineText(Canvas canvas, StaticLayout textLayout, canvas.restore(); } else { - if (anchor.x != 0.f || anchor.y != 0.f) { + if (anchor.getX() != 0.f || anchor.getY() != 0.f) { - drawOffsetX -= drawWidth * anchor.x; - drawOffsetY -= drawHeight * anchor.y; + drawOffsetX -= drawWidth * anchor.getX(); + drawOffsetY -= drawHeight * anchor.getY(); } drawOffsetX += x; @@ -417,7 +416,7 @@ public static FSize getSizeOfRotatedRectangleByDegrees(float rectangleWidth, flo * @return A Recyclable FSize instance */ public static FSize getSizeOfRotatedRectangleByRadians(float rectangleWidth, float rectangleHeight, float radians) { - return FSize.getInstance( + return FSize.Companion.getInstance( Math.abs(rectangleWidth * (float) Math.cos(radians)) + Math.abs(rectangleHeight * (float) Math.sin(radians)), Math.abs(rectangleWidth * (float) Math.sin(radians)) + Math.abs(rectangleHeight * (float) Math.cos(radians)) ); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ViewPortHandler.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ViewPortHandler.kt index 54efa7e82..d5d83b257 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ViewPortHandler.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/ViewPortHandler.kt @@ -319,7 +319,7 @@ open class ViewPortHandler { * @param view * @return save */ - fun centerViewPort(transformedPts: FloatArray, view: View) { + fun centerViewPort(transformedPts: FloatArray, view: View?) { val save = mCenterViewPortMatrixBuffer save.reset() save.set(matrixTouch) @@ -342,13 +342,14 @@ open class ViewPortHandler { * * @param newMatrix */ - fun refresh(newMatrix: Matrix, chart: View, invalidate: Boolean): Matrix { + fun refresh(newMatrix: Matrix, view: View?, invalidate: Boolean): Matrix { matrixTouch.set(newMatrix) // make sure scale and translation are within their bounds limitTransAndScale(matrixTouch, contentRect) - if (invalidate) chart.invalidate() + if (invalidate) + view?.invalidate() newMatrix.set(matrixTouch) return newMatrix diff --git a/MPChartLib/src/test/kotlin/com/github/mikephil/charting/test/ObjectPoolTest.kt b/MPChartLib/src/test/kotlin/com/github/mikephil/charting/test/ObjectPoolTest.kt index bb390d30e..31eef103b 100644 --- a/MPChartLib/src/test/kotlin/com/github/mikephil/charting/test/ObjectPoolTest.kt +++ b/MPChartLib/src/test/kotlin/com/github/mikephil/charting/test/ObjectPoolTest.kt @@ -6,13 +6,13 @@ import org.junit.Assert import org.junit.Test class ObjectPoolTest { - internal class TestPoolable private constructor(var foo: Int, var bar: Int) : Poolable() { - override fun instantiate(): Poolable { + internal class TestPoolable private constructor(var foo: Int, var bar: Int) : Poolable() { + override fun instantiate(): TestPoolable { return TestPoolable(0, 0) } companion object { - private val pool: ObjectPool = ObjectPool.create(4, TestPoolable(0, 0)) as ObjectPool + private val pool: ObjectPool = ObjectPool.create(4, TestPoolable(0, 0)) fun getInstance(foo: Int, bar: Int): TestPoolable { val result = pool.get() @@ -25,7 +25,7 @@ class ObjectPoolTest { pool.recycle(instance) } - fun recycleInstances(instances: List?) { + fun recycleInstances(instances: List) { pool.recycle(instances) } @@ -212,4 +212,4 @@ class ObjectPoolTest { Assert.assertEquals(16, poolCapacity) Assert.assertEquals(7, poolCount) } -} +} \ No newline at end of file