/* * The contents of this file are subject to the terms of the Common Development and * Distribution License (the License). You may not use this file except in compliance with the * License. * * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the * specific language governing permission and limitations under the License. * * When distributing Covered Software, include this CDDL Header Notice in each file and include * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL * Header, with the fields enclosed by brackets [] replaced by your own identifying * information: "Portions Copyright [year] [name of copyright owner]". * * Copyright 2008 Sun Microsystems, Inc. * Portions Copyright 2014-2015 ForgeRock AS. */ package com.forgerock.opendj.cli; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; /** * The result of running a {@link Menu}. The result indicates to the * application how it should proceed: * * * @param * The type of result value(s) contained in success results. Use Void if success results should * not contain values. */ public final class MenuResult { /** The type of result returned from the menu. */ private static enum Type { /** * The user selected an option which did not return a result, * so the menu should be displayed again. */ AGAIN, /** * The user did not select an option and instead * chose to cancel the current task. */ CANCEL, /** * The user did not select an option and instead * chose to quit the entire application. */ QUIT, /** * The user selected an option which succeeded * and returned one or more result values. */ SUCCESS } /** * Creates a new menu result indicating that the menu should be displayed again. A good example of this is when a * user chooses to view some help. Normally, after the help is displayed, the user is allowed to select another * option. * * @param * The type of result value(s) contained in success results. Use Void if success results * should not contain values. * @return Returns a new menu result indicating that the menu should be displayed again. */ public static MenuResult again() { return new MenuResult<>(Type.AGAIN, Collections. emptyList()); } /** * Creates a new menu result indicating that the user chose to cancel any task currently in progress and go back to * the previous main menu if applicable. * * @param * The type of result value(s) contained in success results. Use Void if success results * should not contain values. * @return Returns a new menu result indicating that the user chose to cancel any task currently in progress and go * back to the previous main menu if applicable. */ public static MenuResult cancel() { return new MenuResult<>(Type.CANCEL, Collections. emptyList()); } /** * Creates a new menu result indicating that the user chose to quit the application and cancel all outstanding * tasks. * * @param * The type of result value(s) contained in success results. Use Void if success results * should not contain values. * @return Returns a new menu result indicating that the user chose to quit the application and cancel all * outstanding tasks. */ public static MenuResult quit() { return new MenuResult<>(Type.QUIT, Collections. emptyList()); } /** * Creates a new menu result indicating that the user chose to apply any task currently in progress and go back to * the previous menu if applicable. The menu result will not contain any result values. * * @param * The type of result value(s) contained in success results. Use Void if success results * should not contain values. * @return Returns a new menu result indicating that the user chose to apply any task currently in progress and go * back to the previous menu if applicable.The menu result will not contain any result values. */ public static MenuResult success() { return success(Collections. emptySet()); } /** * Creates a new menu result indicating that the user chose to apply any task currently in progress and go back to * the previous menu if applicable. The menu result will contain the provided values, which can be retrieved using * {@link #getValue()} or {@link #getValues()}. * * @param * The type of the result values. * @param values * The result values. * @return Returns a new menu result indicating that the user chose to apply any task currently in progress and go * back to the previous menu if applicable. The menu result will contain the provided values, which can be * retrieved using {@link #getValue()} or {@link #getValues()}. */ public static MenuResult success(Collection values) { return new MenuResult<>(Type.SUCCESS, new ArrayList<>(values)); } /** * Creates a new menu result indicating that the user chose to apply any task currently in progress and go back to * the previous menu if applicable. The menu result will contain the provided value, which can be retrieved using * {@link #getValue()} or {@link #getValues()}. * * @param * The type of the result value. * @param value * The result value. * @return Returns a new menu result indicating that the user chose to apply any task currently in progress and go * back to the previous menu if applicable. The menu result will contain the provided value, which can be * retrieved using {@link #getValue()} or {@link #getValues()}. */ public static MenuResult success(T value) { return success(Collections.singleton(value)); } /** The type of result returned from the menu. */ private final Type type; /** The menu result value(s). */ private final Collection values; /** Private constructor. */ private MenuResult(Type type, Collection values) { this.type = type; this.values = values; } /** * Gets the menu result value if this is a menu result indicating success. * * @return Returns the menu result value, or null if there was no result value or if this is not a * success menu result. * @see #isSuccess() */ public T getValue() { if (!values.isEmpty()) { return values.iterator().next(); } return null; } /** * Gets the menu result values if this is a menu result indicating success. * * @return Returns the menu result values, which may be empty if there were no result values or if this is not a * success menu result. * @see #isSuccess() */ public Collection getValues() { return new ArrayList<>(values); } /** * Determines if this menu result indicates that the menu should be displayed again. A good example of this is when * a user chooses to view some help. Normally, after the help is displayed, the user is allowed to select another * option. * * @return Returns true if this menu result indicates that the menu should be displayed again. */ public boolean isAgain() { return type == Type.AGAIN; } /** * Determines if this menu result indicates that the user chose to cancel any task currently in progress and go back * to the previous main menu if applicable. * * @return Returns true if this menu result indicates that the user chose to cancel any task currently * in progress and go back to the previous main menu if applicable. */ public boolean isCancel() { return type == Type.CANCEL; } /** * Determines if this menu result indicates that the user chose to quit the application and cancel all outstanding * tasks. * * @return Returns true if this menu result indicates that the user chose to quit the application and * cancel all outstanding tasks. */ public boolean isQuit() { return type == Type.QUIT; } /** * Determines if this menu result indicates that the user chose to apply any task currently in progress and go back * to the previous menu if applicable. Any result values can be retrieved using the {@link #getValue()} or * {@link #getValues()} methods. * * @return Returns true if this menu result indicates that the user chose to apply any task currently * in progress and go back to the previous menu if applicable. * @see #getValue() * @see #getValues() */ public boolean isSuccess() { return type == Type.SUCCESS; } @Override public String toString() { return getClass().getSimpleName() + "(type=" + type + ", values=" + values + ")"; } }