java - Java8 using enum values in interface's default methods -
i'm exploring possibilities of static , default methods introduced in java 8.
i've interface has 2 default methods construct command, run on server through ssh simple tasks remotely. moving mouse requires 2 arguments: x , y position of mouse.
public interface robot { default string movemouse(int x, int y) { return constructcmd("java -jar move_mouse.jar " + x + " " + y); } default string clickleft() { return constructcmd("java -jar click_left.jar"); } static string constructcmd(string cmd) { return "export display=:1.0\n" + "cd desktop\n" + cmd; } }
i've multiple enums values preset, potently combine enums 1 , not use interface ever, enum contain hundreds or thousands of values , want keep organised, i've split evertying in multiple enums.
i want enums share same methods figured i'll give default methods in interface shot.
public enum field implements robot { age_field(778, 232), name_field(662, 280); public int x; public int y; field(int x, int y) { this.x = x; this.y = y; } }
so can string commands by:
field.age_field.clickleft(); field.age_field.movemouse(field.age_field.x, field.age_field.y);
however movemouse looks bad me , think should somehow possible use enum's values default.
anyone has a nice solution such problem?
the problem architecture. on 1 hand, have layer executes mouse-movement (represented robot
interface). now, need layer produces mouse-movement , sends robot
execute mouse-movement. let's call interface defining layer mousetarget
(fits example nicely):
public interface mousetarget { public int gettargetx(); public int gettargety(); public default void movemousehere(robot robot) { robot.movemouse(this.gettargetx(), this.gettargety()); } }
this interface represents 1 mouse-movement 1 target. see movemousehere(robot robot)
method expects robot
send movement (which actual work). now, left adapt fields
enum:
public enum fields implements mousetarget { age_field(778, 232), name_field(662, 280); public int targetx; public int targety; fields(int targetx, int targety) { this.targetx = targetx; this.targety = targety; } @override public int gettargetx() { return (this.targetx); } @override public int gettargety() { return (this.targety); } }
Comments
Post a Comment