io.reactivex.functions.Predicate Java Examples

The following examples show how to use io.reactivex.functions.Predicate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NullAwayRxSupportNegativeCases.java    From NullAway with MIT License 6 votes vote down vote up
private Observable<Integer> filterThenMapNullableContainerMergesReturns(
    Observable<NullableContainer<String>> observable) {
  return observable
      .filter(
          new Predicate<NullableContainer<String>>() {
            @Override
            public boolean test(NullableContainer<String> container) throws Exception {
              if (perhaps() && container.get() != null) {
                return true;
              } else {
                return (container.get() != null);
              }
            }
          })
      .map(
          new Function<NullableContainer<String>, Integer>() {
            @Override
            public Integer apply(NullableContainer<String> c) throws Exception {
              return c.get().length();
            }
          });
}
 
Example #2
Source File: NullAwayRxSupportNegativeCases.java    From NullAway with MIT License 6 votes vote down vote up
private Observable<Integer> filterWithIfThenMapNullableContainer(
    Observable<NullableContainer<String>> observable) {
  return observable
      .filter(
          new Predicate<NullableContainer<String>>() {
            @Override
            public boolean test(NullableContainer<String> container) throws Exception {
              if (container.get() != null) {
                return true;
              } else {
                return false;
              }
            }
          })
      .map(
          new Function<NullableContainer<String>, Integer>() {
            @Override
            public Integer apply(NullableContainer<String> c) throws Exception {
              return c.get().length();
            }
          });
}
 
