All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.immutables.criteria.repository.rxjava2.RxJavaMappers Maven / Gradle / Ivy

/*
 * Copyright 2019 Immutables Authors and Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.immutables.criteria.repository.rxjava2;

import io.reactivex.Flowable;
import io.reactivex.Maybe;
import io.reactivex.Single;
import org.immutables.criteria.backend.Backend;
import org.immutables.criteria.expression.ImmutableQuery;
import org.immutables.criteria.expression.Query;
import org.immutables.criteria.repository.MapperFunction3;
import org.immutables.criteria.repository.MapperFunction4;
import org.immutables.criteria.repository.MapperFunction5;
import org.immutables.criteria.repository.Tuple;
import org.immutables.criteria.repository.reactive.ReactiveMapper1;
import org.immutables.criteria.repository.reactive.ReactiveMapper2;
import org.immutables.criteria.repository.reactive.ReactiveMapper3;
import org.immutables.criteria.repository.reactive.ReactiveMapper4;
import org.immutables.criteria.repository.reactive.ReactiveMapper5;
import org.immutables.criteria.repository.reactive.ReactiveMapperTuple;

import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * Default implementation for various rxjava mappers and their utilities
 */
final class RxJavaMappers {

  static class Mapper1 implements RxJavaMapper1, RxJavaMapper1.DistinctLimitOffset {

    private final ReactiveMapper1 mapper;
    private final RxJavaFetcher.DistinctLimitOffset fetcher;

    Mapper1(Query query, Backend.Session session) {
      this(new ReactiveMapper1<>(query, session));
    }

    private Mapper1(ReactiveMapper1 mapper) {
      this(mapper, RxJavaFetcherDelegate.fromReactive(mapper));
    }

    private Mapper1(ReactiveMapper1 mapper, RxJavaFetcher fetcher) {
      this.mapper = mapper;
      this.fetcher = (RxJavaFetcher.DistinctLimitOffset) fetcher;
    }

    @Override
    public DistinctLimitOffset> asOptional() {
      return new Mapper1<>(mapper.asOptional());
    }

    @Override
    public Flowable fetch() {
      return fetcher.fetch();
    }

    @Override
    public Single one() {
      return fetcher.one();
    }

    @Override
    public Maybe oneOrNone() {
      return fetcher.oneOrNone();
    }

    @Override
    public Single exists() {
      return fetcher.exists();
    }

    @Override
    public Single count() {
      return fetcher.count();
    }

    @Override
    public LimitOffset distinct() {
      return new Mapper1<>(mapper, fetcher.distinct());
    }

    @Override
    public Offset limit(long limit) {
      return new Mapper1<>(mapper, fetcher.limit(limit));
    }

    @Override
    public RxJavaMapper1 offset(long offset) {
      return new Mapper1<>(mapper, fetcher.offset(offset));
    }
  }

  static class Mapper2 implements RxJavaMapper2, RxJavaMapper2.DistinctLimitOffset {

    private final ImmutableQuery query;
    private final Backend.Session session;

    Mapper2(Query query, Backend.Session session) {
      this.query = ImmutableQuery.copyOf(query);
      this.session = session;
    }

    @Override
    public  RxJavaFetcher map(BiFunction mapFn) {
      ReactiveMapper2 delegate = new ReactiveMapper2<>(query, session);
      return RxJavaFetcherDelegate.fromReactive(delegate.map(mapFn));
    }

    @Override
    public  RxJavaFetcher map(Function mapFn) {
      ReactiveMapper2 delegate = new ReactiveMapper2<>(query, session);
      return RxJavaFetcherDelegate.fromReactive(delegate.map(mapFn));
    }

    @Override
    public LimitOffset distinct() {
      return new Mapper2<>(query.withDistinct(true), session);
    }

    @Override
    public Offset limit(long limit) {
      return new Mapper2<>(query.withLimit(limit), session);
    }

    @Override
    public RxJavaMapper2 offset(long offset) {
      return new Mapper2<>(query.withOffset(offset), session);
    }
  }

  static class Mapper3 implements RxJavaMapper3, RxJavaMapper3.DistinctLimitOffset {

    private final ImmutableQuery query;
    private final Backend.Session session;

    Mapper3(Query query, Backend.Session session) {
      this.query = ImmutableQuery.copyOf(query);
      this.session = session;
    }

    @Override
    public  RxJavaFetcher map(MapperFunction3 mapFn) {
      ReactiveMapper3 delegate = new ReactiveMapper3<>(query, session);
      return RxJavaFetcherDelegate.fromReactive(delegate.map(mapFn));
    }

    @Override
    public  RxJavaFetcher map(Function mapFn) {
      ReactiveMapper3 delegate = new ReactiveMapper3<>(query, session);
      return RxJavaFetcherDelegate.fromReactive(delegate.map(mapFn));
    }

    @Override
    public LimitOffset distinct() {
      return new Mapper3<>(query.withDistinct(true), session);
    }

    @Override
    public Offset limit(long limit) {
      return new Mapper3<>(query.withLimit(limit), session);
    }

    @Override
    public RxJavaMapper3 offset(long offset) {
      return new Mapper3<>(query.withOffset(offset), session);
    }
  }

  static class Mapper4 implements RxJavaMapper4, RxJavaMapper4.DistinctLimitOffset {

    private final ImmutableQuery query;
    private final Backend.Session session;

    Mapper4(Query query, Backend.Session session) {
      this.query = ImmutableQuery.copyOf(query);
      this.session = session;
    }

    @Override
    public  RxJavaFetcher map(MapperFunction4 mapFn) {
      ReactiveMapper4 delegate = new ReactiveMapper4<>(query, session);
      return RxJavaFetcherDelegate.fromReactive(delegate.map(mapFn));
    }

    @Override
    public  RxJavaFetcher map(Function mapFn) {
      ReactiveMapper4 delegate = new ReactiveMapper4<>(query, session);
      return RxJavaFetcherDelegate.fromReactive(delegate.map(mapFn));
    }

    @Override
    public LimitOffset distinct() {
      return new Mapper4<>(query.withDistinct(true), session);
    }

    @Override
    public Offset limit(long limit) {
      return new Mapper4<>(query.withLimit(limit), session);
    }

    @Override
    public RxJavaMapper4 offset(long offset) {
      return new Mapper4<>(query.withOffset(offset), session);
    }
  }

  static class Mapper5 implements RxJavaMapper5, RxJavaMapper5.DistinctLimitOffset {

    private final ImmutableQuery query;
    private final Backend.Session session;

    Mapper5(Query query, Backend.Session session) {
      this.query = ImmutableQuery.copyOf(query);
      this.session = session;
    }

    @Override
    public  RxJavaFetcher map(MapperFunction5 mapFn) {
      ReactiveMapper5 delegate = new ReactiveMapper5<>(query, session);
      return RxJavaFetcherDelegate.fromReactive(delegate.map(mapFn));
    }

    @Override
    public  RxJavaFetcher map(Function mapFn) {
      ReactiveMapper5 delegate = new ReactiveMapper5<>(query, session);
      return RxJavaFetcherDelegate.fromReactive(delegate.map(mapFn));
    }

    @Override
    public LimitOffset distinct() {
      return new Mapper5<>(query.withDistinct(true), session);
    }

    @Override
    public Offset limit(long limit) {
      return new Mapper5<>(query.withLimit(limit), session);
    }

    @Override
    public RxJavaMapper5 offset(long offset) {
      return new Mapper5<>(query.withOffset(offset), session);
    }
  }

  static class MapperTuple implements RxJavaMapperTuple, RxJavaMapperTuple.DistinctLimitOffset {
    private final ImmutableQuery query;
    private final Backend.Session session;

    MapperTuple(Query query, Backend.Session session) {
      this.query = ImmutableQuery.copyOf(query);
      this.session = session;
    }

    @Override
    public  RxJavaFetcher map(Function mapFn) {
      ReactiveMapperTuple mapper = new ReactiveMapperTuple(query, session);
      return RxJavaFetcherDelegate.fromReactive(mapper.map(mapFn));
    }

    @Override
    public LimitOffset distinct() {
      return new MapperTuple(query.withDistinct(true), session);
    }

    @Override
    public Offset limit(long limit) {
      return new MapperTuple(query.withLimit(limit), session);
    }

    @Override
    public RxJavaMapperTuple offset(long offset) {
      return new MapperTuple(query.withOffset(offset), session);
    }

  }

  private RxJavaMappers() {}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy