Skip to content

Commit 745e298

Browse files
committed
adds Java9 Objects.requiresNonNullElse
1 parent 6a57663 commit 745e298

File tree

4 files changed

+166
-50
lines changed

4 files changed

+166
-50
lines changed

sources/net.sf.j2s.java.core/src/java/util/Objects.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,44 @@ public static <T> T requireNonNull(T obj) {
204204
return obj;
205205
}
206206

207+
/**
208+
* Returns the first argument if it is non-{@code null} and
209+
* otherwise returns the non-{@code null} second argument.
210+
*
211+
* @param obj an object
212+
* @param defaultObj a non-{@code null} object to return if the first argument
213+
* is {@code null}
214+
* @param <T> the type of the reference
215+
* @return the first argument if it is non-{@code null} and
216+
* otherwise the second argument if it is non-{@code null}
217+
* @throws NullPointerException if both {@code obj} is null and
218+
* {@code defaultObj} is {@code null}
219+
* @since 9
220+
*/
221+
public static <T> T requireNonNullElse(T obj, T defaultObj) {
222+
return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj");
223+
}
224+
225+
/**
226+
* Returns the first argument if it is non-{@code null} and otherwise
227+
* returns the non-{@code null} value of {@code supplier.get()}.
228+
*
229+
* @param obj an object
230+
* @param supplier of a non-{@code null} object to return if the first argument
231+
* is {@code null}
232+
* @param <T> the type of the first argument and return type
233+
* @return the first argument if it is non-{@code null} and otherwise
234+
* the value from {@code supplier.get()} if it is non-{@code null}
235+
* @throws NullPointerException if both {@code obj} is null and
236+
* either the {@code supplier} is {@code null} or
237+
* the {@code supplier.get()} value is {@code null}
238+
* @since 9
239+
*/
240+
public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) {
241+
return (obj != null) ? obj
242+
: requireNonNull(requireNonNull(supplier, "supplier").get(), "supplier.get()");
243+
}
244+
207245
/**
208246
* Checks that the specified object reference is not {@code null} and
209247
* throws a customized {@link NullPointerException} if it is. This method
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package test;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
import java.beans.PropertyChangeEvent;
6+
import java.beans.PropertyChangeListener;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.Comparator;
10+
import java.util.Date;
11+
import java.util.List;
12+
import java.util.function.BiPredicate;
13+
import java.util.function.Consumer;
14+
import java.util.function.Function;
15+
import java.util.function.Predicate;
16+
import java.util.function.ToIntFunction;
17+
import java.util.stream.DoubleStream;
18+
import java.util.stream.IntStream;
19+
import java.util.stream.Stream;
20+
21+
import javax.swing.JButton;
22+
import javax.swing.SwingUtilities;
23+
import javax.swing.Timer;
24+
25+
import test.baeldung.doublecolon.Computer;
26+
import test.baeldung.doublecolon.MacbookPro;
27+
28+
public class Test_J8_lambdafinal extends Test_ {
29+
30+
31+
public static void main(String args[]) {
32+
33+
testFinal();
34+
}
35+
36+
private static void testFinal() {
37+
38+
Runnable r = () -> {
39+
for (String s : new String[] { "a", "b" }) {
40+
Runnable r1 = () -> {
41+
for (int i : new int[] { 0, 1, 2 }) {
42+
System.out.println(i + " " + s);
43+
}
44+
45+
};
46+
r1.run();
47+
}
48+
};
49+
r.run();
50+
}
51+
52+
}

sources/net.sf.j2s.java.core/src/test/Test_Java8.java

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,58 @@ public void actionPerformed(ActionEvent e) {
221221
});
222222
}
223223

224+
private static void checkData(Object data) {
225+
double[] datae = data instanceof String[] ? Stream.of((String[]) data).mapToDouble(Double::parseDouble).toArray() :
226+
IntStream.of((int[]) data).mapToDouble(i ->i).toArray();
227+
System.out.println(Arrays.toString(datae));
228+
229+
}
230+
231+
public DoubleStream values() {
232+
return IntStream.range(0, 4)
233+
.mapToDouble(this::get);
234+
}
235+
236+
237+
public double get(int row) {
238+
return row;
239+
}
240+
241+
242+
private static void testToIntFunction(TestFunc tf, Function<TestFunc, Integer> f) {
243+
int val = f.apply(tf);
244+
System.out.println("testToIntFunction =" + val);
245+
}
246+
247+
private static void testToIntFunction(Object tf) {
248+
System.out.println("testToIntFunction = " + tf);
249+
}
250+
251+
private static void testFunction(String function, int i) {
252+
253+
// Function
254+
255+
int[][][] a;
256+
257+
Function<Integer, int[][][]> ifi = new Function<Integer, int[][][]>() {
258+
259+
@Override
260+
public int[][][] apply(Integer t) {
261+
System.out.println(function);
262+
return new int[t.intValue()][][];
263+
}
264+
};
265+
266+
a = ifi.apply(i);
267+
System.out.println("a length is " + a.length);
268+
269+
}
270+
271+
@Override
272+
public void propertyChange(PropertyChangeEvent evt) {
273+
// checking here that this is not qualified
274+
}
275+
224276
public static void main(String[] args) {
225277

226278
Test_Java8 t8b = new Test_Java8();
@@ -415,55 +467,5 @@ public void run() {
415467
System.out.println("Test_Java8 OK");
416468
}
417469

418-
private static void checkData(Object data) {
419-
double[] datae = data instanceof String[] ? Stream.of((String[]) data).mapToDouble(Double::parseDouble).toArray() :
420-
IntStream.of((int[]) data).mapToDouble(i ->i).toArray();
421-
System.out.println(Arrays.toString(datae));
422-
423-
}
424-
425-
public DoubleStream values() {
426-
return IntStream.range(0, 4)
427-
.mapToDouble(this::get);
428-
}
429-
430-
431-
public double get(int row) {
432-
return row;
433-
}
434470

435-
436-
private static void testToIntFunction(TestFunc tf, Function<TestFunc, Integer> f) {
437-
int val = f.apply(tf);
438-
System.out.println("testToIntFunction =" + val);
439-
}
440-
441-
private static void testToIntFunction(Object tf) {
442-
System.out.println("testToIntFunction = " + tf);
443-
}
444-
445-
private static void testFunction(String function, int i) {
446-
447-
// Function
448-
449-
int[][][] a;
450-
451-
Function<Integer, int[][][]> ifi = new Function<Integer, int[][][]>() {
452-
453-
@Override
454-
public int[][][] apply(Integer t) {
455-
System.out.println(function);
456-
return new int[t.intValue()][][];
457-
}
458-
};
459-
460-
a = ifi.apply(i);
461-
System.out.println("a length is " + a.length);
462-
463-
}
464-
465-
@Override
466-
public void propertyChange(PropertyChangeEvent evt) {
467-
// checking here that this is not qualified
468-
}
469471
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test;
2+
3+
import java.util.Objects;
4+
5+
class Test_Objects extends Test_ {
6+
7+
@SuppressWarnings("unused")
8+
public static void main(String[] args) {
9+
10+
Object x = "";
11+
Objects.requireNonNull(x);
12+
if (/** @j2sNative true || */
13+
false) {
14+
x = "ok";
15+
x = Objects.requireNonNullElse(x, "testing");
16+
assert (x == "ok");
17+
x = null;
18+
x = Objects.requireNonNullElse(x, "testing");
19+
assert (x == "testing");
20+
}
21+
System.out.println("Test_Objects OK");
22+
}
23+
24+
}

0 commit comments

Comments
 (0)