Example #3
Source File: RxJavaUtil.java    From WeiXinRecordedDemo with MIT License 6 votes vote down vote up
@SuppressLint("CheckResult")
public static Disposable loop(long period, final OnRxLoopListener listener){
    return Observable.interval(period, TimeUnit.MILLISECONDS)
            .takeWhile(new Predicate<Long>() {
                @Override
                public boolean test(Long aLong) throws Exception {
                    return listener.takeWhile();
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new DisposableObserver<Long>() {
                @Override
                public void onNext(Long l) {
                    listener.onExecute();
                }
                @Override
                public void onComplete() {
                    listener.onFinish();
                }
                @Override
                public void onError(Throwable e) {
                    listener.onError(e);
                }
            });
}
 
Example #4
Source File: SelectionCheckerBase.java    From edslite with GNU General Public License v2.0 6 votes vote down vote up
SelectionCheckerBase(Location location, String selectionString, String[] selectionArgs)
{
    _location = location;
    if (selectionString != null)
    {
        String[] filtNames = selectionString.split(" ");
        int i = 0;
        for (String filtName : filtNames)
        {
            if (selectionArgs == null || i >= selectionArgs.length)
                break;
            Predicate<CachedPathInfo> f = getFilter(filtName, selectionArgs[i++]);
            //if (f == null)
            //    throw new IllegalArgumentException("Unsupported search filter: " + filtName);
            //else
            if(f!=null)
                _filters.add(f);
        }
    }
}
 
Example #5
Source File: DefaultNotificationConfig.java    From AndroidGodEye with Apache License 2.0 6 votes vote down vote up
@Override
public @NonNull
Predicate<PageLifecycleEventInfo> pageloadPredicate() {
    return new Predicate<PageLifecycleEventInfo>() {
        @Override
        public boolean test(PageLifecycleEventInfo info) throws Exception {
            if (info.currentEvent.lifecycleEvent.isSystemLifecycle()) {
                return getPageLifecycleEventCostTime(info) > 2000;
            }
            if ((info.currentEvent.lifecycleEvent == ActivityLifecycleEvent.ON_DRAW
                    || info.currentEvent.lifecycleEvent == FragmentLifecycleEvent.ON_DRAW)) {
                return getPageLifecycleEventCostTime(info) > 2000;
            }
            if ((info.currentEvent.lifecycleEvent == ActivityLifecycleEvent.ON_LOAD
                    || info.currentEvent.lifecycleEvent == FragmentLifecycleEvent.ON_LOAD)) {
                return getPageLifecycleEventCostTime(info) > 8000;
            }
            return false;
        }
    };
}
 
Example #6
Source File: ThreadDumpTest.java    From AndroidGodEye with Apache License 2.0 6 votes vote down vote up
@Test
public void work2() {
    ((TestScheduler) ThreadUtil.computationScheduler()).advanceTimeBy(5, TimeUnit.SECONDS);
    try {
        TestObserver<List<ThreadInfo>> subscriber = new TestObserver<>();
        GodEye.instance().<ThreadDump, List<ThreadInfo>>moduleObservable(GodEye.ModuleName.THREAD).subscribe(subscriber);
        subscriber.assertValue(new Predicate<List<ThreadInfo>>() {
            @Override
            public boolean test(List<ThreadInfo> threadInfos) throws Exception {
                return threadInfos != null && !threadInfos.isEmpty();
            }
        });
    } catch (UninstallException e) {
        Assert.fail();
    }
}
 
Example #7
Source File: NullAwayRxSupportPositiveCases.java    From NullAway with MIT License 6 votes vote down vote up
private Observable<Integer> filterWithIfThenMapNullableContainerNullableOnSomeBranch(
    Observable<NullableContainer<String>> observable) {
  return observable
      .filter(
          new Predicate<NullableContainer<String>>() {
            @Override
            public boolean test(NullableContainer<String> container) throws Exception {
              if (container.get() != null) {
                return true;
              } else {
                return perhaps();
              }
            }
          })
      .map(
          new Function<NullableContainer<String>, Integer>() {
            @Override
            public Integer apply(NullableContainer<String> c) throws Exception {
              // BUG: Diagnostic contains: dereferenced expression
              return c.get().length();
            }
          });
}
 
Example #8
Source File: NullAwayRxSupportNegativeCases.java    From NullAway with MIT License 6 votes vote down vote up
private Observable<NullableContainer<String>> filterThenDistinctUntilChanged(
    Observable<NullableContainer<String>> observable) {
  return observable
      .filter(
          new Predicate<NullableContainer<String>>() {
            @Override
            public boolean test(NullableContainer<String> container) throws Exception {
              return container.get() != null;
            }
          })
      .distinctUntilChanged(
          new BiPredicate<NullableContainer<String>, NullableContainer<String>>() {
            @Override
            public boolean test(NullableContainer<String> nc1, NullableContainer<String> nc2) {
              return nc1.get().length() == nc2.get().length()
                  && nc1.get().contains(nc2.get())
                  && nc2.get().contains(nc1.get());
            }
          });
}
 
Example #9
Source File: NullAwayRxSupportNegativeCases.java    From NullAway with MIT License 6 votes vote down vote up
private Maybe<Integer> testMaybe(Maybe<NullableContainer<String>> maybe) {
  return maybe
      .filter(
          new Predicate<NullableContainer<String>>() {
            @Override
            public boolean test(NullableContainer<String> container) throws Exception {
              return container.get() != null;
            }
          })
      .map(
          new Function<NullableContainer<String>, Integer>() {
            @Override
            public Integer apply(NullableContainer<String> c) throws Exception {
              return c.get().length();
            }
          });
}
 
Example #10
Source File: NullAwayRxSupportPositiveCases.java    From NullAway with MIT License 6 votes vote down vote up
private Observable<Integer> filterWithIfThenMapNullableContainerNullableOnSomeBranchAnyOrder(
    Observable<NullableContainer<String>> observable) {
  return observable
      .filter(
          new Predicate<NullableContainer<String>>() {
            @Override
            public boolean test(NullableContainer<String> container) throws Exception {
              if (container.get() == null) {
                return perhaps();
              } else {
                return true;
              }
            }
          })
      .map(
          new Function<NullableContainer<String>, Integer>() {
            @Override
            public Integer apply(NullableContainer<String> c1) throws Exception {
              // BUG: Diagnostic contains: dereferenced expression
              return c1.get().length();
            }
          });
}
 
Example #11
Source File: RxBusCustomSubscriptionTest.java    From RxBus with Apache License 2.0 6 votes vote down vote up
@Test
public void registerCustomSubscriberWithFilter() throws Exception {
    CustomSubscriber<TestEvent> subscriber = bus
            .obtainSubscriber(TestEvent.class, testEventConsumer)
            .withFilter(new Predicate<TestEvent>() {
                @Override
                public boolean test(TestEvent testEvent) throws Exception {
                    return testEvent == TestEvent.TWO;
                }
            });
    bus.registerSubscriber(observer, subscriber);
    bus.post(TestEvent.ONE);
    verify(testEventConsumer, never()).accept(TestEvent.ONE);
    bus.post(TestEvent.TWO);
    verify(testEventConsumer).accept(TestEvent.TWO);

    bus.unregister(observer);
}
 
Example #12
Source File: FragmentResultPublisherImpl.java    From flowr with Apache License 2.0 6 votes vote down vote up
public Disposable observeResultsForFragment(final String fragmentId, Consumer<ResultResponse> consumer) {
    return publishSubject
            .filter(new Predicate<ResultResponse>() {
                @Override
                public boolean test(ResultResponse resultResponse) throws Exception {
                    return resultResponse.fragmentId.equals(fragmentId);
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(consumer, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    throwable.printStackTrace();
                }
            });
}
 
Example #13
Source File: RxTimer.java    From Tangram-Android with MIT License 6 votes vote down vote up
public void register(final int interval, @NonNull final OnTickListener onTickListener, boolean intermediate) {
    Disposable disposable = this.mIntervalObservable.delaySubscription(intermediate ? 0 : interval * mInterval, TimeUnit.MILLISECONDS).skipWhile(

            new Predicate<Long>() {
                @Override
                public boolean test(Long aLong) throws Exception {
                    return pause;
                }
            }).observeOn(
            AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {

        @Override
        public void accept(Long count) throws Exception {
            Log.d("RxTimerSupportTest", "accept " + " value " + count);
            if (count % interval == 0) {
                if (onTickListener != null) {
                    onTickListener.onTick();
                }
            }
        }
    });
    tickCache.put(onTickListener, disposable);
}
 
Example #14
Source File: GodEyeHelperTest.java    From AndroidGodEye with Apache License 2.0 6 votes vote down vote up
@Test
public void onAppStartEndSuccess() {
    try {
        GodEye.instance().uninstall();
        GodEye.instance().install(GodEyeConfig.noneConfigBuilder().withStartupConfig(new StartupConfig()).build());
        StartupInfo startupInfo = new StartupInfo(StartupInfo.StartUpType.COLD, 3000);
        GodEyeHelper.onAppStartEnd(startupInfo);
        TestObserver testObserver = GodEye.instance().<Startup, StartupInfo>moduleObservable(GodEye.ModuleName.STARTUP).test();
        testObserver.assertValue(new Predicate<StartupInfo>() {
            @Override
            public boolean test(StartupInfo o) throws Exception {
                return startupInfo.startupType.equals(o.startupType) && startupInfo.startupTime == o.startupTime;
            }
        });
    } catch (Throwable e) {
        fail();
    }
}
 
Example #15
Source File: LifeCycleHelper.java    From Tangram-Android with MIT License 6 votes vote down vote up
public static <T, E> LifecycleTransformer<T> bindToLifeCycle(Observable<E> lifecycle,
    final Function<E, E> correspondingEvents) {
    Observable<E> lifecycleCopy = lifecycle.share();
    return new LifecycleTransformer<>(Observable.combineLatest(lifecycle.take(1).map(correspondingEvents),
        lifecycleCopy.skip(1),
        new BiFunction<E, E, Boolean>() {

            @Override
            public Boolean apply(E e, E e2) throws Exception {
                return e.equals(e2);
            }
        }).filter(new Predicate<Boolean>() {
        @Override
        public boolean test(Boolean cmpResult) throws Exception {
            return cmpResult;
        }
    }));
}
 
Example #16
Source File: RxBus.java    From RxBus with Apache License 2.0 6 votes vote down vote up
public <CLASS> Observable<Getter<CLASS>> onGet(final Class<CLASS> theClass) {
    return onEvent(AskedEvent.class)//I wait for an event (askevent) of CLASS
            .filter(new Predicate<AskedEvent>() {
                @Override
                public boolean test(@NonNull AskedEvent askedEvent) throws Exception {
                    return askedEvent.askedObject.equals(theClass);
                }
            })
            .map(new Function<AskedEvent, Getter<CLASS>>() {
                @Override
                public Getter<CLASS> apply(@NonNull AskedEvent o) throws Exception {
                    return new Getter<CLASS>() {
                        //then I send to the listener a Getter (interface)
                        //when the getter is notified, the value is sent to the first subscrier
                        //who called the method `get`
                        @Override
                        public void get(CLASS value) {
                            post(value); //the value is published on the bus
                        }
                    };
                }
            });
}
 
Example #17
Source File: TripRepository.java    From InstantAppSample with Apache License 2.0 6 votes vote down vote up
public Maybe<Trip> getTrip(final String tripId) {
    return getTrips()
            .toObservable()
            .flatMap(new Function<List<Trip>, ObservableSource<? extends Trip>>() {
                @Override
                public ObservableSource<? extends Trip> apply(List<Trip> tripList) throws Exception {
                    return Observable.fromIterable(tripList);
                }
            })
            .filter(new Predicate<Trip>() {
                @Override
                public boolean test(Trip trip) throws Exception {
                    return trip.getId().equals(tripId);
                }
            })
            .singleElement();
}
 
Example #18
Source File: DefaultNotificationConfig.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public @NonNull
Predicate<FpsInfo> fpsPredicate() {
    return new Predicate<FpsInfo>() {
        @Override
        public boolean test(FpsInfo info) throws Exception {
            return (info.currentFps * 1.0 / info.systemFps) < 0.5f;
        }
    };
}
 
Example #19
Source File: DefaultNotificationConfig.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public @NonNull
Predicate<TrafficInfo> trafficPredicate() {
    return new Predicate<TrafficInfo>() {
        @Override
        public boolean test(TrafficInfo info) throws Exception {
            return false;
        }
    };
}
 
Example #20
Source File: DefaultNotificationConfig.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public @NonNull
Predicate<BatteryInfo> batteryPredicate() {
    return new Predicate<BatteryInfo>() {
        @Override
        public boolean test(BatteryInfo info) throws Exception {
            return false;
        }
    };
}
 
Example #21
Source File: DefaultNotificationConfig.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public @NonNull
Predicate<CpuInfo> cpuPredicate() {
    return new Predicate<CpuInfo>() {
        @Override
        public boolean test(CpuInfo info) throws Exception {
            return info.appCpuRatio > 0.8;
        }
    };
}
 
Example #22
Source File: DefaultNotificationConfig.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public @NonNull
Predicate<LeakInfo> leakPredicate() {
    return new Predicate<LeakInfo>() {
        @Override
        public boolean test(LeakInfo info) throws Exception {
            return true;
        }
    };
}
 
Example #23
Source File: DefaultNotificationConfig.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public @NonNull
Predicate<HeapInfo> heapPredicate() {
    return new Predicate<HeapInfo>() {
        @Override
        public boolean test(HeapInfo info) throws Exception {
            return info.allocatedKb * 1.0 / info.maxMemKb > 0.9;
        }
    };
}
 
Example #24
Source File: DefaultNotificationConfig.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public @NonNull
Predicate<PssInfo> pssPredicate() {
    return new Predicate<PssInfo>() {
        @Override
        public boolean test(PssInfo info) throws Exception {
            return false;
        }
    };
}
 
Example #25
Source File: RxLifecycle.java    From RxLifecycle with Apache License 2.0 5 votes vote down vote up
public Observable<Lifecycle.Event> onResume() {
    return onEvent().filter(new Predicate<Lifecycle.Event>() {
        @Override
        public boolean test(@NonNull Lifecycle.Event event) throws Exception {
            return ON_RESUME.equals(event);
        }
    });
}
 
Example #26
Source File: LifeCycleHelper.java    From Tangram-Android with MIT License 5 votes vote down vote up
public static <T, E> LifecycleTransformer<T> bindUntilEvent(Observable<E> lifecycle, final E event) {
    return new LifecycleTransformer<>(lifecycle.filter(new Predicate<E>() {
        @Override
        public boolean test(E e) throws Exception {
            return e.equals(event);
        }
    }));
}
 
Example #27
Source File: DefaultNotificationConfig.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public @NonNull
Predicate<NetworkInfo> networkPredicate() {
    return new Predicate<NetworkInfo>() {
        @Override
        public boolean test(NetworkInfo info) throws Exception {
            return !info.isSuccessful;
        }
    };
}
 
Example #28
Source File: RxBus.java    From RxBus with Apache License 2.0 5 votes vote down vote up
protected  <CLASS> Observable<CLASS> onEvent(final Class<CLASS> theClass) {
    return bus
            .filter(new Predicate<Object>() {
                @Override
                public boolean test(@NonNull Object o) throws Exception {
                    return theClass.isInstance(o) || theClass.equals(o);
                }
            })
            .cast(theClass);
}
 
Example #29
Source File: RxBus.java    From RxBus with Apache License 2.0 5 votes vote down vote up
public Observable<String> onEvent(final String eventName) {
    return bus
            .filter(new Predicate<Object>() {
                @Override
                public boolean test(@NonNull Object anObject) throws Exception {
                    return eventName.equals(anObject);
                }
            })
            .cast(String.class);
}
 
Example #30
Source File: CpuEngine.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Override
public void work() {
    mCompositeDisposable.add(Observable.interval(mIntervalMillis, TimeUnit.MILLISECONDS)
            .subscribeOn(ThreadUtil.computationScheduler())
            .observeOn(ThreadUtil.computationScheduler())
            .map(new Function<Long, CpuInfo>() {
                @Override
                public CpuInfo apply(Long aLong) throws Exception {
                    ThreadUtil.ensureWorkThread("CpuEngine apply");
                    return CpuUsage.getCpuInfo();
                }
            })
            .filter(new Predicate<CpuInfo>() {
                        @Override
                        public boolean test(CpuInfo cpuInfo) throws Exception {
                            return CpuInfo.INVALID != cpuInfo;
                        }
                    }
            )
            .subscribe(new Consumer<CpuInfo>() {
                @Override
                public void accept(CpuInfo food) throws Exception {
                    ThreadUtil.ensureWorkThread("CpuEngine accept");
                    mProducer.produce(food);
                }
            }));
}