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

org.apache.crunch.fn.SFunctions Maven / Gradle / Ivy

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.crunch.fn;

import org.apache.spark.api.java.function.DoubleFlatMapFunction;
import org.apache.spark.api.java.function.DoubleFunction;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.FlatMapFunction2;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import scala.Tuple2;

/**
 * Utility methods for wrapping existing Spark Java API Functions for
 * Crunch compatibility.
 */
public final class SFunctions {

  public static  SFunction wrap(final Function f) {
    return new SFunction() {
      @Override
      public R call(T t) throws Exception {
        return f.call(t);
      }
    };
  }

  public static  SFunction2 wrap(final Function2 f) {
    return new SFunction2() {
      @Override
      public R call(K k, V v) throws Exception {
        return f.call(k, v);
      }
    };
  }

  public static  SPairFunction wrap(final PairFunction f) {
    return new SPairFunction() {
      @Override
      public Tuple2 call(T t) throws Exception {
        return f.call(t);
      }
    };
  }

  public static  SFlatMapFunction wrap(final FlatMapFunction f) {
    return new SFlatMapFunction() {
      @Override
      public Iterable call(T t) throws Exception {
        return f.call(t);
      }
    };
  }

  public static  SFlatMapFunction2 wrap(final FlatMapFunction2 f) {
    return new SFlatMapFunction2() {
      @Override
      public Iterable call(K k, V v) throws Exception {
        return f.call(k, v);
      }
    };
  }

  public static  SDoubleFunction wrap(final DoubleFunction f) {
    return new SDoubleFunction() {
      @Override
      public double call(T t) throws Exception {
        return f.call(t);
      }
    };
  }

  public static  SDoubleFlatMapFunction wrap(final DoubleFlatMapFunction f) {
    return new SDoubleFlatMapFunction() {
      @Override
      public Iterable call(T t) throws Exception {
        return f.call(t);
      }
    };
  }

  private SFunctions() {}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy