convert MyPath to kotlin

This commit is contained in:
tibbi 2017-04-08 17:11:50 +02:00
parent ef19eafa1f
commit 9abc45b9a8
2 changed files with 83 additions and 92 deletions

View File

@ -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<Action> 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<Action> getActions() {
return actions;
}
}

View File

@ -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<Action>()
@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
}