Skip to content

Commit

Permalink
Fix #141 Allow to return void with an empty Wave, but the returItem
Browse files Browse the repository at this point in the history
shall be voidItem
  • Loading branch information
sbordes committed Feb 25, 2015
1 parent aacb2fa commit 66ea721
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 34 deletions.
Expand Up @@ -101,7 +101,7 @@ public interface WaveType {
WaveItem<?> returnItem();

/**
* Return the WaveItem that hold the type sent to hold the current WaveType result.
* Set the WaveItem that hold the type sent to hold the current WaveType result.
*
* @param returnItem the return WaveItem to set
*
Expand All @@ -116,6 +116,15 @@ public interface WaveType {
*/
WaveType returnWaveType();

/**
* Set the WaveType that will be used a return, override returnItem and returnAction.
*
* @param returnWaveType the return WaveType to set
*
* @return the wave type to chain method call
*/
WaveType returnWaveType(WaveType returnWaveType);

/**
* Set the Command to call in order to process the returned value.
*
Expand All @@ -132,4 +141,11 @@ public interface WaveType {
*/
Class<? extends Command> returnCommandClass();

/**
* Return only WaveItem that can be used as method parameter.
*
* @return eligible method parameters
*/
List<WaveItem<?>> parameters();

}
Expand Up @@ -62,16 +62,12 @@ public ServiceException(final Throwable t) {

public String getExplanation() {
final StringBuilder sb = new StringBuilder();
if (getCause() != null) {
sb.append(getCause().getClass().getSimpleName());
}
if (getMessage() != null) {
if(getCause() != null){
sb.append(" : ");
}
sb.append(getMessage());
}

if (getCause() != null) {
sb.append(getCause().getClass().getSimpleName());
}

return sb.toString();
}
Expand Down
Expand Up @@ -40,6 +40,8 @@
import org.jrebirth.af.core.exception.ServiceException;
import org.jrebirth.af.core.log.JRLoggerFactory;
import org.jrebirth.af.core.wave.Builders;
import org.jrebirth.af.core.wave.JRebirthItems;
import org.jrebirth.af.core.wave.JRebirthWaves;
import org.jrebirth.af.core.wave.WaveItemBase;

/**
Expand Down Expand Up @@ -150,7 +152,7 @@ protected T call() throws CoreException {
// Call this method with right parameters
res = (T) this.method.invoke(this.service, params.toArray());

if (Void.TYPE.equals(this.method.getReturnType())) {
if (Void.TYPE.equals(this.method.getReturnType()) && this.wave.waveType().returnItem() != JRebirthItems.voidItem) {
// No return wave required because the service method will return nothing (VOID)
LOGGER.log(NO_RETURN_WAVE_CONSUMED, this.service.getClass().getSimpleName(), this.wave.toString());
this.wave.status(Status.Consumed);
Expand Down Expand Up @@ -194,15 +196,17 @@ private void sendReturnWave(final T res) throws CoreException {

if (responseWaveType != null) {

final WaveItemBase<T> resultWaveItem;

// No service result type defined into a WaveItem
if (responseWaveType.items().isEmpty()) {
if (responseWaveType != JRebirthWaves.RETURN_VOID && responseWaveType.items().isEmpty()) {
LOGGER.log(NO_RETURNED_WAVE_ITEM);
throw new CoreException(NO_RETURNED_WAVE_ITEM);
} else {
// Get the first (and unique) WaveItem used to define the service result type
resultWaveItem = (WaveItemBase<T>) responseWaveType.items().get(0);
}

// Get the first (and unique) WaveItem used to define the service result type
final WaveItemBase<T> resultWaveItem = (WaveItemBase<T>) responseWaveType.items().get(0);

// Try to retrieve the command class, could be null
// final Class<? extends Command> responseCommandClass = this.service.getReturnCommand(this.wave.waveType());

Expand All @@ -212,16 +216,20 @@ private void sendReturnWave(final T res) throws CoreException {
returnWave = Builders.wave()
.waveGroup(WaveGroup.CALL_COMMAND)
.fromClass(this.service.getClass())
.componentClass(responseCommandClass)
.addDatas(Builders.waveData(resultWaveItem, res));
.componentClass(responseCommandClass);
} else {

// Otherwise send a generic wave that can be handled by any component
returnWave = Builders.wave()
.waveType(responseWaveType)
.fromClass(this.service.getClass())
.addDatas(Builders.waveData(resultWaveItem, res));
.fromClass(this.service.getClass());
}

// Add the result wrapped into a WaveData with the right WaveItem
if (resultWaveItem != null) {
returnWave.addDatas(Builders.waveData(resultWaveItem, res));
}
// Don't add data when method has returned VOID

returnWave.relatedWave(this.wave);
returnWave.addWaveListener(new ServiceTaskReturnWaveListener());
Expand Down
Expand Up @@ -43,7 +43,7 @@ public abstract class AbstractBaseModel<M extends Model> extends AbstractBehavio
/** Flag used to determine if a view has been already displayed, useful to manage first time animation. */
private boolean viewDisplayed;

/** . */
/** Force the creation of the View into JAT if set to true. */
protected boolean createViewIntoJAT = false;

/**
Expand Down
Expand Up @@ -65,7 +65,8 @@ public static void checkWaveTypeContract(final Class<? extends Component> waveRe
int methodParameters = 0;
boolean hasCompliantMethod = false;

final List<WaveItem<?>> wParams = waveType.items();
// Remove Wave Item that are not parameters
final List<WaveItem<?>> wParams = waveType.parameters();

for (int j = 0; j < methods.size() && !hasCompliantMethod; j++) {
hasCompliantMethod = checkMethodSignature(methods.get(j), wParams);
Expand Down
Expand Up @@ -17,49 +17,55 @@
*/
package org.jrebirth.af.core.wave;

import org.jrebirth.af.api.wave.contract.WaveItem;

/**
* The Interface JRebirthItems.
*/
public interface JRebirthItems {

/** The Void wave item will be used only into a WaveData to wrap the VOID value when nothing is returned. The Void type cannot be used into a method signature. */
WaveItem<Void> voidItem = new WaveItemBase<Void>(false) {
};

/** The class wave item. */
WaveItemBase<Class<?>> classItem = new WaveItemBase<Class<?>>() {
WaveItem<Class<?>> classItem = new WaveItemBase<Class<?>>() {
};

/** The boolean wave item. */
WaveItemBase<Boolean> booleanItem = new WaveItemBase<Boolean>() {
WaveItem<Boolean> booleanItem = new WaveItemBase<Boolean>() {
};

/** The string wave item. */
WaveItemBase<String> stringItem = new WaveItemBase<String>() {
WaveItem<String> stringItem = new WaveItemBase<String>() {
};

/** The character wave item. */
WaveItemBase<Character> characterItem = new WaveItemBase<Character>() {
WaveItem<Character> characterItem = new WaveItemBase<Character>() {
};

/** The byte wave item. */
WaveItemBase<Byte> byteItem = new WaveItemBase<Byte>() {
WaveItem<Byte> byteItem = new WaveItemBase<Byte>() {
};

/** The short wave item. */
WaveItemBase<Short> shortItem = new WaveItemBase<Short>() {
WaveItem<Short> shortItem = new WaveItemBase<Short>() {
};

/** The integer wave item. */
WaveItemBase<Integer> integerItem = new WaveItemBase<Integer>() {
WaveItem<Integer> integerItem = new WaveItemBase<Integer>() {
};

/** The long wave item. */
WaveItemBase<Long> longItem = new WaveItemBase<Long>() {
WaveItem<Long> longItem = new WaveItemBase<Long>() {
};

/** The float wave item. */
WaveItemBase<Float> floatItem = new WaveItemBase<Float>() {
WaveItem<Float> floatItem = new WaveItemBase<Float>() {
};

/** The double wave item. */
WaveItemBase<Double> doubleItem = new WaveItemBase<Double>() {
WaveItem<Double> doubleItem = new WaveItemBase<Double>() {
};

}
Expand Up @@ -29,6 +29,7 @@

import org.jrebirth.af.api.wave.Wave;
import org.jrebirth.af.api.wave.contract.WaveData;
import org.jrebirth.af.api.wave.contract.WaveItem;
import org.jrebirth.af.api.wave.contract.WaveType;
import org.jrebirth.af.core.service.ServiceTaskBase;

Expand Down Expand Up @@ -69,22 +70,30 @@ public interface JRebirthWaves {
WaveType HIDE_VIEW = waveType("HIDE_VIEW");

/********************************/
/** WaveType related to Service */
/** WaveItem related to Service */
/********************************/

/** This wave item will be used only into a WaveData to pass the current Service task handled by the wave. */
WaveItemBase<ServiceTaskBase<?>> SERVICE_TASK = new WaveItemBase<ServiceTaskBase<?>>(false) {
WaveItem<ServiceTaskBase<?>> SERVICE_TASK = new WaveItemBase<ServiceTaskBase<?>>(false) {
};

/** This wave item will be used only into a WaveData to pass the right progress bar used by service task. */
WaveItemBase<ProgressBar> PROGRESS_BAR = new WaveItemBase<ProgressBar>(false) {
WaveItem<ProgressBar> PROGRESS_BAR = new WaveItemBase<ProgressBar>(false) {
};

/** This wave item will be used only into a WaveData to pass the right string property used to display the task title. */
WaveItemBase<StringProperty> TASK_TITLE = new WaveItemBase<StringProperty>(false) {
WaveItem<StringProperty> TASK_TITLE = new WaveItemBase<StringProperty>(false) {
};

/** This wave item will be used only into a WaveData to pass the right string property used to display the task message. */
WaveItemBase<StringProperty> TASK_MESSAGE = new WaveItemBase<StringProperty>(false) {
WaveItem<StringProperty> TASK_MESSAGE = new WaveItemBase<StringProperty>(false) {
};

/********************************/
/** WaveType related to Service */
/********************************/

/** WaveType used to receive an empty wave when the return action returns void. */
WaveType RETURN_VOID = waveType("RETURN_VOID").items(JRebirthItems.voidItem);

}
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.jrebirth.af.api.command.Command;
import org.jrebirth.af.api.wave.contract.WaveItem;
Expand Down Expand Up @@ -199,11 +200,25 @@ private void buildReturnWaveType() {
}
}

/**
* {@inheritDoc}
*/
@Override
public WaveType returnWaveType() {
return this.returnWaveType;
}

/**
* {@inheritDoc}
*/
@Override
public WaveType returnWaveType(WaveType returnWaveType) {
this.returnWaveType = returnWaveType;
this.returnAction = returnWaveType.action();
this.returnItem = returnWaveType.items().stream().findFirst().get();
return this;
}

/**
* Return the required method parameter list to handle this WaveType.
*
Expand Down Expand Up @@ -291,4 +306,9 @@ public WaveType returnCommandClass(final Class<? extends Command> returnCommandC
return this;
}

@Override
public List<WaveItem<?>> parameters() {
return items().stream().filter(item -> item.isParameter()).collect(Collectors.toList());
}

}

0 comments on commit 66ea721

Please sign in to comment.