/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.b2international.commons.status;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.Serializable;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;

import com.b2international.commons.CommonsActivator;

/**
 * Common methods and functions to work with {@link IStatus},
 * {@link SerializableStatus} and {@link SerializableMultiStatus} instances much
 * more easily.
 * 
 * @since 3.2
 */
public final class Statuses {

	private Statuses() {
	}

	/**
	 * Returns the {@link Status#CANCEL_STATUS} instance.
	 * 
	 * @return
	 */
	public static IStatus cancel() {
		return Status.CANCEL_STATUS;
	}

	/**
	 * Returns the {@link Status#OK_STATUS} instance.
	 * 
	 * @return
	 */
	public static IStatus ok() {
		return Status.OK_STATUS;
	}

	/**
	 * Returns a new {@link IStatus} instance with {@link IStatus#ERROR}
	 * severity, {@link CommonsActivator#PLUGIN_ID} as pluginId, and with the
	 * given message.
	 * 
	 * @param message
	 * @return
	 */
	public static IStatus error(final String message) {
		return new Status(IStatus.ERROR, CommonsActivator.PLUGIN_ID, message);
	}

	/**
	 * Returns a new {@link IStatus} instance with {@link IStatus#WARNING}
	 * severity, {@link CommonsActivator#PLUGIN_ID} as pluginId, and with the
	 * given message.
	 * 
	 * @param message
	 * @return
	 */
	public static IStatus warn(final String message) {
		return new Status(IStatus.WARNING, CommonsActivator.PLUGIN_ID, message);
	}

	/**
	 * Returns a new {@link IStatus} instance with {@link IStatus#INFO}
	 * severity, {@link CommonsActivator#PLUGIN_ID} as pluginId, and with the
	 * given message.
	 * 
	 * @param message
	 * @return
	 */
	public static IStatus info(final String message) {
		return new Status(IStatus.INFO, CommonsActivator.PLUGIN_ID, message);
	}

	/**
	 * Returns a new {@link IStatus} instance with {@link IStatus#ERROR}
	 * severity, and with the given pluginId and message.
	 * 
	 * @param message
	 * @return
	 */
	public static IStatus error(final String pluginId, final String message) {
		return new Status(IStatus.ERROR, pluginId, message);
	}

	/**
	 * Returns a new {@link IStatus} instance with {@link IStatus#WARNING}
	 * severity, and with the given pluginId and message.
	 * 
	 * @param message
	 * @return
	 */
	public static IStatus warn(final String pluginId, final String message) {
		return new Status(IStatus.WARNING, pluginId, message);
	}

	/**
	 * Returns a new {@link IStatus} instance with {@link IStatus#INFO}
	 * severity, and with the given pluginId and message.
	 * 
	 * @param message
	 * @return
	 */
	public static IStatus info(final String pluginId, final String message) {
		return new Status(IStatus.INFO, pluginId, message);
	}

	/**
	 * Returns anew {@link IStatus} instanceof with {@link IStatus#ERROR}
	 * severity, and with the given pluginId, message, a throwable.
	 * 
	 * @param pluginId
	 * @param message
	 * @param t
	 * @return
	 */
	public static IStatus error(final String pluginId, final String message, final Throwable t) {
		return new Status(IStatus.ERROR, pluginId, message, t);
	}

	/**
	 * Returns a serializable {@link IStatus} implementation with
	 * {@link IStatus#OK} severity.
	 * 
	 * @return
	 */
	public static IStatus serializableOk() {
		return SerializableStatus.OK_STATUS;
	}

	/**
	 * Returns a serializable {@link IStatus} implementation with
	 * {@link IStatus#CANCEL} severity.
	 * 
	 * @return
	 */
	public static IStatus serializableCancel() {
		return SerializableStatus.CANCEL_STATUS;
	}

	/**
	 * Wraps the given {@link IStatus} instance as serializable {@link IStatus}.
	 * 
	 * @param status
	 * @return
	 */
	public static SerializableStatus toSerializable(final IStatus status) {
		return new SerializableStatus(status);
	}

	/**
	 * Returns with a copy of the status argument.
	 * 
	 * @param status
	 *            the status to copy.
	 * @return the cloned status.
	 */
	public static IStatus copyOf(final IStatus status) {
		final IStatus copy;
		if (isMultiStatus(status)) {
			copy = new MultiStatus(status.getPlugin(), status.getCode(), status.getMessage(), status.getException());
			for (final IStatus childStatus : ((MultiStatus) status).getChildren()) {
				((MultiStatus) copy).add(copyOf(childStatus));
			}
		} else {
			copy = new Status(status.getSeverity(), status.getPlugin(), status.getCode(), status.getMessage(),
					status.getException());
		}
		return status instanceof Serializable ? toSerializable(copy) : copy;
	}

	private static boolean isMultiStatus(final IStatus status) {
		return checkNotNull(status, "status").isMultiStatus() && status instanceof MultiStatus;
	}

}
@@ -4450,24 +4450,440 @@
 tatus);%0A%09%7D%0A%0A
+%09/**%0A%09 * Adds the content of the given %7B@link IStatus%7D to the specified %7B@link SerializableMultiStatus%7D.%0A%09 * %0A%09 * @param multiStatus%0A%09 * @param status%0A%09 */%0A%09public static void mergeInto(final SerializableMultiStatus multiStatus, final IStatus status) %7B%0A%09%09if (status instanceof SerializableStatus) %7B%0A%09%09%09multiStatus.merge((SerializableStatus) status);%0A%09%09%7D else %7B%0A%09%09%09multiStatus.merge(toSerializable(status));%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09/**%0A%09 * Ret

Add convenient method for handling serializable multi statuses