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

com.google.cloud.storage.ChannelSession Maven / Gradle / Ivy

There is a newer version: 2.45.0
Show newest version
/*
 * Copyright 2022 Google LLC
 *
 * 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 com.google.cloud.storage;

import com.google.api.core.ApiFunction;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
import com.google.api.core.SettableApiFuture;
import com.google.cloud.storage.BufferedReadableByteChannelSession.BufferedReadableByteChannel;
import com.google.cloud.storage.BufferedWritableByteChannelSession.BufferedWritableByteChannel;
import com.google.cloud.storage.UnbufferedReadableByteChannelSession.UnbufferedReadableByteChannel;
import com.google.cloud.storage.UnbufferedWritableByteChannelSession.UnbufferedWritableByteChannel;
import com.google.common.base.MoreObjects;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.function.BiFunction;

class ChannelSession {
  private final Object channelInitSyncObj = new ChannelSessionInitLockObject();

  private final ApiFuture startFuture;
  private final ApiFunction f;
  private final SettableApiFuture resultFuture;

  private volatile ApiFuture channelFuture;

  private ChannelSession(
      ApiFuture startFuture, BiFunction, ChannelT> f) {
    this.startFuture = startFuture;
    this.resultFuture = SettableApiFuture.create();
    this.f = (s) -> f.apply(s, resultFuture);
  }

  public ApiFuture openAsync() {
    ApiFuture result = channelFuture;
    if (result != null) {
      return result;
    }

    synchronized (channelInitSyncObj) {
      if (channelFuture == null) {
        channelFuture = ApiFutures.transform(startFuture, f, MoreExecutors.directExecutor());
      }
      return channelFuture;
    }
  }

  public ApiFuture getResult() {
    return resultFuture;
  }

  static final class UnbufferedReadSession
      extends ChannelSession
      implements UnbufferedReadableByteChannelSession {

    UnbufferedReadSession(
        ApiFuture startFuture,
        BiFunction, UnbufferedReadableByteChannel> f) {
      super(startFuture, f);
    }
  }

  static final class BufferedReadSession
      extends ChannelSession
      implements BufferedReadableByteChannelSession {

    BufferedReadSession(
        ApiFuture startFuture,
        BiFunction, BufferedReadableByteChannel> f) {
      super(startFuture, f);
    }
  }

  static final class UnbufferedWriteSession
      extends ChannelSession
      implements UnbufferedWritableByteChannelSession {

    UnbufferedWriteSession(
        ApiFuture startFuture,
        BiFunction, UnbufferedWritableByteChannel> f) {
      super(startFuture, f);
    }
  }

  static final class BufferedWriteSession
      extends ChannelSession
      implements BufferedWritableByteChannelSession {

    BufferedWriteSession(
        ApiFuture startFuture,
        BiFunction, BufferedWritableByteChannel> f) {
      super(startFuture, f);
    }
  }

  private static final class ChannelSessionInitLockObject {
    private ChannelSessionInitLockObject() {}

    @Override
    public String toString() {
      return MoreObjects.toStringHelper(this).toString();
    }
  }
}