From 9abc45b9a892c082ec35c4efa7b8c4afc750e01e Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 8 Apr 2017 17:11:50 +0200 Subject: [PATCH] convert MyPath to kotlin --- .../com/simplemobiletools/draw/MyPath.java | 92 ------------------- .../com/simplemobiletools/draw/MyPath.kt | 83 +++++++++++++++++ 2 files changed, 83 insertions(+), 92 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/draw/MyPath.java create mode 100644 app/src/main/kotlin/com/simplemobiletools/draw/MyPath.kt diff --git a/app/src/main/java/com/simplemobiletools/draw/MyPath.java b/app/src/main/java/com/simplemobiletools/draw/MyPath.java deleted file mode 100644 index 41e7baa..0000000 --- a/app/src/main/java/com/simplemobiletools/draw/MyPath.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.simplemobiletools.draw; - -import android.graphics.Path; - -import com.simplemobiletools.draw.actions.Action; -import com.simplemobiletools.draw.actions.Line; -import com.simplemobiletools.draw.actions.Move; -import com.simplemobiletools.draw.actions.Quad; - -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.Serializable; -import java.security.InvalidParameterException; -import java.util.LinkedList; -import java.util.List; - -// https://stackoverflow.com/a/8127953 -class MyPath extends Path implements Serializable { - - private List actions = new LinkedList<>(); - - private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { - in.defaultReadObject(); - - for (Action action : actions) { - action.perform(this); - } - } - - public void readObject(String pathData) throws InvalidParameterException { - String[] tokens = pathData.split("\\s+"); - for (int i = 0; i < tokens.length; ++i) { - switch (tokens[i].charAt(0)) { - case 'M': - addAction(new Move(tokens[i])); - break; - case 'L': - addAction(new Line(tokens[i])); - break; - case 'Q': - // Quad actions are of the following form: - // "Qx1,y1 x2,y2" - // Since we split the tokens by whitespace, we need to join them again - if (i+1 >= tokens.length) - throw new InvalidParameterException("Error parsing the data for a Quad."); - - addAction(new Quad(tokens[i]+" "+tokens[i+1])); - ++i; - break; - } - } - } - - @Override - public void reset() { - actions.clear(); - super.reset(); - } - - private void addAction(Action action) { - if (action instanceof Move) { - moveTo(((Move) action).getX(), ((Move) action).getY()); - } else if (action instanceof Line) { - lineTo(((Line) action).getX(), ((Line) action).getY()); - } else if (action instanceof Quad) { - final Quad q = (Quad)action; - quadTo(q.getX1(), q.getY1(), q.getX2(), q.getY2()); - } - } - - @Override - public void moveTo(float x, float y) { - actions.add(new Move(x, y)); - super.moveTo(x, y); - } - - @Override - public void lineTo(float x, float y) { - actions.add(new Line(x, y)); - super.lineTo(x, y); - } - - @Override - public void quadTo(float x1, float y1, float x2, float y2) { - actions.add(new Quad(x1, y1, x2, y2)); - super.quadTo(x1, y1, x2, y2); - } - - List getActions() { - return actions; - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/MyPath.kt b/app/src/main/kotlin/com/simplemobiletools/draw/MyPath.kt new file mode 100644 index 0000000..007f626 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/draw/MyPath.kt @@ -0,0 +1,83 @@ +package com.simplemobiletools.draw + +import android.graphics.Path +import com.simplemobiletools.draw.actions.Action +import com.simplemobiletools.draw.actions.Line +import com.simplemobiletools.draw.actions.Move +import com.simplemobiletools.draw.actions.Quad +import java.io.IOException +import java.io.ObjectInputStream +import java.io.Serializable +import java.security.InvalidParameterException +import java.util.* + +// https://stackoverflow.com/a/8127953 +internal class MyPath : Path(), Serializable { + + private val actions = LinkedList() + + @Throws(IOException::class, ClassNotFoundException::class) + private fun readObject(inputStream: ObjectInputStream) { + inputStream.defaultReadObject() + + for (action in actions) { + action.perform(this) + } + } + + @Throws(InvalidParameterException::class) + fun readObject(pathData: String) { + val tokens = pathData.split("\\s+".toRegex()).dropLastWhile(String::isEmpty).toTypedArray() + var i = 0 + while (i < tokens.size) { + when (tokens[i][0]) { + 'M' -> addAction(Move(tokens[i])) + 'L' -> addAction(Line(tokens[i])) + 'Q' -> { + // Quad actions are of the following form: + // "Qx1,y1 x2,y2" + // Since we split the tokens by whitespace, we need to join them again + if (i + 1 >= tokens.size) + throw InvalidParameterException("Error parsing the data for a Quad.") + + addAction(Quad(tokens[i] + " " + tokens[i + 1])) + ++i + } + } + ++i + } + } + + override fun reset() { + actions.clear() + super.reset() + } + + private fun addAction(action: Action) { + if (action is Move) { + moveTo(action.x, action.y) + } else if (action is Line) { + lineTo(action.x, action.y) + } else if (action is Quad) { + val q = action + quadTo(q.x1, q.y1, q.x2, q.y2) + } + } + + override fun moveTo(x: Float, y: Float) { + actions.add(Move(x, y)) + super.moveTo(x, y) + } + + override fun lineTo(x: Float, y: Float) { + actions.add(Line(x, y)) + super.lineTo(x, y) + } + + override fun quadTo(x1: Float, y1: Float, x2: Float, y2: Float) { + actions.add(Quad(x1, y1, x2, y2)) + super.quadTo(x1, y1, x2, y2) + } + + fun getActions() = actions +}