• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trydofor / professional-mirana / #68

31 Aug 2024 02:56AM UTC coverage: 84.4% (+1.0%) from 83.382%
#68

push

web-flow
Merge pull request #45 from trydofor/develop

v2.7.3 with minor change

474 of 568 new or added lines in 27 files covered. (83.45%)

8 existing lines in 6 files now uncovered.

5237 of 6205 relevant lines covered (84.4%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/main/java/pro/fessional/mirana/data/Z.java
1
package pro.fessional.mirana.data;
2

3
import org.jetbrains.annotations.Contract;
4
import org.jetbrains.annotations.NotNull;
5
import org.jetbrains.annotations.Nullable;
6
import pro.fessional.mirana.best.DummyBlock;
7

8
import java.math.BigDecimal;
9
import java.util.ArrayList;
10
import java.util.Collection;
11
import java.util.Collections;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.function.Function;
16
import java.util.function.Predicate;
17
import java.util.function.Supplier;
18

19
/**
20
 * The first data operation that satisfies the condition (e.g., non-null)
21
 *
22
 * @author trydofor
23
 * @since 2019-10-24
24
 */
25
public interface Z {
26

27
    /**
28
     * Extract the key in the specified way.
29
     * return objects that are unique and in the same order as the input.
30
     */
31
    @SuppressWarnings("unchecked")
32
    @SafeVarargs
33
    @NotNull
34
    static <T> List<T> uniq(Collection<? extends T> ts, Function<? super T, ?>... fn) {
35
        if (ts == null) return Collections.emptyList();
1✔
36
        if (fn == null || fn.length == 0) {
1✔
37
            if (ts instanceof List) {
1✔
38
                return (List<T>) ts;
1✔
39
            }
40
            else {
41
                return new ArrayList<>(ts);
1✔
42
            }
43
        }
44

45
        Map<Object, Boolean> map = new HashMap<>();
1✔
46
        List<T> result = new ArrayList<>(ts.size());
1✔
47
        for (T t : ts) {
1✔
48
            List<Object> ks = new ArrayList<>(fn.length);
1✔
49
            for (Function<? super T, ?> f : fn) {
1✔
50
                ks.add(f.apply(t));
1✔
51
            }
52
            Boolean o = map.putIfAbsent(ks, Boolean.TRUE);
1✔
53
            if (o == null) {
1✔
54
                result.add(t);
1✔
55
            }
56
        }
1✔
57

58
        return result;
1✔
59
    }
60

61
    //// find
62

63
    @Nullable
64
    static <T> T find(Predicate<T> p, Iterable<? extends T> ts) {
65
        if (ts == null) return null;
1✔
66
        for (T t : ts) {
1✔
67
            if (t != null && p.test(t)) return t;
1✔
68
        }
1✔
69
        return null;
1✔
70
    }
71

72
    @SafeVarargs
73
    @Nullable
74
    static <T> T find(Predicate<T> p, T... ts) {
75
        if (ts == null) return null;
1✔
76
        for (T t : ts) {
1✔
77
            if (t != null && p.test(t)) return t;
1✔
78
        }
79
        return null;
1✔
80
    }
81

82
    @SafeVarargs
83
    @Contract("!null,_,_ ->!null")
84
    static <T> T findSure(T d, Predicate<T> p, T... ts) {
85
        T t = find(p, ts);
1✔
86
        return t == null ? d : t;
1✔
87
    }
88

89
    @Contract("!null,_,_ ->!null")
90
    static <T> T findSure(T d, Predicate<T> p, Iterable<? extends T> ts) {
91
        T t = find(p, ts);
1✔
92
        return t == null ? d : t;
1✔
93
    }
94

95
    @SafeVarargs
96
    @Contract("!null,_,_ ->!null")
97
    static <T> T findSafe(Supplier<T> d, Predicate<T> p, T... ts) {
98
        T t = find(p, ts);
1✔
99
        return t == null ? d.get() : t;
1✔
100
    }
101

102
    @Contract("!null,_,_ ->!null")
103
    static <T> T findSafe(Supplier<T> d, Predicate<T> p, Iterable<? extends T> ts) {
104
        T t = find(p, ts);
1✔
105
        return t == null ? d.get() : t;
1✔
106
    }
107

108
    //// make
109

110
    @Nullable
111
    static <T, R> R make(Function<T, R> f, Iterable<? extends T> ts) {
112
        if (ts == null) return null;
1✔
113
        for (T t : ts) {
1✔
114
            if (t != null) {
1✔
115
                try {
116
                    R r = f.apply(t);
1✔
117
                    if (r != null) return r;
1✔
118
                }
119
                catch (Exception e) {
1✔
120
                    DummyBlock.ignore(e);
1✔
121
                }
×
122
            }
123
        }
1✔
124
        return null;
1✔
125
    }
126

127
    @SafeVarargs
128
    @Nullable
129
    static <T, R> R make(Function<T, R> f, T... ts) {
130
        if (ts == null) return null;
1✔
131
        for (T t : ts) {
1✔
132
            if (t != null) {
1✔
133
                try {
134
                    R r = f.apply(t);
1✔
135
                    if (r != null) return r;
1✔
136
                }
137
                catch (Exception e) {
1✔
138
                    DummyBlock.ignore(e);
1✔
NEW
139
                }
×
140
            }
141
        }
142
        return null;
1✔
143
    }
144

145
    @SafeVarargs
146
    @Contract("!null,_,_ ->!null")
147
    static <T, R> R makeSure(R d, Function<T, R> f, T... ts) {
148
        R t = make(f, ts);
1✔
149
        return t == null ? d : t;
1✔
150
    }
151

152
    @Contract("!null,_,_ ->!null")
153
    static <T, R> R makeSure(R d, Function<T, R> f, Iterable<? extends T> ts) {
154
        R t = make(f, ts);
1✔
155
        return t == null ? d : t;
1✔
156
    }
157

158
    /**
159
     * Convert the first non-null object that can be applied to `f`.
160
     */
161
    @SafeVarargs
162
    @Contract("!null,_,_ ->!null")
163
    static <T, R> R makeSafe(Supplier<R> d, Function<T, R> f, T... ts) {
164
        R t = make(f, ts);
1✔
165
        return t == null ? d.get() : t;
1✔
166
    }
167

168
    @Contract("!null,_,_ ->!null")
169
    static <T, R> R makeSafe(Supplier<R> d, Function<T, R> f, Iterable<? extends T> ts) {
170
        R t = make(f, ts);
1✔
171
        return t == null ? d.get() : t;
1✔
172
    }
173

174
    //// decimal
175
    @Nullable
176
    static BigDecimal decimal(Iterable<? extends CharSequence> ts) {
177
        if (ts == null) return null;
1✔
178
        for (CharSequence t : ts) {
1✔
179
            if (t != null && t.length() > 0) {
1✔
180
                String s = t.toString().trim();
1✔
181
                if (!s.isEmpty()) {
1✔
182
                    try {
183
                        return new BigDecimal(s);
1✔
184
                    }
185
                    catch (Exception e) {
1✔
186
                        DummyBlock.ignore(e);
1✔
187
                    }
188
                }
189
            }
190
        }
1✔
191
        return null;
1✔
192
    }
193

194
    @Nullable
195
    static BigDecimal decimal(CharSequence... ts) {
196
        if (ts == null) return null;
1✔
197
        for (CharSequence t : ts) {
1✔
198
            if (t != null && t.length() > 0) {
1✔
199
                String s = t.toString().trim();
1✔
200
                if (!s.isEmpty()) {
1✔
201
                    try {
202
                        return new BigDecimal(s);
1✔
203
                    }
NEW
204
                    catch (Exception e) {
×
NEW
205
                        DummyBlock.ignore(e);
×
206
                    }
207
                }
208
            }
209
        }
NEW
210
        return null;
×
211
    }
212

213
    @Contract("!null,_ ->!null")
214
    static BigDecimal decimalSure(BigDecimal d, CharSequence... ts) {
215
        BigDecimal t = decimal(ts);
1✔
216
        return t == null ? d : t;
1✔
217
    }
218

219
    @Contract("!null,_ ->!null")
220
    static BigDecimal decimalSure(BigDecimal d, Iterable<? extends CharSequence> ts) {
221
        BigDecimal t = decimal(ts);
1✔
222
        return t == null ? d : t;
1✔
223
    }
224

225
    /**
226
     * The first non-null decimal that can be converted
227
     */
228
    @Contract("!null,_ ->!null")
229
    static BigDecimal decimalSafe(Supplier<BigDecimal> d, CharSequence... ts) {
230
        BigDecimal t = decimal(ts);
1✔
231
        return t == null ? d.get() : t;
1✔
232
    }
233

234
    @Contract("!null,_ ->!null")
235
    static BigDecimal decimalSafe(Supplier<BigDecimal> d, Iterable<? extends CharSequence> ts) {
236
        BigDecimal t = decimal(ts);
1✔
237
        return t == null ? d.get() : t;
1✔
238
    }
239

240
    //// int64
241

242
    @Nullable
243
    static Long int64(Iterable<? extends CharSequence> ts) {
244
        if (ts == null) return null;
1✔
245
        for (CharSequence t : ts) {
1✔
246
            if (t != null && t.length() > 0) {
1✔
247
                String s = t.toString().trim();
1✔
248
                if (!s.isEmpty()) {
1✔
249
                    try {
250
                        return Long.valueOf(s);
1✔
251
                    }
252
                    catch (Exception e) {
×
253
                        DummyBlock.ignore(e);
×
254
                    }
255
                }
256
            }
257
        }
1✔
NEW
258
        return null;
×
259
    }
260

261
    @Nullable
262
    static Long int64(CharSequence... ts) {
263
        if (ts == null) return null;
1✔
264
        for (CharSequence t : ts) {
1✔
265
            if (t != null && t.length() > 0) {
1✔
266
                String s = t.toString().trim();
1✔
267
                if (!s.isEmpty()) {
1✔
268
                    try {
269
                        return Long.valueOf(s);
1✔
270
                    }
271
                    catch (Exception e) {
1✔
272
                        DummyBlock.ignore(e);
1✔
273
                    }
274
                }
275
            }
276
        }
277
        return null;
1✔
278
    }
279

280
    /**
281
     * The first non-null long that can be converted
282
     */
283
    @Contract("!null,_ ->!null")
284
    static Long int64Sure(Long d, CharSequence... ts) {
285
        Long t = int64(ts);
1✔
286
        return t == null ? d : t;
1✔
287
    }
288

289
    @Contract("!null,_ ->!null")
290
    static Long int64Sure(Long d, Iterable<? extends CharSequence> ts) {
291
        Long t = int64(ts);
1✔
292
        return t == null ? d : t;
1✔
293
    }
294

295
    @Contract("!null,_ ->!null")
296
    static Long int64Safe(Supplier<Long> d, CharSequence... ts) {
297
        Long t = int64(ts);
1✔
298
        return t == null ? d.get() : t;
1✔
299
    }
300

301
    @Contract("!null,_ ->!null")
302
    static Long int64Safe(Supplier<Long> d, Iterable<? extends CharSequence> ts) {
303
        Long t = int64(ts);
1✔
304
        return t == null ? d.get() : t;
1✔
305
    }
306

307
    @Nullable
308
    static Integer int32(Iterable<? extends CharSequence> ts) {
309
        if (ts == null) return null;
1✔
310
        for (CharSequence t : ts) {
1✔
311
            if (t != null && t.length() > 0) {
1✔
312
                String s = t.toString().trim();
1✔
313
                if (!s.isEmpty()) {
1✔
314
                    try {
315
                        return Integer.valueOf(s);
1✔
316
                    }
317
                    catch (Exception e) {
×
318
                        DummyBlock.ignore(e);
×
319
                    }
320
                }
321
            }
322
        }
1✔
NEW
323
        return null;
×
324
    }
325

326
    @Nullable
327
    static Integer int32(CharSequence... ts) {
328
        if (ts == null) return null;
1✔
329
        for (CharSequence t : ts) {
1✔
330
            if (t != null && t.length() > 0) {
1✔
331
                String s = t.toString().trim();
1✔
332
                if (!s.isEmpty()) {
1✔
333
                    try {
334
                        return Integer.valueOf(s);
1✔
335
                    }
336
                    catch (Exception e) {
1✔
337
                        DummyBlock.ignore(e);
1✔
338
                    }
339
                }
340
            }
341
        }
342
        return null;
1✔
343
    }
344

345
    /**
346
     * The first non-null integer that can be converted
347
     */
348
    @Contract("!null,_ ->!null")
349
    static Integer int32Sure(Integer d, CharSequence... ts) {
350
        Integer t = int32(ts);
1✔
351
        return t == null ? d : t;
1✔
352
    }
353

354
    @Contract("!null,_ ->!null")
355
    static Integer int32Sure(Integer d, Iterable<? extends CharSequence> ts) {
356
        Integer t = int32(ts);
1✔
357
        return t == null ? d : t;
1✔
358
    }
359

360
    @Contract("!null,_ ->!null")
361
    static Integer int32Safe(Supplier<Integer> d, CharSequence... ts) {
362
        Integer t = int32(ts);
1✔
363
        return t == null ? d.get() : t;
1✔
364
    }
365

366
    @Contract("!null,_ ->!null")
367
    static Integer int32Safe(Supplier<Integer> d, Iterable<? extends CharSequence> ts) {
368
        Integer t = int32(ts);
1✔
369
        return t == null ? d.get() : t;
1✔
370
    }
371

372
    //// notNull
373

374
    @Nullable
375
    static <T> T notNull(Iterable<? extends T> ts) {
376
        if (ts == null) return null;
1✔
377
        for (T t : ts) {
1✔
378
            if (t != null) return t;
1✔
379
        }
1✔
NEW
380
        return null;
×
381
    }
382

383
    @SafeVarargs
384
    @Nullable
385
    static <T> T notNull(T... ts) {
386
        if (ts == null) return null;
1✔
387
        for (T t : ts) {
1✔
388
            if (t != null) return t;
1✔
389
        }
390
        return null;
1✔
391
    }
392

393
    @Contract("!null,_ ->!null")
394
    @SafeVarargs
395
    static <T> T notNullSure(T d, T... ts) {
396
        T t = notNull(ts);
1✔
397
        return t == null ? d : t;
1✔
398
    }
399

400
    @Contract("!null,_ ->!null")
401
    static <T> T notNullSure(T d, Iterable<? extends T> ts) {
402
        T t = notNull(ts);
1✔
403
        return t == null ? d : t;
1✔
404
    }
405

406
    @Contract("!null,_ ->!null")
407
    @SafeVarargs
408
    static <T> T notNullSafe(Supplier<T> d, T... ts) {
409
        T t = notNull(ts);
1✔
410
        return t == null ? d.get() : t;
1✔
411
    }
412

413
    @Contract("!null,_ ->!null")
414
    static <T> T notNullSafe(Supplier<T> d, Iterable<? extends T> ts) {
415
        T t = notNull(ts);
1✔
416
        return t == null ? d.get() : t;
1✔
417
    }
418

419
    //// notEmpty
420

421
    @Nullable
422
    static <T extends CharSequence> T notEmpty(Iterable<? extends T> ts) {
423
        if (ts == null) return null;
1✔
424
        for (T t : ts) {
1✔
425
            if (t != null && t.length() > 0) return t;
1✔
426
        }
1✔
NEW
427
        return null;
×
428
    }
429

430
    @SafeVarargs
431
    @Nullable
432
    static <T extends CharSequence> T notEmpty(T... ts) {
433
        if (ts == null) return null;
1✔
434
        for (T t : ts) {
1✔
435
            if (t != null && t.length() > 0) return t;
1✔
436
        }
437
        return null;
1✔
438
    }
439

440
    @Contract("!null,_ ->!null")
441
    @SafeVarargs
442
    static <T extends CharSequence> T notEmptySure(T d, T... ts) {
443
        T t = notEmpty(ts);
1✔
444
        return t == null ? d : t;
1✔
445
    }
446

447
    @Contract("!null,_ ->!null")
448
    static <T extends CharSequence> T notEmptySure(T d, Iterable<? extends T> ts) {
449
        T t = notEmpty(ts);
1✔
450
        return t == null ? d : t;
1✔
451
    }
452

453
    @Contract("!null,_ ->!null")
454
    @SafeVarargs
455
    static <T extends CharSequence> T notEmptySafe(Supplier<T> d, T... ts) {
456
        T t = notEmpty(ts);
1✔
457
        return t == null ? d.get() : t;
1✔
458
    }
459

460
    @Contract("!null,_ ->!null")
461
    static <T extends CharSequence> T notEmptySafe(Supplier<T> d, Iterable<? extends T> ts) {
462
        T t = notEmpty(ts);
1✔
463
        return t == null ? d.get() : t;
1✔
464
    }
465

466
    //// notBlank
467

468
    @Nullable
469
    static String notBlank(Iterable<? extends CharSequence> ts) {
470
        if (ts == null) return null;
1✔
471
        for (CharSequence t : ts) {
1✔
472
            if (t != null && t.length() > 0) {
1✔
473
                String s = t.toString().trim();
1✔
474
                if (!s.isEmpty()) return s;
1✔
475
            }
476
        }
1✔
NEW
477
        return null;
×
478
    }
479

480
    @Nullable
481
    static String notBlank(CharSequence... ts) {
482
        if (ts == null) return null;
1✔
483
        for (CharSequence t : ts) {
1✔
484
            if (t != null && t.length() > 0) {
1✔
485
                String s = t.toString().trim();
1✔
486
                if (!s.isEmpty()) return s;
1✔
487
            }
488
        }
489
        return null;
1✔
490
    }
491

492
    @Contract("!null,_ ->!null")
493
    static String notBlankSure(String d, CharSequence... ts) {
494
        String t = notBlank(ts);
1✔
495
        return t == null ? d : t;
1✔
496
    }
497

498
    @Contract("!null,_ ->!null")
499
    static String notBlankSure(String d, Iterable<? extends CharSequence> ts) {
500
        String t = notBlank(ts);
1✔
501
        return t == null ? d : t;
1✔
502
    }
503

504
    @Contract("!null,_ ->!null")
505
    static String notBlankSafe(Supplier<String> d, CharSequence... ts) {
506
        String t = notBlank(ts);
1✔
507
        return t == null ? d.get() : t;
1✔
508
    }
509

510
    @Contract("!null,_ ->!null")
511
    static String notBlankSafe(Supplier<String> d, Iterable<? extends CharSequence> ts) {
512
        String t = notBlank(ts);
1✔
513
        return t == null ? d.get() : t;
1✔
514
    }
515
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc