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

burningwave / reflection / #55

07 Sep 2023 06:44AM UTC coverage: 61.136% (-0.2%) from 61.29%
#55

push

Roberto-Gentili
Improved functionalities. Releasing new version

1 of 1 new or added line in 1 file covered. (100.0%)

969 of 1585 relevant lines covered (61.14%)

0.61 hits per line

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

33.92
/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 negate() {
89
                final ThrowingBiPredicate<T, E, ? extends Throwable> predicate = this.predicate;
×
90
                this.predicate = new ThrowingBiPredicate<T, E, Throwable>() {
×
91
                        @Override
92
                        public boolean test(final T context, final E entity) throws Throwable {
93
                                return predicate.test(context, entity);
×
94
                        }
95
                };
96
                return (C)this;
×
97
        }
98

99
        public C and(final C criteria) {
100
                return logicOperation(
×
101
                        this.createCopy(),
×
102
                        criteria.createCopy(),
×
103
                        new Function<
104
                                ThrowingBiPredicate<T, E, ? extends Throwable>,
105
                                Function<
106
                                        ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
107
                                        ThrowingBiPredicate<T, E, ? extends Throwable>
108
                                >
109
                        >() {
×
110
                                @Override
111
                                public Function<
112
                                        ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
113
                                        ThrowingBiPredicate<T, E, ? extends Throwable>
114
                                > apply(final ThrowingBiPredicate<T, E, ? extends Throwable> predicate) {
115
                                        return new Function<
×
116
                                                ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
117
                                                ThrowingBiPredicate<T, E, ? extends Throwable>
118
                                        >() {
×
119
                                                @Override
120
                                                public ThrowingBiPredicate<T, E, ? extends Throwable> apply (
121
                                                        final ThrowingBiPredicate<? super T, ? super E, ? extends Throwable> otherPredicate
122
                                                ) {
123
                                                        return Handler.and(predicate, (ThrowingBiPredicate)otherPredicate);
×
124
                                                }
125
                                        };
126
                                }
127
                        },
128
                        newInstance()
×
129
                );
130
        }
131

132

133
        public C createCopy() {
134
                final C copy = newInstance();
1✔
135
                copy.predicate = this.predicate;
1✔
136
                copy.logicalOperator = this.logicalOperator;
1✔
137
                return copy;
1✔
138
        }
139

140

141
        public ThrowingPredicate<E, ? extends Throwable> getPredicateOrFalsePredicateIfPredicateIsNull() {
142
                return getPredicate(createTestContext(), false);
×
143
        }
144

145
        public ThrowingPredicate<E, ? extends Throwable> getPredicateOrTruePredicateIfPredicateIsNull() {
146
                return getPredicate(createTestContext(), true);
1✔
147
        }
148

149
        public boolean hasNoPredicate() {
150
                return this.predicate == null;
×
151
        }
152

153
        public C or(){
154
                logicalOperator = new Function<ThrowingBiPredicate<T, E, ? extends Throwable>, ThrowingBiPredicate<T, E, ? extends Throwable>>() {
1✔
155
                        @Override
156
                        public ThrowingBiPredicate<T, E, ? extends Throwable> apply(final ThrowingBiPredicate<T, E, ? extends Throwable> predicate) {
157
                                return Handler.or(Criteria.this.predicate, (ThrowingBiPredicate) predicate);
1✔
158
                        }
159
                };
160
                return (C)this;
1✔
161
        }
162

