Skip to content

Commit e05bb70

Browse files
committed
Processing core Pattern Matching instanceof
1 parent 6bfe04f commit e05bb70

15 files changed

+233
-190
lines changed

src/main/java/monkstone/FastNoiseModuleJava.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ public static void createNoiseModule(Ruby runtime) {
3030
RubyModule noiseModule = runtime.defineModule("FastNoise");
3131
noiseModule.defineAnnotatedMethods(FastNoiseModuleJava.class);
3232
}
33+
34+
35+
/**
36+
* Utility method
37+
*
38+
* @param obj
39+
* @return parse float value of object or zero
40+
*/
41+
private static double jvalue(IRubyObject obj) {
42+
if (obj instanceof RubyFloat rubyFloat) {
43+
return rubyFloat.getValue();
44+
}
45+
if (obj instanceof RubyFixnum rubyFixnum) {
46+
return rubyFixnum.getDoubleValue();
47+
}
48+
return 0;
49+
}
3350

3451
/**
3552
*
@@ -46,21 +63,21 @@ public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject re
4663
double four;
4764
double result = switch (args.length) {
4865
case 2 -> {
49-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
50-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
66+
two = jvalue(args[1]);
67+
one = jvalue(args[0]);
5168
yield ng.noise2_XBeforeY(one, two);
5269
}
5370
case 3 -> {
54-
three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
55-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
56-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
71+
three = jvalue(args[2]);
72+
two = jvalue(args[1]);
73+
one = jvalue(args[0]);
5774
yield ng.noise3_XYBeforeZ(one, two, three);
5875
}
5976
case 4 -> {
60-
four = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
61-
three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
62-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
63-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
77+
four = jvalue(args[3]);
78+
three = jvalue(args[2]);
79+
two = jvalue(args[1]);
80+
one = jvalue(args[0]);
6481
yield ng.noise4_XYBeforeZW(one, two, three, four);
6582
}
6683
default -> { yield 2; } // yield an invalid value for noise
@@ -87,25 +104,25 @@ public static IRubyObject noiseImpl(ThreadContext context, IRubyObject recv, IRu
87104
double four;
88105
double result = switch (args.length) {
89106
case 1 -> {
90-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
107+
one = jvalue(args[0]);
91108
yield ng.noise2(one, 0);
92109
}
93110
case 2 -> {
94-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
95-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
111+
two = jvalue(args[1]);
112+
one = jvalue(args[0]);
96113
yield ng.noise2(one, two);
97114
}
98115
case 3 -> {
99-
three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
100-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
101-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
116+
three = jvalue(args[2]);
117+
two = jvalue(args[1]);
118+
one = jvalue(args[0]);
102119
yield ng.noise3_Classic(one, two, three);
103120
}
104121
case 4 -> {
105-
four = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
106-
three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
107-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
108-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
122+
four = jvalue(args[3]);
123+
three = jvalue(args[2]);
124+
two = jvalue(args[1]);
125+
one = jvalue(args[0]);
109126
yield ng.noise4_Classic(one, two, three, four);
110127
}
111128
default -> { yield 2; } // yield an invalid value for noise

src/main/java/monkstone/MathToolModule.java

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ public static void createMathToolModule(Ruby runtime) {
3737
RubyModule mtModule = runtime.defineModule("MathTool");
3838
mtModule.defineAnnotatedMethods(MathToolModule.class);
3939
}
40+
41+
/**
42+
* Utility method
43+
*
44+
* @param obj
45+
* @return parse float value of object or zero
46+
*/
47+
private static double jvalue(IRubyObject obj) {
48+
if (obj instanceof RubyFloat rubyFloat) {
49+
return rubyFloat.getValue();
50+
}
51+
if (obj instanceof RubyFixnum rubyFixnum) {
52+
return rubyFixnum.getDoubleValue();
53+
}
54+
return 0;
55+
}
4056

4157
/**
4258
*
@@ -47,18 +63,13 @@ public static void createMathToolModule(Ruby runtime) {
4763
*/
4864
@JRubyMethod(name = "map1d", rest = true, module = true)
4965
public static IRubyObject mapOneD(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
50-
double value = args[0] instanceof RubyFloat
51-
? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
66+
double value = jvalue(args[0]);
5267
RubyRange r1 = (RubyRange) args[1];
5368
RubyRange r2 = (RubyRange) args[2];
54-
double first1 = r1.first(context) instanceof RubyFloat
55-
? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue();
56-
double first2 = r2.first(context) instanceof RubyFloat
57-
? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue();
58-
double last1 = r1.last(context) instanceof RubyFloat
59-
? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue();
60-
double last2 = r2.last(context) instanceof RubyFloat
61-
? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue();
69+
double first1 = jvalue(r1.first(context));
70+
double first2 = jvalue(r2.first(context));
71+
double last1 = jvalue(r1.last(context));
72+
double last2 = jvalue(r2.last(context));
6273
return mapMt(context, value, first1, last1, first2, last2);
6374
}
6475

@@ -71,17 +82,13 @@ public static IRubyObject mapOneD(ThreadContext context, IRubyObject recv, IRuby
7182
*/
7283
@JRubyMethod(name = "constrained_map", rest = true, module = true)
7384
public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
74-
double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
85+
double value = jvalue(args[0]);
7586
RubyRange r1 = (RubyRange) args[1];
7687
RubyRange r2 = (RubyRange) args[2];
77-
double first1 = r1.first(context) instanceof RubyFloat
78-
? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue();
79-
double first2 = r2.first(context) instanceof RubyFloat
80-
? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue();
81-
double last1 = r1.last(context) instanceof RubyFloat
82-
? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue();
83-
double last2 = r2.last(context) instanceof RubyFloat
84-
? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue();
88+
double first1 = jvalue(r1.first(context));
89+
double first2 = jvalue(r2.first(context));
90+
double last1 = jvalue(r1.last(context));
91+
double last2 = jvalue(r2.last(context));
8592
double max = Math.max(first1, last1);
8693
double min = Math.min(first1, last1);
8794
if (value < min) {
@@ -102,11 +109,11 @@ public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv
102109
*/
103110
@JRubyMethod(name = "p5map", rest = true, module = true)
104111
public static IRubyObject mapProcessing(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
105-
double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
106-
double first1 = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
107-
double first2 = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
108-
double last1 = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
109-
double last2 = args[4] instanceof RubyFloat ? ((RubyFloat) args[4]).getValue() : ((RubyFixnum) args[4]).getDoubleValue();
112+
double value = jvalue(args[0]);
113+
double first1 = jvalue(args[1]);
114+
double first2 = jvalue(args[3]);
115+
double last1 = jvalue(args[2]);
116+
double last2 = jvalue(args[4]);
110117
return mapMt(context, value, first1, last1, first2, last2);
111118
}
112119

@@ -121,9 +128,9 @@ public static IRubyObject mapProcessing(ThreadContext context, IRubyObject recv,
121128
*/
122129
@JRubyMethod(name = "lerp", rest = true, module = true)
123130
public static IRubyObject lerpP(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
124-
double start = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
125-
double stop = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
126-
double amount = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
131+
double start = jvalue(args[0]);
132+
double stop = jvalue(args[1]);
133+
double amount = jvalue(args[2]);
127134
if (amount <= 0) {
128135
return args[0];
129136
}
@@ -145,9 +152,9 @@ public static IRubyObject lerpP(ThreadContext context, IRubyObject recv, IRubyOb
145152
*/
146153
@JRubyMethod(name = "norm", rest = true, module = true)
147154
public static IRubyObject normP(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
148-
double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
149-
double start = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
150-
double stop = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
155+
double value = jvalue(args[0]);
156+
double start = jvalue(args[1]);
157+
double stop = jvalue(args[2]);
151158
return mapMt(context, value, start, stop, 0, 1.0);
152159
}
153160

@@ -162,10 +169,9 @@ public static IRubyObject normP(ThreadContext context, IRubyObject recv, IRubyOb
162169
*/
163170
@JRubyMethod(name = "norm_strict", rest = true, module = true)
164171
public static IRubyObject norm_strict(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
165-
166-
double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
167-
double start = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
168-
double stop = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
172+
double value = jvalue(args[0]);
173+
double start = jvalue(args[1]);
174+
double stop = jvalue(args[2]);
169175
double max = Math.max(start, stop);
170176
double min = Math.min(start, stop);
171177
if (value < min) {

src/main/java/monkstone/SmoothNoiseModuleJava.java

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ public static void createNoiseModule(Ruby runtime) {
3030
RubyModule noiseModule = runtime.defineModule("SmoothNoise");
3131
noiseModule.defineAnnotatedMethods(SmoothNoiseModuleJava.class);
3232
}
33+
34+
/**
35+
* Utility method
36+
*
37+
* @param obj
38+
* @return parse float value of object or zero
39+
*/
40+
private static double jvalue(IRubyObject obj) {
41+
if (obj instanceof RubyFloat rubyFloat) {
42+
return rubyFloat.getValue();
43+
}
44+
if (obj instanceof RubyFixnum rubyFixnum) {
45+
return rubyFixnum.getDoubleValue();
46+
}
47+
return 0;
48+
}
3349

3450
/**
3551
*
@@ -46,21 +62,21 @@ public static IRubyObject terrainNoiseImpl(ThreadContext context, IRubyObject re
4662
double four;
4763
double result = switch (args.length) {
4864
case 2 -> {
49-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
50-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
65+
two = jvalue(args[1]);
66+
one = jvalue(args[0]);
5167
yield ng.noise2_XBeforeY(one, two);
5268
}
5369
case 3 -> {
54-
three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
55-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
56-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
70+
three = jvalue(args[2]);
71+
two = jvalue(args[1]);
72+
one = jvalue(args[0]);
5773
yield ng.noise3_XYBeforeZ(one, two, three);
5874
}
5975
case 4 -> {
60-
four = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
61-
three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
62-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
63-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
76+
four = jvalue(args[3]);
77+
three = jvalue(args[2]);
78+
two = jvalue(args[1]);
79+
one = jvalue(args[0]);
6480
yield ng.noise4_XYBeforeZW(one, two, three, four);
6581
}
6682
default -> {
@@ -90,25 +106,25 @@ public static IRubyObject noiseImpl(ThreadContext context, IRubyObject recv, IRu
90106
double four;
91107
double result = switch (args.length) {
92108
case 1 -> {
93-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
109+
one = jvalue(args[0]);
94110
yield ng.noise2(one, 0);
95111
}
96112
case 2 -> {
97-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
98-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
113+
two = jvalue(args[1]);
114+
one = jvalue(args[0]);
99115
yield ng.noise2(one, two);
100116
}
101117
case 3 -> {
102-
three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
103-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
104-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
118+
three = jvalue(args[2]);
119+
two = jvalue(args[1]);
120+
one = jvalue(args[0]);
105121
yield ng.noise3_Classic(one, two, three);
106122
}
107123
case 4 -> {
108-
four = args[3] instanceof RubyFloat ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue();
109-
three = args[2] instanceof RubyFloat ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue();
110-
two = args[1] instanceof RubyFloat ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
111-
one = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
124+
four = jvalue(args[3]);
125+
three = jvalue(args[2]);
126+
two = jvalue(args[1]);
127+
one = jvalue(args[0]);
112128
yield ng.noise4_Classic(one, two, three, four);
113129
}
114130
default -> {

src/main/java/processing/awt/PImageAWT.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public class PImageAWT extends PImage {
6262
*/
6363
public PImageAWT(Image img) {
6464
format = RGB;
65-
if (img instanceof BufferedImage) {
66-
BufferedImage bi = (BufferedImage) img;
65+
if (img instanceof BufferedImage bi) {
6766
width = bi.getWidth();
6867
height = bi.getHeight();
6968
int type = bi.getType();
@@ -78,8 +77,8 @@ public PImageAWT(Image img) {
7877
}
7978
} else {
8079
DataBuffer db = bi.getRaster().getDataBuffer();
81-
if (db instanceof DataBufferInt) {
82-
pixels = ((DataBufferInt) db).getData();
80+
if (db instanceof DataBufferInt dataBufferInt) {
81+
pixels = dataBufferInt.getData();
8382
if (type == BufferedImage.TYPE_INT_ARGB) {
8483
format = ARGB;
8584
} else if (type == BufferedImage.TYPE_INT_RGB) {

src/main/java/processing/awt/PShapeJava2D.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public PShapeJava2D(PShapeSVG parent, XML properties, boolean parseKids) {
6262
protected void setParent(PShapeSVG parent) {
6363
super.setParent(parent);
6464

65-
if (parent instanceof PShapeJava2D) {
66-
PShapeJava2D pj = (PShapeJava2D) parent;
65+
if (parent instanceof PShapeJava2D pj) {
6766
fillGradientPaint = pj.fillGradientPaint;
6867
strokeGradientPaint = pj.strokeGradientPaint;
6968

@@ -310,16 +309,14 @@ public Raster getRaster(int x, int y, int w, int h) {
310309

311310

312311
protected Paint calcGradientPaint(Gradient gradient) {
313-
if (gradient instanceof LinearGradient) {
312+
if (gradient instanceof LinearGradient grad) {
314313
// System.out.println("creating linear gradient");
315-
LinearGradient grad = (LinearGradient) gradient;
316-
return new LinearGradientPaint(grad.x1, grad.y1, grad.x2, grad.y2,
314+
return new LinearGradientPaint(grad.x1, grad.y1, grad.x2, grad.y2,
317315
grad.offset, grad.color, grad.count,
318316
opacity);
319317

320-
} else if (gradient instanceof RadialGradient) {
318+
} else if (gradient instanceof RadialGradient grad) {
321319
// System.out.println("creating radial gradient");
322-
RadialGradient grad = (RadialGradient) gradient;
323320
return new RadialGradientPaint(grad.cx, grad.cy, grad.r,
324321
grad.offset, grad.color, grad.count,
325322
opacity);

src/main/java/processing/awt/PSurfaceAWT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ public void initFrame(final PApplet sketch) {/*, int backgroundColor,
505505
// backgroundColor = WINDOW_BGCOLOR;
506506
// }
507507
final Color windowColor = new Color(sketch.sketchWindowColor(), false);
508-
if (frame instanceof JFrame) {
509-
((JFrame) frame).getContentPane().setBackground(windowColor);
508+
if (frame instanceof JFrame jframe) {
509+
jframe.getContentPane().setBackground(windowColor);
510510
} else {
511511
frame.setBackground(windowColor);
512512
}

0 commit comments

Comments
 (0)