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

burningwave / reflection / #57

07 Sep 2023 07:45AM UTC coverage: 60.982% (-0.2%) from 61.136%
#57

push

Roberto-Gentili
Releasing new version

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 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 C negate() {
510
                        final ThrowingPredicate<E, ? extends Throwable> predicate = this.predicate;
×
511
                        this.predicate = new ThrowingPredicate<E, Throwable>() {
×
512

513
                                @Override
514
                                public boolean test(E entity) throws Throwable {
515
                                        return !predicate.test(entity);
×
516
                                }
517

518
                        };
519
                        return (C)this;
×
520
                }
521

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

533

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

545
                public boolean testWithFalseResultForNullPredicate(final E entity) {
546
                        try {
547
                                return getPredicateOrFalsePredicateIfPredicateIsNull().test(entity);
×
548
                        } catch (final Throwable exc) {
×
549
                                return Throwables.INSTANCE.throwException(exc);
×
550
                        }
551
                }
552

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

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

575
                public boolean testWithTrueResultForNullPredicate(final E entity) {
576
                        try {
577
                                return getPredicateOrTruePredicateIfPredicateIsNull().test(entity);
×
578
                        } catch (final Throwable exc) {
×
579
                                return Throwables.INSTANCE.throwException(exc);
×
580
                        }
581
                }
582

583

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

598
                protected ThrowingPredicate<E, ? extends Throwable> concat(
599
                        final ThrowingPredicate<E, ? extends Throwable> mainPredicate,
600
                        final ThrowingPredicate<E, ? extends Throwable> otherPredicate
601
                ) {
602
                        final ThrowingPredicate<E, ? extends Throwable> predicate = concat(mainPredicate, this.logicalOperator, otherPredicate);
×
603
                        this.logicalOperator = null;
×
604
                        return predicate;
×
605
                }
606

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

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

640
                protected C newInstance() {
641
                        return (C)Constructors.INSTANCE.newInstanceOf(this.getClass());
×
642
                }
643

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

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

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

690
        public static class TestContext<E, C extends Criteria<E, C, ?>> extends Context {
691
                private enum Elements {
1✔
692
                        ENTITY,
1✔
693
                        PREDICATE,
1✔
694
                        TEST_RESULT,
1✔
695
                        THIS_CRITERIA
1✔
696
                }
697

698
                protected TestContext(final C criteria) {
699
                        super();
1✔
700
                        put(Elements.THIS_CRITERIA, criteria);
1✔
701
                }
1✔
702

703
                public static <E, C extends Criteria<E, C, T>, T extends Criteria.TestContext<E, C>> TestContext<E, C> create(final C criteria) {
704
                        return new TestContext<>(criteria);
1✔
705
                }
706

707
                public C getCriteria() {
708
                        return get(Elements.THIS_CRITERIA);
×
709
                }
710

711
                public E getEntity() {
712
                        return get(Elements.ENTITY);
1✔
713
                }
714

715
                public ThrowingPredicate<E, ? extends Throwable> getPredicate() {
716
                        return get(Elements.PREDICATE);
1✔
717
                }
718

719
                public Boolean getResult() {
720
                        return super.get(Elements.TEST_RESULT);
1✔
721
                }
722

723
                <T extends Criteria.TestContext<E, C>> T setEntity(final E entity) {
724
                        put(Elements.ENTITY, entity);
1✔
725
                        return (T) this;
1✔
726
                }
727

728
                <T extends Criteria.TestContext<E, C>> T setPredicate(final ThrowingPredicate<E, ? extends Throwable> predicate) {
729
                        put(Elements.PREDICATE, predicate);
1✔
730
                        return (T)this;
1✔
731
                }
732

733
                <T extends Criteria.TestContext<E, C>> T setResult(final Boolean result) {
734
                        put(Elements.TEST_RESULT, result);
1✔
735
                        return (T) this;
1✔
736
                }
737
        }
738

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