163
        public C or(final C criteria) {
164
                return logicOperation(
1✔
165
                        this.createCopy(),
1✔
166
                        criteria.createCopy(),
1✔
167
                        new Function<
168
                                ThrowingBiPredicate<T, E, ? extends Throwable>,
169
                                Function<
170
                                        ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
171
                                        ThrowingBiPredicate<T, E, ? extends Throwable>
172
                                >
173
                        >() {
1✔
174
                                @Override
175
                                public Function<
176
                                        ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
177
                                        ThrowingBiPredicate<T, E, ? extends Throwable>
178
                                > apply(final ThrowingBiPredicate<T, E, ? extends Throwable> predicate) {
179
                                        return new Function<
1✔
180
                                                ThrowingBiPredicate<? super T, ? super E, ? extends Throwable>,
181
                                                ThrowingBiPredicate<T, E, ? extends Throwable>
182
                                        >() {
1✔
183
                                                @Override
184
                                                public ThrowingBiPredicate<T, E, ? extends Throwable> apply (
185
                                                        final ThrowingBiPredicate<? super T, ? super E, ? extends Throwable> otherPredicate
186
                                                ) {
187
                                                        return Handler.or(predicate, (ThrowingBiPredicate)otherPredicate);
1✔
188
                                                }
189
                                        };
190
                                }
191
                        },
192
                        newInstance()
1✔
193
                );
194
        }
195

196

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

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

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

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

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

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

244

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

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

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

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

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

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

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

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

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

359

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

371

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

383

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

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

406
        public static class Simple<E, C extends Simple<E, C>> {
×
407

408
                protected Function<ThrowingPredicate<E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>> logicalOperator;
409
                protected ThrowingPredicate<E, ? extends Throwable> predicate;
410

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

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

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

451
                                        };
452
                                }
453
                        }, newInstance());
×
454
                }
455

456
                public C createCopy() {
457
                        final C copy = newInstance();
×
458
                        copy.predicate = this.predicate;
×
459
                        copy.logicalOperator = this.logicalOperator;
×
460
                        return copy;
×
461
                }
462

463
                public ThrowingPredicate<E, ? extends Throwable> getPredicateOrFalsePredicateIfPredicateIsNull() {
464
                        return getPredicate(false);
×
465
                }
466

467

468
                public ThrowingPredicate<E, ? extends Throwable> getPredicateOrTruePredicateIfPredicateIsNull() {
469
                        return getPredicate(true);
×
470
                }
471

472
                public boolean hasNoPredicate() {
473
                        return this.predicate == null;
×
474
                }
475

476
                public C or(){
477
                        logicalOperator = new Function<ThrowingPredicate<E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>>() {
×
478
                                @Override
479
                                public ThrowingPredicate<E, ? extends Throwable> apply(final ThrowingPredicate<E, ? extends Throwable> predicate) {
480
                                        return Handler.or(Simple.this.predicate, (ThrowingPredicate)predicate);
×
481
                                }
482
                        };
483
                        return (C)this;
×
484
                }
485

486
                public C or(final C criteria) {
487
                        return logicOperation(
×
488
                                this.createCopy(), criteria.createCopy(),
×
489
                                new Function<ThrowingPredicate<E, ? extends Throwable>, Function<ThrowingPredicate<? super E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>>>() {
×
490
                                        @Override
491
                                        public Function<ThrowingPredicate<? super E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>> apply(final ThrowingPredicate<E, ? extends Throwable> predicate) {
492
                                                return new Function<
×
493
                                                        ThrowingPredicate<? super E, ? extends Throwable>,
494
                                                        ThrowingPredicate<E, ? extends Throwable>
495
                                                >() {
×
496
                                                        @Override
497
                                                        public ThrowingPredicate<E, ? extends Throwable> apply(
498
                                                                final ThrowingPredicate<? super E, ? extends Throwable> otherPredicate
499
                                                        ) {
500
                                                                return Handler.or(predicate, (ThrowingPredicate)otherPredicate);
×
501
                                                        }
502
                                                };
503
                                        }
504
                                },
505
                                newInstance()
×
506
                        );
507
                }
508

509
                public boolean testWithFalseResultForNullEntityOrFalseResultForNullPredicate(final E entity) {
510
                        if (entity != null) {
×
511
                                try {
512
                                        return getPredicateOrFalsePredicateIfPredicateIsNull().test(entity);
×
513
                                } catch (final Throwable exc) {
×
514
                                        return Throwables.INSTANCE.throwException(exc);
×
515
                                }
516
                        }
517
                        return false;
×
518
                }
519

520

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

532
                public boolean testWithFalseResultForNullPredicate(final E entity) {
533
                        try {
534
                                return getPredicateOrFalsePredicateIfPredicateIsNull().test(entity);
×
535
                        } catch (final Throwable exc) {
×
536
                                return Throwables.INSTANCE.throwException(exc);
×
537
                        }
538
                }
539

540
                public boolean testWithTrueResultForNullEntityOrFalseResultForNullPredicate(final E entity) {
541
                        if (entity != null) {
×
542
                                try {
543
                                        return getPredicateOrFalsePredicateIfPredicateIsNull().test(entity);
×
544
                                } catch (final Throwable exc) {
×
545
                                        return Throwables.INSTANCE.throwException(exc);
×
546
                                }
547
                        }
548
                        return true;
×
549
                }
550

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

562
                public boolean testWithTrueResultForNullPredicate(final E entity) {
563
                        try {
564
                                return getPredicateOrTruePredicateIfPredicateIsNull().test(entity);
×
565
                        } catch (final Throwable exc) {
×
566
                                return Throwables.INSTANCE.throwException(exc);
×
567
                        }
568
                }
569

570

571
                protected <E, C extends Simple<E, C>> ThrowingPredicate<E, ? extends Throwable> concat(
572
                        final ThrowingPredicate<E, ? extends Throwable> mainPredicate,
573
                        final Function<ThrowingPredicate<E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>> logicalOperator,
574
                        final ThrowingPredicate<E, ? extends Throwable> otherPredicate
575
                ) {
576
                        if (otherPredicate != null) {
×
577
                                if (mainPredicate != null) {
×
578
                                        return consumeLogicalOperator(otherPredicate, logicalOperator);
×
579
                                }
580
                                return otherPredicate;
×
581
                        }
582
                        return mainPredicate;
×
583
                }
584

585
                protected ThrowingPredicate<E, ? extends Throwable> concat(
586
                        final ThrowingPredicate<E, ? extends Throwable> mainPredicate,
587
                        final ThrowingPredicate<E, ? extends Throwable> otherPredicate
588
                ) {
589
                        final ThrowingPredicate<E, ? extends Throwable> predicate = concat(mainPredicate, this.logicalOperator, otherPredicate);
×
590
                        this.logicalOperator = null;
×
591
                        return predicate;
×
592
                }
593

594
                protected <V> ThrowingPredicate<E, ? extends Throwable> getPredicateWrapper(
595
                        final Function<E, V[]> valueSupplier,
596
                        final ThrowingBiPredicate<V[], Integer, ? extends Throwable> predicate
597
                ) {
598
                        return getPredicateWrapper(new ThrowingPredicate<E, Throwable>() {
×
599
                                @Override
600
                                public boolean test(final E entity) throws Throwable {
601
                                        final V[] array = valueSupplier.apply(entity);
×
602
                                        boolean result = false;
×
603
                                        for (int i = 0; i < array.length; i++) {
×
604
                                                if (result = predicate.test(array, i)) {
×
605
                                                        break;
×
606
                                                }
607
                                        }
608
                                        //logDebug("test for {} return {}", entity, result);
609
                                        return result;
×
610
                                }
611
                        });
612
                }
613

614
                protected C logicOperation(final C leftCriteria, final C rightCriteria,
615
                        final Function<ThrowingPredicate<E, ? extends Throwable>, Function<ThrowingPredicate< ? super E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>>> binaryOperator,
616
                        final C targetCriteria
617
                ) {
618
                        targetCriteria.predicate =
×
619
                                leftCriteria.predicate != null?
620
                                        (rightCriteria.predicate != null?
621
                                                binaryOperator.apply(leftCriteria.predicate).apply(rightCriteria.predicate) :
×
622
                                                leftCriteria.predicate):
623
                                        rightCriteria.predicate;
624
                        return targetCriteria;
×
625
                }
626

