forked from kenta-shimizu/secs4java8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleGetSecs2Value.java
149 lines (123 loc) · 5.53 KB
/
ExampleGetSecs2Value.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package example4;
import java.util.Arrays;
import com.shimizukenta.secs.secs2.Secs2;
import com.shimizukenta.secs.secs2.Secs2Exception;
/**
* Example-3.
*
* <p>
* This is getting SECS-II value examples.<br />
* </p>
*
* @author kenta-shimizu
*
*/
public class ExampleGetSecs2Value {
public ExampleGetSecs2Value() {
/* Nothing */
}
public static void main(String[] args) {
Secs2 ss = Secs2.list(
Secs2.binary((byte)1), /* 0 */
Secs2.ascii("MESSAGE-1"), /* 1 */
Secs2.bool(true), /* 2 */
Secs2.list( /* 3 */
Secs2.list( /* 3,0 */
Secs2.ascii("KEY-1"), /* 3,0,0 */
Secs2.int4(100, 101, 102) /* 3,0,1 */
),
Secs2.list( /* 3,1 */
Secs2.ascii("KEY-2"), /* 3,1,0 */
Secs2.int4(200, 201, 202) /* 3,1,1 */
),
Secs2.list( /* 3,2 */
Secs2.ascii("KEY-3"), /* 3,2,0 */
Secs2.int4(300, 301, 302) /* 3,2,1 */
)
),
Secs2.float4(400.0F)
);
System.out.println("# Example-SECS2");
System.out.println(ss);
System.out.println();
try {
/* get(s) */
System.out.println("# Get value by index");
System.out.println("getByte(0, 0):\t" + ss.getByte(0, 0)); /* 1 */
System.out.println("getBytes(0):\t" + Arrays.toString(ss.getBytes(0))); /* 1 */
System.out.println("getAscii(1):\t" + ss.getAscii(1)); /* "MESSAGE-1" */
System.out.println("getBoolean(2, 0):\t" + ss.getBoolean(2, 0)); /* true */
System.out.println("getAscii(3, 0, 0):\t" + ss.getAscii(3, 0, 0)); /* "KEY-1" */
System.out.println("getInt(3, 0, 1, 0):\t" + ss.getInt(3, 0 , 1, 0)); /* 100 */
System.out.println("getInt(3, 0, 1, 1):\t" + ss.getInt(3, 0 , 1, 1)); /* 101 */
System.out.println("getInt(3, 0, 1, 2):\t" + ss.getInt(3, 0 , 1, 2)); /* 102 */
System.out.println("getInt(3, 1, 1, 0):\t" + ss.getInt(3, 1 , 1, 0)); /* 200 */
System.out.println("getInt(3, 2, 1, 0):\t" + ss.getInt(3, 2 , 1, 0)); /* 300 */
System.out.println("getFloat(4, 0):\t" + ss.getFloat(4, 0)); /* 400.0F */
System.out.println("get(3):\t" + ss.get(3)); /* <L[3] ...> */
System.out.println();
System.out.println("# Get Item-Type");
System.out.println("get() item-type:\t" + ss.get().secs2Item()); /* LIST */
System.out.println("get(0) item-type:\t" + ss.get(0).secs2Item()); /* BINARY */
System.out.println("get(1) item-type:\t" + ss.get(1).secs2Item()); /* ASCII */
System.out.println("get(2) item-type:\t" + ss.get(2).secs2Item()); /* BOOLEAN */
System.out.println("get(3) item-type:\t" + ss.get(3).secs2Item()); /* LIST */
System.out.println("get(3, 0, 1) item-type:\t" + ss.get(3, 0, 1).secs2Item()); /* INT4 */
System.out.println("get(4) item-type:\t" + ss.get(4).secs2Item()); /* FLOAT4 */
System.out.println();
System.out.println("# Get Size");
System.out.println("get() size:\t" + ss.get().size()); /* 4 */
System.out.println("get(0) size:\t" + ss.get(0).size()); /* 1 */
System.out.println("get(1) size:\t" + ss.get(1).size()); /* 9 */
System.out.println("get(2) size:\t" + ss.get(2).size()); /* 1 */
System.out.println("get(3) size:\t" + ss.get(3).size()); /* 3 */
System.out.println("get(3, 0, 1) size:\t" + ss.get(3, 0, 1).size()); /* 3 */
System.out.println("get(4) size:\t" + ss.get(4).size()); /* 1 */
System.out.println();
{
System.out.println("# Use for-each-loop, getAscii(3, x, 0)");
Secs2 s3 = ss.get(3);
for ( Secs2 s : s3 ) { /* "KEY-1" *//* "KEY-2" *//* "KEY-3" */
System.out.println("getAscii(3, x, 0):\t" + s.getAscii(0));
}
System.out.println();
}
{
System.out.println("# Use stream, getInt(3, x, 1, 0)");
Secs2 s3 = ss.get(3);
s3.stream()
.map(s -> {
try {
return s.getInt(1, 0);
}
catch ( Secs2Exception e ) {
throw new RuntimeException(e);
}
})
.map(v -> "getInt(3, x, 1, 0):\t" + v)
.forEach(System.out::println); /* 100 *//* 200 *//* 300 */
System.out.println();
}
/* optional(s) */
System.out.println("# optional value by index");
System.out.println("optionalByte(0, 0):\t" + ss.optionalByte(0, 0).map(b -> "" + b).orElse("not found")); /* 1 */
System.out.println("optionalBytes(0):\t" + ss.optionalBytes(0).map(Arrays::toString).orElse("not found")); /* 1 */
System.out.println("oprionAscii(1):\t" + ss.optionalAscii(1).orElse("not found")); /* "MESSAGE-1" */
System.out.println("optionalBoolean(2, 0):\t" + ss.optionalBoolean(2, 0).map(String::valueOf).orElse("not found")); /* true */
System.out.println("optionalAscii(3, 0, 0):\t" + ss.optionalAscii(3, 0, 0).orElse("not found")); /* "KEY-1" */
System.out.println("optionalInt(3, 0, 1, 0):\t" + ss.optionalInt(3, 0 , 1, 0).getAsInt()); /* 100 */
System.out.println("optionalInt(3, 0, 1, 1):\t" + ss.optionalInt(3, 0 , 1, 1).getAsInt()); /* 101 */
System.out.println("optionalInt(3, 0, 1, 2):\t" + ss.optionalInt(3, 0 , 1, 2).getAsInt()); /* 102 */
System.out.println("optionalInt(3, 1, 1, 0):\t" + ss.optionalInt(3, 1 , 1, 0).getAsInt()); /* 200 */
System.out.println("optionalInt(3, 2, 1, 0):\t" + ss.optionalInt(3, 2 , 1, 0).getAsInt()); /* 300 */
System.out.println("optionalDouble(4, 0):\t" + ss.optionalDouble(4, 0).getAsDouble()); /* 400.0F */
System.out.println("optional(4):\t" + ss.optional(3).map(Secs2::toString).orElse("not found")); /* <L[3] ...> */
System.out.println();
System.out.println("optional(99):\t" + ss.optional(99).map(Secs2::toString).orElse("not found")); /* not found */
System.out.println();
}
catch ( Throwable t ) {
t.printStackTrace();
}
}
}