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

burningwave / reflection / #65

03 Oct 2023 02:37PM UTC coverage: 60.982%. Remained the same
#65

push

Roberto-Gentili
Added JDK 21 support

969 of 1589 relevant lines covered (60.98%)

0.61 hits per line

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

33.33
/src/main/java/org/burningwave/Criteria.java
1
/*
2
 * This file is part of Burningwave Reflection.
3
 *
4
 * Author: Roberto Gentili
5
 *
6
 * Hosted at: https://github.com/burningwave/reflection
7
 *
8
 * --
9
 *
10
 * The MIT License (MIT)
11
 *
12
 * Copyright (c) 2022 Roberto Gentili
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
15
 * documentation files (the "Software"), to deal in the Software without restriction, including without
16
 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
17
 * the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
18
 * conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all copies or substantial
21
 * portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24
 * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
25
 * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
27
 * OR OTHER DEALINGS IN THE SOFTWARE.
28
 */
29
package org.burningwave;
30

31
import org.burningwave.function.Function;
32
import org.burningwave.function.Handler;
33
import org.burningwave.function.ThrowingBiFunction;
34
import org.burningwave.function.ThrowingBiPredicate;
35
import org.burningwave.function.ThrowingPredicate;
36
import org.burningwave.function.ThrowingTriPredicate;
37
import org.burningwave.reflection.Constructors;
38

