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

org.apache.hudi.common.data.HoodieBaseListData 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.hudi.common.data;

import org.apache.hudi.common.util.Either;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Data representation of either a stream or a list of objects with Type T.
 *
 * @param  Object value type.
 */
public abstract class HoodieBaseListData {

  protected final Either, List> data;
  protected final boolean lazy;

  protected HoodieBaseListData(List data, boolean lazy) {
    this.data = lazy ? Either.left(data.stream().parallel()) : Either.right(data);
    this.lazy = lazy;
  }

  protected HoodieBaseListData(Stream dataStream, boolean lazy) {
    // NOTE: In case this container is being instantiated by an eager parent, we have to
    //       pre-materialize the stream
    this.data = lazy ? Either.left(dataStream) : Either.right(dataStream.collect(Collectors.toList()));
    this.lazy = lazy;
  }

  protected Stream asStream() {
    return lazy ? data.asLeft() : data.asRight().parallelStream();
  }

  protected boolean isEmpty() {
    if (lazy) {
      return data.asLeft().findAny().isPresent();
    } else {
      return data.asRight().isEmpty();
    }
  }

  protected long count() {
    if (lazy) {
      return data.asLeft().count();
    } else {
      return data.asRight().size();
    }
  }

  protected List collectAsList() {
    if (lazy) {
      return data.asLeft().collect(Collectors.toList());
    } else {
      return data.asRight();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy