/*
 * 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.flink.runtime.state;

import org.apache.flink.util.LambdaUtil;

import org.apache.flink.shaded.guava18.com.google.common.base.Joiner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RunnableFuture;

/**
 * Helpers for {@link StateObject} related code.
 */
public class StateUtil {

	private static final Logger LOG = LoggerFactory.getLogger(StateUtil.class);

	private StateUtil() {
		throw new AssertionError();
	}

	/**
	 * Returns the size of a state object
	 *
	 * @param handle The handle to the retrieved state
	 */
	public static long getStateSize(StateObject handle) {
		return handle == null ? 0 : handle.getStateSize();
	}

	/**
	 * Iterates through the passed state handles and calls discardState() on each handle that is not null. All
	 * occurring exceptions are suppressed and collected until the iteration is over and emitted as a single exception.
	 *
	 * @param handlesToDiscard State handles to discard. Passed iterable is allowed to deliver null values.
	 * @throws Exception exception that is a collection of all suppressed exceptions that were caught during iteration
	 */
	public static void bestEffortDiscardAllStateObjects(
		Iterable<? extends StateObject> handlesToDiscard) throws Exception {
		LambdaUtil.applyToAllWhileSuppressingExceptions(handlesToDiscard, StateObject::discardState);
	}

	/**
	 * Discards the given state future by first trying to cancel it. If this is not possible, then
	 * the state object contained in the future is calculated and afterwards discarded.
	 *
	 * @param stateFuture to be discarded
	 * @throws Exception if the discard operation failed
	 */
	public static void discardStateFuture(Future<? extends StateObject> stateFuture) throws Exception {
		if (null != stateFuture) {
			if (!stateFuture.cancel(true)) {

				try {
					// We attempt to get a result, in case the future completed before cancellation.
					if (stateFuture instanceof RunnableFuture<?> && !stateFuture.isDone()) {
						((RunnableFuture<?>) stateFuture).run();
					}
					StateObject stateObject = stateFuture.get();
					if (null != stateObject) {
						stateObject.discardState();
					}

				} catch (CancellationException | ExecutionException ex) {
					LOG.debug("Cancelled execution of snapshot future runnable. Cancellation produced the following " +
						"exception, which is expected an can be ignored.", ex);
				}
			}
		}
	}

	/**
	 * Creates an {@link RuntimeException} that signals that an operation did not get the type of
	 * {@link StateObject} that was expected. This can mostly happen when a different {@link
	 * StateBackend} from the one that was used for taking a checkpoint/savepoint is used when
	 * restoring.
	 */
	@SuppressWarnings("unchecked")
	public static RuntimeException unexpectedStateHandleException(
			Class<? extends StateObject> expectedStateHandleClass,
			Class<? extends StateObject> actualStateHandleClass) {
		return unexpectedStateHandleException(
				new Class[]{expectedStateHandleClass},
				actualStateHandleClass);
	}

	/**
	 * Creates a {@link RuntimeException} that signals that an operation did not get the type of
	 * {@link StateObject} that was expected. This can mostly happen when a different {@link
	 * StateBackend} from the one that was used for taking a checkpoint/savepoint is used when
	 * restoring.
	 */
	public static RuntimeException unexpectedStateHandleException(
			Class<? extends StateObject>[] expectedStateHandleClasses,
			Class<? extends StateObject> actualStateHandleClass) {

		return new IllegalStateException("Unexpected state handle type, expected one of: " +
				Joiner.on(", ").join(expectedStateHandleClasses)
				+ ", but found: " + actualStateHandleClass + ". "
				+ "This can mostly happen when a different StateBackend from the one "
				+ "that was used for taking a checkpoint/savepoint is used when restoring.");
	}
}

@@ -1449,16 +1449,17 @@
 e object
+.
 %0A%09 *%0A%09 *

Fix Checkstyle in StateUtil