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

org.immutables.criteria.repository.Mappers 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;

import java.util.function.Function;

/**
 * Set of functions to process a {@link Tuple}
 */
public final class Mappers {
  private Mappers() {}


  public static  Function fromTuple() {
    return tuple -> {
      if (tuple.values().size() != 1) {
        throw new IllegalArgumentException(String.format("Expected tuple of size 1 got %d", tuple.values().size()));
      }

      @SuppressWarnings("unchecked")
      R result = (R) tuple.values().get(0);
      return result;
    };
  }

  @SuppressWarnings("unchecked")
  public static  Function fromTuple(MapperFunction2 fn) {
    return tuple -> {
      if (tuple.values().size() != 2) {
        throw new IllegalArgumentException(String.format("Expected tuple of size 2 got %d", tuple.values().size()));
      }

      T1 t1 = (T1) tuple.values().get(0);
      T2 t2 = (T2) tuple.values().get(1);

      return fn.apply(t1, t2);
    };
  }

  @SuppressWarnings("unchecked")
  public static  Function fromTuple(MapperFunction3 fn) {
    return tuple -> {
      if (tuple.values().size() != 3) {
        throw new IllegalArgumentException(String.format("Expected tuple of size 3 got %d", tuple.values().size()));
      }

      T1 t1 = (T1) tuple.values().get(0);
      T2 t2 = (T2) tuple.values().get(1);
      T3 t3 = (T3) tuple.values().get(2);
      return fn.apply(t1, t2, t3);
    };
  }

  @SuppressWarnings("unchecked")
  public static  Function fromTuple(MapperFunction4 fn) {
    return tuple -> {
      if (tuple.values().size() != 4) {
        throw new IllegalArgumentException(String.format("Expected tuple of size 4 got %d", tuple.values().size()));
      }

      T1 t1 = (T1) tuple.values().get(0);
      T2 t2 = (T2) tuple.values().get(1);
      T3 t3 = (T3) tuple.values().get(2);
      T4 t4 = (T4) tuple.values().get(3);
      return fn.apply(t1, t2, t3, t4);
    };
  }

  @SuppressWarnings("unchecked")
  public static  Function fromTuple(MapperFunction5 fn) {
    return tuple -> {
      if (tuple.values().size() != 5) {
        throw new IllegalArgumentException(String.format("Expected tuple of size 5 got %d", tuple.values().size()));
      }

      T1 t1 = (T1) tuple.values().get(0);
      T2 t2 = (T2) tuple.values().get(1);
      T3 t3 = (T3) tuple.values().get(2);
      T4 t4 = (T4) tuple.values().get(3);
      T5 t5 = (T5) tuple.values().get(4);
      return fn.apply(t1, t2, t3, t4, t5);
    };
  }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy