Skip to content

Commit

Permalink
Merge pull request #392 from gastaldi/features/FORGE-1465
Browse files Browse the repository at this point in the history
FORGE-1465: Introduced org.jboss.forge.addon.ui.result.navigation package
  • Loading branch information
gastaldi committed Feb 27, 2014
2 parents 043ced0 + eb138be commit f062e8c
Show file tree
Hide file tree
Showing 21 changed files with 966 additions and 57 deletions.
Expand Up @@ -40,7 +40,6 @@
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.hints.InputType;
import org.jboss.forge.addon.ui.impl.result.CompositeResultImpl;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UIInputMany;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
Expand Down Expand Up @@ -154,7 +153,7 @@ public Result execute(UIExecutionContext context) throws Exception
}
}

return CompositeResultImpl.from(results);
return Results.aggregate(results);
}

public Result execute(Shell shell, BufferedWriter stdin, String line, int quantity, TimeUnit unit, long startTime)
Expand Down
Expand Up @@ -10,10 +10,11 @@
import java.util.List;

/**
* A {@link CompositeResult} is composed of several {@link Result} objects
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public interface CompositeResult extends Result
{
public List<Result> getResults();
List<Result> getResults();
}
@@ -0,0 +1,84 @@
/**
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.ui.result;

import java.util.List;

import org.jboss.forge.furnace.util.Assert;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
abstract class CompositeResultImpl implements CompositeResult
{
private final List<Result> results;

public CompositeResultImpl(List<Result> results)
{
Assert.notNull(results, "Result list cannot be null");
this.results = results;
}

@Override
public List<Result> getResults()
{
return results;
}

@Override
public String getMessage()
{
throw new UnsupportedOperationException(
"getMessage() should not be called in a CompositeResult. Call getResults() instead.");
}

public static CompositeResult from(List<Result> results)
{
boolean failed = false;
Throwable throwable = null;
for (Result result : results)
{
if (result instanceof Failed)
{
failed = true;
throwable = ((Failed) result).getException();
break;
}
}

if (failed)
return new CompositeResultFailed(results, throwable);
return new CompositeResultSuccess(results);
}

private static class CompositeResultFailed extends CompositeResultImpl implements Result, Failed
{
private final Throwable exception;

public CompositeResultFailed(List<Result> results, Throwable e)
{
super(results);
this.exception = e;
}

@Override
public Throwable getException()
{
return exception;
}
}

private static class CompositeResultSuccess extends CompositeResultImpl implements Result
{
public CompositeResultSuccess(List<Result> results)
{
super(results);
}
}
}
Expand Up @@ -7,8 +7,6 @@

package org.jboss.forge.addon.ui.result;

import org.jboss.forge.addon.ui.command.UICommand;

/**
* The result of a navigation
*
Expand All @@ -17,5 +15,8 @@
*/
public interface NavigationResult
{
Class<? extends UICommand>[] getNext();
/**
* Returns the next entries for this navigation
*/
NavigationResultEntry[] getNext();
}
@@ -0,0 +1,32 @@
/**
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.ui.result;

import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.controller.CommandController;
import org.jboss.forge.furnace.addons.AddonRegistry;

/**
* Encapsulates the {@link UICommand} creation.
*
* This interface should only be used in {@link CommandController} implementations
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public interface NavigationResultEntry
{
/**
* Returns a Command associated with this {@link NavigationResultEntry}
*
* @param addonRegistry the {@link AddonRegistry} instance of this
* @param context the current {@link UIContext}
* @return command instance, never null
*/
UICommand getCommand(AddonRegistry addonRegistry, UIContext context);
}

This file was deleted.

70 changes: 60 additions & 10 deletions ui/api/src/main/java/org/jboss/forge/addon/ui/result/Results.java
Expand Up @@ -7,7 +7,11 @@

package org.jboss.forge.addon.ui.result;

import java.util.ArrayList;
import java.util.List;

import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.result.navigation.NavigationResultBuilder;

/**
* Utilities for creating {@link Result} instances.
Expand Down Expand Up @@ -57,32 +61,38 @@ public static final Result fail(String message, Throwable e)
}

/**
* Create a failed {@link NavigationResult} using the given {@link UICommand} type as the target.
* Create a {@link NavigationResult} using the given {@link UICommand} type as the target.
*/
@SuppressWarnings("unchecked")
public static final NavigationResult navigateTo(Class<? extends UICommand> next)
{
if (next == null)
{
return null;
}
return navigateTo(next, new Class[0]);
NavigationResultBuilder builder = navigationBuilder();
builder.add(next);
return builder.build();
}

/**
* Create a failed {@link NavigationResult} using the given {@link UICommand} array as the target.
* Create a {@link NavigationResult} using the given {@link UICommand} array as the target.
*/
public static final NavigationResult navigateTo(Class<? extends UICommand>[] next)
{
if (next == null)
{
return null;
}
return new NavigationResultImpl(next);
NavigationResultBuilder builder = navigationBuilder();
for (Class<? extends UICommand> type : next)
{
builder.add(type);
}
return builder.build();
}

/**
* Create a failed {@link NavigationResult} using the given {@link UICommand} types as the targets.
* Create a {@link NavigationResult} using the given {@link UICommand} types as the targets.
*/
@SuppressWarnings("unchecked")
public static final NavigationResult navigateTo(Class<? extends UICommand> next,
Expand All @@ -91,10 +101,50 @@ public static final NavigationResult navigateTo(Class<? extends UICommand> next,
if (next == null)
return null;

Class<? extends UICommand>[] all = new Class[1 + additional.length];
all[0] = next;
System.arraycopy(additional, 0, all, 1, additional.length);
return new NavigationResultImpl(all);
NavigationResultBuilder builder = navigationBuilder();
builder.add(next);
for (Class<? extends UICommand> type : additional)
{
builder.add(type);
}
return builder.build();
}

public static NavigationResultBuilder navigationBuilder()
{
NavigationResultBuilder builder = NavigationResultBuilder.create();
return builder;
}

/**
* Aggregates a list of results into one single {@link CompositeResult}
*/
public static CompositeResult aggregate(Iterable<Result> results)
{
List<Result> resultList = toList(results);
return CompositeResultImpl.from(resultList);
}

// TODO: Move to an utils method
private static <T> List<T> toList(Iterable<T> iterable)
{
if (iterable == null)
{
return null;
}
else if (iterable instanceof List)
{
return (List<T>) iterable;
}
else
{
List<T> list = new ArrayList<>();
for (T obj : iterable)
{
list.add(obj);
}
return list;
}
}

private static class SuccessfulResult implements Result
Expand Down
@@ -0,0 +1,67 @@
/**
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.ui.result.navigation;

import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.result.NavigationResultEntry;
import org.jboss.forge.furnace.addons.AddonRegistry;

/**
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
class ClassNavigationResultEntry implements NavigationResultEntry
{
private final Class<? extends UICommand> type;

public ClassNavigationResultEntry(Class<? extends UICommand> type)
{
this.type = type;
}

@Override
public UICommand getCommand(AddonRegistry addonRegistry, UIContext context)
{
return addonRegistry.getServices(type).get();
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ClassNavigationResultEntry other = (ClassNavigationResultEntry) obj;
if (type == null)
{
if (other.type != null)
return false;
}
else if (!type.getName().equals(other.type.getName()))
return false;
return true;
}

@Override
public String toString()
{
return "ClassNavigationResultEntry [type=" + type + "]";
}
}

0 comments on commit f062e8c

Please sign in to comment.