1 | package testcode;
|
---|
2 |
|
---|
3 | import java.util.function.Function;
|
---|
4 |
|
---|
5 | import org.eclipse.jdt.annotation.NonNull;
|
---|
6 |
|
---|
7 | /**
|
---|
8 | * Basic object functions that have special translations: hashcode, equals,
|
---|
9 | * toString.
|
---|
10 | *
|
---|
11 | */
|
---|
12 | public class ObjectFunctions {
|
---|
13 | private final int a;
|
---|
14 |
|
---|
15 | public ObjectFunctions(int a) {
|
---|
16 | this.a = a;
|
---|
17 | }
|
---|
18 |
|
---|
19 | @Override
|
---|
20 | public @NonNull String toString() {
|
---|
21 | return Integer.toString(a); // aotoboxing not yet supported
|
---|
22 | }
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | public int hashCode() {
|
---|
26 | return a;
|
---|
27 | }
|
---|
28 |
|
---|
29 | @Override
|
---|
30 | public boolean equals(Object obj) {
|
---|
31 | if (this == obj)
|
---|
32 | return true;
|
---|
33 | if (obj == null)
|
---|
34 | return false;
|
---|
35 | if (getClass() != obj.getClass())
|
---|
36 | return false;
|
---|
37 | final @NonNull ObjectFunctions other = (ObjectFunctions) obj;
|
---|
38 | return a == other.a;
|
---|
39 | }
|
---|
40 |
|
---|
41 | private static String method1(String x) {
|
---|
42 | return x;
|
---|
43 | }
|
---|
44 |
|
---|
45 | private String method2(String x) {
|
---|
46 | return x;
|
---|
47 | }
|
---|
48 |
|
---|
49 | private String method2call() {
|
---|
50 | Function<String, String> method = this::method2;
|
---|
51 | return method.apply("ok2");
|
---|
52 | }
|
---|
53 |
|
---|
54 | public static void main(String[] args) {
|
---|
55 | // 'final' should be ignored as not supported in python.
|
---|
56 | final @NonNull ObjectFunctions v = new ObjectFunctions(12);
|
---|
57 | final @NonNull ObjectFunctions v2 = new ObjectFunctions(12);
|
---|
58 | System.out.println(v.toString());
|
---|
59 | System.out.println(Boolean.toString(v.equals(v2)));
|
---|
60 | System.out.println(Integer.toString(v.hashCode()));
|
---|
61 | // 12True12 . We avoid autoboxing - not supported
|
---|
62 | // notice that in java this will print 12true12 -- LOWER CASE T.
|
---|
63 | Function<String, String> method = ObjectFunctions::method1;
|
---|
64 | System.out.println(method.apply("ok1"));
|
---|
65 | System.out.println(v.method2call());
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|