39
@SuppressWarnings("unchecked")
40
public class Criteria<E, C extends Criteria<E, C, T>, T extends Criteria.TestContext<E, C>> {
1✔
41

42
        protected Function<ThrowingBiPredicate<T, E, ? extends Throwable>, ThrowingBiPredicate<T, E, ? extends Throwable>> logicalOperator;
43

44
        protected ThrowingBiPredicate<T, E, ? extends Throwable> predicate;
45

46
        public final static <E, C extends Criteria<E, C, T>, T extends Criteria.TestContext<E, C>> Criteria<E, C, T> of(final ThrowingBiPredicate<T, E, ? extends Throwable> predicate) {
47
                return new Criteria<E, C, T>().allThoseThatMatch(predicate);
×
48
        }
49

50
        public final static <E, C extends Criteria<E, C, T>, T extends Criteria.TestContext<E, C>> Criteria<E, C, T> of(final ThrowingPredicate<E, ? extends Throwable> predicate) {
51
                return new Criteria<E, C, T>().allThoseThatMatch(predicate);
×
52
        }
53

54
        public C allThoseThatMatch(final ThrowingBiPredicate<T, E, ? extends Throwable> predicate) {
55
                this.predicate = concat(
1✔
56
                        this.predicate,
57
                        new ThrowingBiPredicate<T, E, Throwable>() {
1✔
58
                                @Override
59
                                public boolean test(final T context, final E entity) throws Throwable {
60
                                        return predicate.test(context, entity);
1✔
61
                                }
62
                        }
63
                );
64
                return (C)this;
1✔
65
        }
66

67
        public C allThoseThatMatch(final ThrowingPredicate<E, ? extends Throwable> predicate) {
68
                return allThoseThatMatch(
1✔
69
                        new ThrowingBiPredicate<T, E, Throwable>() {
1✔
70
                                @Override
71
                                public boolean test(final T context, final E entity) throws Throwable {
72
                                        return predicate.test(entity);
1✔
73
                                }
74
                        }
75
                ) ;
76
        }
77

78
        public C and(){
79
                logicalOperator = new Function<ThrowingBiPredicate<T, E, ? extends Throwable>, ThrowingBiPredicate<T, E, ? extends Throwable>>() {
1✔
80
                        @Override
81
                        public ThrowingBiPredicate<T, E, ? extends Throwable> apply(final ThrowingBiPredicate<T, E, ? extends Throwable> predicate) {
82
                                return Handler.and(Criteria.this.predicate, (ThrowingBiPredicate)predicate);
1✔
83
                        }
84
                };
85
                return (C)this;
1✔
86
        }
87

88
        public C and(final C criteria) {
89
                return logicOperation(
×
90
                        this.createCopy(),
×
91
                        criteria.createCopy(),
×
92
                        new Function<
93
                                ThrowingBiPredicate<T, E, ? extends Throwable>,
94
                                Function<
95
                                        ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
96
                                        ThrowingBiPredicate<T, E, ? extends Throwable>
97
                                >
98
                        >() {
×
99
                                @Override
100
                                public Function<
101
                                        ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
102
                                        ThrowingBiPredicate<T, E, ? extends Throwable>
103
                                > apply(final ThrowingBiPredicate<T, E, ? extends Throwable> predicate) {
104
                                        return new Function<
×
105
                                                ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
106
                                                ThrowingBiPredicate<T, E, ? extends Throwable>
107
                                        >() {
×
108
                                                @Override
109
                                                public ThrowingBiPredicate<T, E, ? extends Throwable> apply (
110
                                                        final ThrowingBiPredicate<? super T, ? super E, ? extends Throwable> otherPredicate
111
                                                ) {
112
                                                        return Handler.and(predicate, (ThrowingBiPredicate)otherPredicate);
×
113
                                                }
114
                                        };
115
                                }
116
                        },
117
                        newInstance()
×
118
                );
119
        }
120

121
        public C or(){
122
                logicalOperator = new Function<ThrowingBiPredicate<T, E, ? extends Throwable>, ThrowingBiPredicate<T, E, ? extends Throwable>>() {
1✔
123
                        @Override
124
                        public ThrowingBiPredicate<T, E, ? extends Throwable> apply(final ThrowingBiPredicate<T, E, ? extends Throwable> predicate) {
125
                                return Handler.or(Criteria.this.predicate, (ThrowingBiPredicate) predicate);
1✔
126
                        }
127
                };
128
                return (C)this;
1✔
129
        }
130

131
        public C or(final C criteria) {
132
                return logicOperation(
1✔
133
                        this.createCopy(),
1✔
134
                        criteria.createCopy(),
1✔
135
                        new Function<
136
                                ThrowingBiPredicate<T, E, ? extends Throwable>,
137
                                Function<
138
                                        ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
139
                                        ThrowingBiPredicate<T, E, ? extends Throwable>
140
                                >
141
                        >() {
1✔
142
                                @Override
143
                                public Function<
144
                                        ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
145
                                        ThrowingBiPredicate<T, E, ? extends Throwable>
146
                                > apply(final ThrowingBiPredicate<T, E, ? extends Throwable> predicate) {
147
                                        return new Function<
1✔
148
                                                ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
149
                                                ThrowingBiPredicate<T, E, ? extends Throwable>
150
                                        >() {
1✔
151
                                                @Override
152
                                                public ThrowingBiPredicate<T, E, ? extends Throwable> apply (
153
                                                        final ThrowingBiPredicate<? super T, ? super E, ? extends Throwable> otherPredicate
154
                                                ) {
155
                                                        return Handler.or(predicate, (ThrowingBiPredicate)otherPredicate);
1✔
156
                                                }
157
                                        };
158
                                }
159
                        },
160
                        newInstance()
1✔
161
                );
162
        }
163

164
        public C negate() {
165
                final ThrowingBiPredicate<T, E, ? extends Throwable> predicate = this.predicate;
×
166
                this.predicate = new ThrowingBiPredicate<T, E, Throwable>() {
×
167
                        @Override
168
                        public boolean test(final T context, final E entity) throws Throwable {
169
                                return !predicate.test(context, entity);
×
170
                        }
171
                };
172
                return (C)this;
×
173
        }
174

175
        public C createCopy() {
176
                final C copy = newInstance();
1✔
177
                copy.predicate = this.predicate;
1✔
178
                copy.logicalOperator = this.logicalOperator;
1✔
179
                return copy;
1✔
180
        }
181

182

183
        public ThrowingPredicate<E, ? extends Throwable> getPredicateOrFalsePredicateIfPredicateIsNull() {
184
                return getPredicate(createTestContext(), false);
×
185
        }
186

187
        public ThrowingPredicate<E, ? extends Throwable> getPredicateOrTruePredicateIfPredicateIsNull() {
188
                return getPredicate(createTestContext(), true);
1✔
189
        }
190

191
        public boolean hasNoPredicate() {
192
                return this.predicate == null;
×
193
        }
194

195
        public T testWithFalseResultForNullEntityOrFalseResultForNullPredicate(final E entity) {
196
                final T context = createTestContext();
×
197
                testWithFalseResultForNullEntityOrFalseResultForNullPredicate(context, entity);
×
198
                return context;
×
199
        }
200

201
        public T testWithFalseResultForNullEntityOrTrueResultForNullPredicate(final E entity) {
202
                final T context = createTestContext();
×
203
                testWithFalseResultForNullEntityOrTrueResultForNullPredicate(context, entity);
×
204
                return context;
×
205
        }
206

207
        public T testWithTrueResultForNullEntityOrFalseResultForNullPredicate(final E entity) {
208
                final T context = createTestContext();
×
209
                testWithTrueResultForNullEntityOrFalseResultForNullPredicate(context, entity);
×
210
                return context;
×
211
        }
212

213
        public T testWithTrueResultForNullEntityOrTrueResultForNullPredicate(final E entity) {
214
                final T context = createTestContext();
×
215
                testWithTrueResultForNullEntityOrTrueResultForNullPredicate(context, entity);
×
216
                return context;
×
217
        }
218

219
        protected ThrowingBiPredicate<T, E, ? extends Throwable> concat(
220
                final ThrowingBiPredicate<T, E, ? extends Throwable> mainPredicate,
221
                final ThrowingBiPredicate<T, E, ? extends Throwable> otherPredicate
222
        ) {
223
                final ThrowingBiPredicate<T, E, ? extends Throwable> predicate = concat(mainPredicate, this.logicalOperator, otherPredicate);
1✔
224
                this.logicalOperator = null;
1✔
225
                return predicate;
1✔
226
        }
227

228
        protected <E, C extends Criteria<E, C, T>, T extends Criteria.TestContext<E, C>> ThrowingBiPredicate<T, E, ? extends Throwable> concat(
229
                final ThrowingBiPredicate<T, E, ? extends Throwable> mainPredicate,
230
                final Function<ThrowingBiPredicate<T, E, ? extends Throwable>, ThrowingBiPredicate<T, E, ? extends Throwable>> logicalOperator,
231
                final ThrowingBiPredicate<T, E, ? extends Throwable> otherPredicate
232
        ) {
233
                if (otherPredicate != null) {
1✔
234
                        if (mainPredicate != null) {
1✔
235
                                return consumeLogicalOperator(otherPredicate, logicalOperator);
1✔
236
                        }
237
                        return otherPredicate;
1✔
238
                }
239
                return mainPredicate;
×
240
        }
241

242

243
        protected T createTestContext() {
244
                return (T)TestContext.<E, C, T>create((C)this);
1✔
245
        }
246

247
        protected T getContextWithFalsePredicateForNullPredicate() {
248
                final T context = createTestContext();
×
249
                getPredicate(context, false);
×
250
                return context;
×
251
        }
252

253
        protected T getContextWithTruePredicateForNullPredicate() {
254
                final T context = createTestContext();
×
255
                getPredicate(context, true);
×
256
                return context;
×
257
        }
258

259
        protected <V> ThrowingBiPredicate<T, E, ? extends Throwable> getPredicateWrapper(
260
                final ThrowingBiFunction<T, E, V[], ? extends Throwable> valueSupplier,
261
                final ThrowingTriPredicate<T, V[], Integer, ? extends Throwable> predicate
262
        ) {
263
                return getPredicateWrapper(
1✔
264
                        new ThrowingBiPredicate<T, E, Throwable>() {
1✔
265
                                @Override
266
                                public boolean test(final T criteria, final E entity) throws Throwable {
267
                                        final V[] array = valueSupplier.apply(criteria, entity);
1✔
268
                                        boolean result = false;
1✔
269
                                        for (int i = 0; i < array.length; i++) {
1✔
270
                                                if (result = predicate.test(criteria, array, i)) {
1✔
271
                                                        break;
1✔
272
                                                }
273
                                        }
274
                                        //logDebug("test for {} return {}", entity, result);
275
                                        return result;
1✔
276
                                }
277
                        }
278
                );
279
        }
280

281
        protected C logicOperation(final C leftCriteria, final C rightCriteria,
282
                final Function<
283
                        ThrowingBiPredicate<T, E, ? extends Throwable>,
284
                        Function<
285
                                ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
286
                                ThrowingBiPredicate<T, E, ? extends Throwable>
287
                        >
288
                > binaryOperator,
289
                final C targetCriteria
290
        ) {
291
                targetCriteria.predicate =
1✔
292
                        leftCriteria.predicate != null?
293
                                (rightCriteria.predicate != null?
294
                                        binaryOperator.apply(leftCriteria.predicate).apply(rightCriteria.predicate) :
1✔
295
                                        leftCriteria.predicate):
296
                                rightCriteria.predicate;
297
                return targetCriteria;
1✔
298
        }
299

300
        protected C newInstance() {
301
                return (C)Constructors.INSTANCE.newInstanceOf(this.getClass());
1✔
302
        }
303

304
        @SuppressWarnings("hiding")
305
        <E, C extends Criteria<E, C, T>, T extends Criteria.TestContext<E, C>> ThrowingBiPredicate<T,E, ? extends Throwable> consumeLogicalOperator(
306
                final ThrowingBiPredicate<T, E, ? extends Throwable> input,
307
                final Function<
308
                        ThrowingBiPredicate<T, E, ? extends Throwable>,
309
                        ThrowingBiPredicate<T, E, ? extends Throwable>
310
                > logicalOperator
311
        ) {
312
                if (logicalOperator != null) {
1✔
313
                        return logicalOperator.apply(input);
1✔
314
                }
315
                return Throwables.INSTANCE.throwException(
×
316
                        "A call to and/or method is necessary before calling {} at {}",
317
                        Thread.currentThread().getStackTrace()[10].getMethodName(),
×
318
                        Thread.currentThread().getStackTrace()[11]
×
319
                );
320
        }
321

322
        ThrowingBiPredicate<T, E, ? extends Throwable> getPredicateWrapper(
323
                final ThrowingBiPredicate<T, E, ? extends Throwable> function
324
        ) {
325
                if (function != null) {
1✔
326
                        return new ThrowingBiPredicate<T, E, Throwable>() {
1✔
327
                                @Override
328
                                public boolean test(final T criteria, final E entity) throws Throwable {
329
                                        return function.test(criteria, entity);
1✔
330
                                }
331
                        };
332
                }
333
                return null;
×
334
        }
335

336
        private ThrowingPredicate<E, ? extends Throwable> getPredicate(final T context, final boolean defaultResult) {
337
                return context.setPredicate(
1✔
338
                        this.predicate != null?
339
                                new ThrowingPredicate<E, Throwable>() {
1✔
340
                                        @Override
341
                                        public boolean test(final E entity) throws Throwable {
342
                                                return context.setEntity(entity).setResult(Criteria.this.predicate.test(
1✔
343
                                                        context,
344
                                                        entity
345
                                                )).getResult();
1✔
346
                                        }
347
                                } :
348
                        new ThrowingPredicate<E, Throwable>() {
1✔
349
                                @Override
350
                                public boolean test(final E entity) {
351
                                        return context.setEntity(entity).setResult(defaultResult).getResult();
1✔
352
                                }
353
                        }
354
                ).getPredicate();
1✔
355
        }
356

357

358
        private boolean testWithFalseResultForNullEntityOrFalseResultForNullPredicate(final T context, final E entity) {
359
                if (entity != null) {
×
360
                        try {
361
                                return getPredicate(context, false).test(entity);
×
362
                        } catch (final Throwable exc) {
×
363
                                Throwables.INSTANCE.throwException(exc);
×
364
                        }
365
                }
366
                return context.setEntity(entity).setResult(false).getResult();
×
367
        }
368

369

370
        private boolean testWithFalseResultForNullEntityOrTrueResultForNullPredicate(final T context, final E entity) {
371
                if (entity != null) {
×
372
                        try {
373
                                return getPredicate(context, true).test(entity);
×
374
                        } catch (final Throwable exc) {
×
375
                                Throwables.INSTANCE.throwException(exc);
×
376
                        }
377
                }
378
                return context.setEntity(entity).setResult(false).getResult();
×
379
        }
380

381

382
        private boolean testWithTrueResultForNullEntityOrFalseResultForNullPredicate(final T context, final E entity) {
383
                if (entity != null) {
×
384
                        try {
385
                                return getPredicate(context, false).test(entity);
×
386
                        } catch (final Throwable exc) {
×
387
                                Throwables.INSTANCE.throwException(exc);
×
388
                        }
389
                }
390
                return context.setEntity(entity).setResult(true).getResult();
×
391
        }
392

393
        private boolean testWithTrueResultForNullEntityOrTrueResultForNullPredicate(final T context, final E entity) {
394
                if (entity != null) {
×
395
                        try {
396
                                return getPredicate(context, true).test(entity);
×
397
                        } catch (final Throwable exc) {
×
398
                                Throwables.INSTANCE.throwException(exc);
×
399
                        }
400
                }
401
                return context.setEntity(entity).setResult(true).getResult();
×
402
        }
403

404
        public static class Simple<E, C extends Simple<E, C>> {
×
405

406
                protected Function<ThrowingPredicate<E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>> logicalOperator;
407
                protected ThrowingPredicate<E, ? extends Throwable> predicate;
408

409
                public C allThoseThatMatch(final ThrowingPredicate<E, ? extends Throwable> predicate) {
410
                        this.predicate = concat(
×
411
                                this.predicate,
412
                                new ThrowingPredicate<E,Throwable>() {
×
413
                                        @Override
414
                                        public boolean test(final E entity) throws Throwable {
415
                                                return predicate.test(entity);
×
416
                                        }
417
                                }
418
                        );
419
                        return (C)this;
×
420
                }
421

422
                public C and(){
423
                        logicalOperator = new Function<ThrowingPredicate<E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>>() {
×
424
                                @Override
425
                                public ThrowingPredicate<E, ? extends Throwable> apply(final ThrowingPredicate<E, ? extends Throwable> predicate) {
426
                                        return Handler.and(Simple.this.predicate, (ThrowingPredicate)predicate);
×
427
                                }
428
                        };
429
                        return (C)this;
×
430
                }
431

432
                public C and(final C criteria) {
433
                        return logicOperation(this.createCopy(), criteria.createCopy(),
×
434
                                        new Function<ThrowingPredicate<E, ? extends Throwable>,
435
                                        Function<ThrowingPredicate<? super E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>>>() {
×
436
                                @Override
437
                                public Function<ThrowingPredicate<? super E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>> apply(final ThrowingPredicate<E, ? extends Throwable> predicate) {
438
                                        return new Function<
×
439
                                                ThrowingPredicate<? super E, ? extends Throwable>,
440
                                                ThrowingPredicate<E, ? extends Throwable>
441
                                        >() {
×
442
                                                @Override
443
                                                public ThrowingPredicate<E, ? extends Throwable> apply(
444
                                                        final ThrowingPredicate<? super E, ? extends Throwable> otherPredicate
445
                                                ) {
446
                                                        return Handler.and(predicate, (ThrowingPredicate) otherPredicate);
×
447
                                                }
448

449
                                        };
450
                                }
451
                        }, newInstance());
×
452
                }
453

454
                public C or(){
455
                        logicalOperator = new Function<ThrowingPredicate<E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>>() {
×
456
                                @Override
457
                                public ThrowingPredicate<E, ? extends Throwable> apply(final ThrowingPredicate<E, ? extends Throwable> predicate) {
458
                                        return Handler.or(Simple.this.predicate, (ThrowingPredicate)predicate);
×
459
                                }
460
                        };
461
                        return (C)this;
×
462
                }
463

464
                public C or(final C criteria) {
465
                        return logicOperation(
×
466
                                this.createCopy(), criteria.createCopy(),
×
467
                                new Function<ThrowingPredicate<E, ? extends Throwable>, Function<ThrowingPredicate<? super E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>>>() {
×
468
                                        @Override
469
                                        public Function<ThrowingPredicate<? super E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>> apply(final ThrowingPredicate<E, ? extends Throwable> predicate) {
470
                                                return new Function<
×
471
                                                        ThrowingPredicate<? super E, ? extends Throwable>,
472
                                                        ThrowingPredicate<E, ? extends Throwable>
473
                                                >() {
×
474
                                                        @Override
475
                                                        public ThrowingPredicate<E, ? extends Throwable> apply(
476
                                                                final ThrowingPredicate<? super E, ? extends Throwable> otherPredicate
477
                                                        ) {
478
                                                                return Handler.or(predicate, (ThrowingPredicate)otherPredicate);
×
479
                                                        }
480
                                                };
481
                                        }
482
                                },
483
                                newInstance()
×
484
                        );
485
                }
486

487
                public C negate() {
488
                        final ThrowingPredicate<E, ? extends Throwable> predicate = this.predicate;
×
489
                        this.predicate = new ThrowingPredicate<E, Throwable>() {
×
490

491
                                @Override
492
                                public boolean test(E entity) throws Throwable {
493
                                        return !predicate.test(entity);
×
494
                                }
495

496
                        };
497
                        return (C)this;
×
498
                }
499

500
                public C createCopy() {
501
                        final C copy = newInstance();
×
502
                        copy.predicate = this.predicate;
×
503
                        copy.logicalOperator = this.logicalOperator;
×
504
                        return copy;
×
505
                }
506

507
                public ThrowingPredicate<E, ? extends Throwable> getPredicateOrFalsePredicateIfPredicateIsNull() {
508
                        return getPredicate(false);
×
509
                }
510

511
                public ThrowingPredicate<E, ? extends Throwable> getPredicateOrTruePredicateIfPredicateIsNull() {
512
                        return getPredicate(true);
×
513
                }
514

515
                public boolean hasNoPredicate() {
516
                        return this.predicate == null;
×
517
                }
518

519
                public boolean testWithFalseResultForNullEntityOrFalseResultForNullPredicate(final E entity) {
520
                        if (entity != null) {
×
521
                                try {
522
                                        return getPredicateOrFalsePredicateIfPredicateIsNull().test(entity);
×
523
                                } catch (final Throwable exc) {
×
524
                                        return Throwables.INSTANCE.throwException(exc);
×
525
                                }
526
                        }
527
                        return false;
×
528
                }
529

530
                public boolean testWithFalseResultForNullEntityOrTrueResultForNullPredicate(final E entity) {
531
                        if (entity != null) {
×
532
                                try {
533
                                        return getPredicateOrTruePredicateIfPredicateIsNull().test(entity);
×
534
                                } catch (final Throwable exc) {
×
535
                                        return Throwables.INSTANCE.throwException(exc);
×
536
                                }
537
                        }
538
                        return false;
×
539
                }
540

541
                public boolean testWithFalseResultForNullPredicate(final E entity) {
542
                        try {
543
                                return getPredicateOrFalsePredicateIfPredicateIsNull().test(entity);
×
544
                        } catch (final Throwable exc) {
×
545
                                return Throwables.INSTANCE.throwException(exc);
×
546
                        }
547
                }
548

549
                public boolean testWithTrueResultForNullEntityOrFalseResultForNullPredicate(final E entity) {
550
                        if (entity != null) {
×
551
                                try {
552
                                        return getPredicateOrFalsePredicateIfPredicateIsNull().test(entity);
×
553
                                } catch (final Throwable exc) {
×
554
                                        return Throwables.INSTANCE.throwException(exc);
×
555
                                }
556
                        }
557
                        return true;
×
558
                }
559

560
                public boolean testWithTrueResultForNullEntityOrTrueResultForNullPredicate(final E entity) {
561
                        if (entity != null) {
×
562
                                try {
563
                                        return getPredicateOrTruePredicateIfPredicateIsNull().test(entity);
×
564
                                } catch (final Throwable exc) {
×
565
                                        return Throwables.INSTANCE.throwException(exc);
×
566
                                }
567
                        }
568
                        return true;
×
569
                }
570

571
                public boolean testWithTrueResultForNullPredicate(final E entity) {
572
                        try {
573
                                return getPredicateOrTruePredicateIfPredicateIsNull().test(entity);
×
574
                        } catch (final Throwable exc) {
×
575
                                return Throwables.INSTANCE.throwException(exc);
×
576
                        }
577
                }
578

579

580
                protected <E, C extends Simple<E, C>> ThrowingPredicate<E, ? extends Throwable> concat(
581
                        final ThrowingPredicate<E, ? extends Throwable> mainPredicate,
582
                        final Function<ThrowingPredicate<E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>> logicalOperator,
583
                        final ThrowingPredicate<E, ? extends Throwable> otherPredicate
584
                ) {
585
                        if (otherPredicate != null) {
×
586
                                if (mainPredicate != null) {
×
587
                                        return consumeLogicalOperator(otherPredicate, logicalOperator);
×
588
                                }
589
                                return otherPredicate;
×
590
                        }
591
                        return mainPredicate;
×
592
                }
593

594
                protected ThrowingPredicate<E, ? extends Throwable> concat(
595
                        final ThrowingPredicate<E, ? extends Throwable> mainPredicate,
596
                        final ThrowingPredicate<E, ? extends Throwable> otherPredicate
597
                ) {
598
                        final ThrowingPredicate<E, ? extends Throwable> predicate = concat(mainPredicate, this.logicalOperator, otherPredicate);
×
599
                        this.logicalOperator = null;
×
600
                        return predicate;
×
601
                }
602

603
                protected <V> ThrowingPredicate<E, ? extends Throwable> getPredicateWrapper(
604
                        final Function<E, V[]> valueSupplier,
605
                        final ThrowingBiPredicate<V[], Integer, ? extends Throwable> predicate
606
                ) {
607
                        return getPredicateWrapper(new ThrowingPredicate<E, Throwable>() {
×
608
                                @Override
609
                                public boolean test(final E entity) throws Throwable {
610
                                        final V[] array = valueSupplier.apply(entity);
×
611
                                        boolean result = false;
×
612
                                        for (int i = 0; i < array.length; i++) {
×
613
                                                if (result = predicate.test(array, i)) {
×
614
                                                        break;
×
615
                                                }
616
                                        }
617
                                        //logDebug("test for {} return {}", entity, result);
618
                                        return result;
×
619
                                }
620
                        });
621
                }
622

623
                protected C logicOperation(final C leftCriteria, final C rightCriteria,
624
                        final Function<ThrowingPredicate<E, ? extends Throwable>, Function<ThrowingPredicate< ? super E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>>> binaryOperator,
625
                        final C targetCriteria
626
                ) {
627
                        targetCriteria.predicate =
×
628
                                leftCriteria.predicate != null?
629
                                        (rightCriteria.predicate != null?
630
                                                binaryOperator.apply(leftCriteria.predicate).apply(rightCriteria.predicate) :
×
631
                                                leftCriteria.predicate):
632
                                        rightCriteria.predicate;
633
                        return targetCriteria;
×
634
                }
635

636
                protected C newInstance() {
637
                        return (C)Constructors.INSTANCE.newInstanceOf(this.getClass());
×
638
                }
639

640
                @SuppressWarnings("hiding")
641
                <E, C extends Simple<E, C>> ThrowingPredicate<E, ? extends Throwable> consumeLogicalOperator (
642
                        final ThrowingPredicate<E, ? extends Throwable> input,
643
                        final Function<ThrowingPredicate<E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>> logicalOperator
644
                ) {
645
                        if (logicalOperator != null) {
×
646
                                return logicalOperator.apply(input);
×
647
                        }
648
                        return Throwables.INSTANCE.throwException(
×
649
                                "A call to and/or method is necessary before calling {} at {}",
650
                                Thread.currentThread().getStackTrace()[10].getMethodName(),
×
651
                                Thread.currentThread().getStackTrace()[11]
×
652
                        );
653
                }
654

655
                ThrowingPredicate<E, ? extends Throwable> getPredicateWrapper(
656
                        final ThrowingPredicate<E, ? extends Throwable> function
657
                ) {
658
                        if (function != null) {
×
659
                                return new ThrowingPredicate<E, Throwable>() {
×
660
                                        @Override
661
                                        public boolean test(final E entity) throws Throwable {
662
                                                return function.test(entity);
×
663
                                        }
664
                                };
665
                        }
666
                        return null;
×
667
                }
668

669
                private ThrowingPredicate<E, ? extends Throwable> getPredicate(final boolean defaultResult) {
670
                        return this.predicate != null?
×
671
                                new ThrowingPredicate<E, Throwable>() {
×
672
                                        @Override
673
                                        public boolean test(final E entity) throws Throwable {
674
                                                return Simple.this.predicate.test(entity);
×
675
                                        }
676
                                } :
677
                                new ThrowingPredicate<E, Throwable>() {
×
678
                                        @Override
679
                                        public boolean test(final E entity) {
680
                                                return defaultResult;
×
681
                                        }
682
                                };
683
                }
684
        }
685

686
        public static class TestContext<E, C extends Criteria<E, C, ?>> extends Context {
687
                private enum Elements {
1✔
688
                        ENTITY,
1✔
689
                        PREDICATE,
1✔
690
                        TEST_RESULT,
1✔
691
                        THIS_CRITERIA
1✔
692
                }
693

694
                protected TestContext(final C criteria) {
695
                        super();
1✔
696
                        put(Elements.THIS_CRITERIA, criteria);
1✔
697
                }
1✔
698

699
                public static <E, C extends Criteria<E, C, T>, T extends Criteria.TestContext<E, C>> TestContext<E, C> create(final C criteria) {
700
                        return new TestContext<>(criteria);
1✔
701
                }
702

703
                public C getCriteria() {
704
                        return get(Elements.THIS_CRITERIA);
×
705
                }
706

707
                public E getEntity() {
708
                        return get(Elements.ENTITY);
1✔
709
                }
710

711
                public ThrowingPredicate<E, ? extends Throwable> getPredicate() {
712
                        return get(Elements.PREDICATE);
1✔
713
                }
714

715
                public Boolean getResult() {
716
                        return super.get(Elements.TEST_RESULT);
1✔
717
                }
718

719
                <T extends Criteria.TestContext<E, C>> T setEntity(final E entity) {
720
                        put(Elements.ENTITY, entity);
1✔
721
                        return (T) this;
1✔
722
                }
723

724
                <T extends Criteria.TestContext<E, C>> T setPredicate(final ThrowingPredicate<E, ? extends Throwable> predicate) {
725
                        put(Elements.PREDICATE, predicate);
1✔
726
                        return (T)this;
1✔
727
                }
728

729
                <T extends Criteria.TestContext<E, C>> T setResult(final Boolean result) {
730
                        put(Elements.TEST_RESULT, result);
1✔
731
                        return (T) this;
1✔
732
                }
733
        }
734

735
}
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

© 2025 Coveralls, Inc