627
                protected C newInstance() {
628
                        return (C)Constructors.INSTANCE.newInstanceOf(this.getClass());
×
629
                }
630

631
                @SuppressWarnings("hiding")
632
                <E, C extends Simple<E, C>> ThrowingPredicate<E, ? extends Throwable> consumeLogicalOperator (
633
                        final ThrowingPredicate<E, ? extends Throwable> input,
634
                        final Function<ThrowingPredicate<E, ? extends Throwable>, ThrowingPredicate<E, ? extends Throwable>> logicalOperator
635
                ) {
636
                        if (logicalOperator != null) {
×
637
                                return logicalOperator.apply(input);
×
638
                        }
639
                        return Throwables.INSTANCE.throwException(
×
640
                                "A call to and/or method is necessary before calling {} at {}",
641
                                Thread.currentThread().getStackTrace()[10].getMethodName(),
×
642
                                Thread.currentThread().getStackTrace()[11]
×
643
                        );
644
                }
645

646
                ThrowingPredicate<E, ? extends Throwable> getPredicateWrapper(
647
                        final ThrowingPredicate<E, ? extends Throwable> function
648
                ) {
649
                        if (function != null) {
×
650
                                return new ThrowingPredicate<E, Throwable>() {
×
651
                                        @Override
652
                                        public boolean test(final E entity) throws Throwable {
653
                                                return function.test(entity);
×
654
                                        }
655
                                };
656
                        }
657
                        return null;
×
658
                }
659

660
                private ThrowingPredicate<E, ? extends Throwable> getPredicate(final boolean defaultResult) {
661
                        return this.predicate != null?
×
662
                                new ThrowingPredicate<E, Throwable>() {
×
663
                                        @Override
664
                                        public boolean test(final E entity) throws Throwable {
665
                                                return Simple.this.predicate.test(entity);
×
666
                                        }
667
                                } :
668
                                new ThrowingPredicate<E, Throwable>() {
×
669
                                        @Override
670
                                        public boolean test(final E entity) {
671
                                                return defaultResult;
×
672
                                        }
673
                                };
674
                }
675
        }
676

677
        public static class TestContext<E, C extends Criteria<E, C, ?>> extends Context {
678
                private enum Elements {
1✔
679
                        ENTITY,
1✔
680
                        PREDICATE,
1✔
681
                        TEST_RESULT,
1✔
682
                        THIS_CRITERIA
1✔
683
                }
684

685
                protected TestContext(final C criteria) {
686
                        super();
1✔
687
                        put(Elements.THIS_CRITERIA, criteria);
1✔
688
                }
1✔
689

690
                public static <E, C extends Criteria<E, C, T>, T extends Criteria.TestContext<E, C>> TestContext<E, C> create(final C criteria) {
691
                        return new TestContext<>(criteria);
1✔
692
                }
693

694
                public C getCriteria() {
695
                        return get(Elements.THIS_CRITERIA);
×
696
                }
697

698
                public E getEntity() {
699
                        return get(Elements.ENTITY);
1✔
700
                }
701

702
                public ThrowingPredicate<E, ? extends Throwable> getPredicate() {
703
                        return get(Elements.PREDICATE);
1✔
704
                }
705

706
                public Boolean getResult() {
707
                        return super.get(Elements.TEST_RESULT);
1✔
708
                }
709

710
                <T extends Criteria.TestContext<E, C>> T setEntity(final E entity) {
711
                        put(Elements.ENTITY, entity);
1✔
712
                        return (T) this;
1✔
713
                }
714

715
                <T extends Criteria.TestContext<E, C>> T setPredicate(final ThrowingPredicate<E, ? extends Throwable> predicate) {
716
                        put(Elements.PREDICATE, predicate);
1✔
717
                        return (T)this;
1✔
718
                }
719

720
                <T extends Criteria.TestContext<E, C>> T setResult(final Boolean result) {
721
                        put(Elements.TEST_RESULT, result);
1✔
722
                        return (T) this;
1✔
723
                }
724
        }
725

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