Permalink
Browse files

java8 update: turn Callback.XXX into functional interfaces

  • Loading branch information...
BalusC committed Apr 17, 2017
1 parent feae34a commit 4d3d1233e936d15df9986dee23e53de91fafacee
@@ -33,8 +33,6 @@
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
import org.omnifaces.util.Callback;
/**
* <p>
* This JSF listener for {@link UIViewRoot} ensures that the necessary JavaScript code to open or close the
@@ -76,7 +74,7 @@ public void processEvent(SystemEvent event) {
final FacesContext context = FacesContext.getCurrentInstance();
final Map<String, Boolean> sockets = getSockets(context);
forEachComponent(context).ofTypes(Socket.class).invoke(new Callback.WithArgument<Socket>() { @Override public void invoke(Socket socket) {
forEachComponent(context).ofTypes(Socket.class).<Socket>invoke(socket -> {
if (!sockets.containsKey(socket.getChannel())) {
return;
}
@@ -94,7 +92,7 @@ public void processEvent(SystemEvent event) {
addScriptToBody(script);
}
}
}});
});
}
// Helpers --------------------------------------------------------------------------------------------------------
@@ -47,7 +47,6 @@
import org.omnifaces.io.ResettableBufferedWriter;
import org.omnifaces.servlet.BufferedHttpServletResponse;
import org.omnifaces.servlet.HttpServletResponseOutputWrapper;
import org.omnifaces.util.Callback;
import org.omnifaces.util.State;
import org.omnifaces.util.cache.CacheEntry;
import org.omnifaces.util.cache.CacheFactory;
@@ -185,14 +184,7 @@ public Cache() {
// Execute the following code in PreRenderView, since at construction time the "useBuffer" and "key" attributes
// have not been set, and there is no @PostContruct for UIComponents.
subscribeToViewEvent(PRE_RENDER, new Callback.SerializableVoid() {
private static final long serialVersionUID = 1L;
@Override
public void invoke() {
processPreRenderViewEvent(context);
}
});
subscribeToViewEvent(PRE_RENDER, () -> processPreRenderViewEvent(context));
}
private void processPreRenderViewEvent(final FacesContext context) {
@@ -209,12 +201,7 @@ private void processPreRenderViewEvent(final FacesContext context) {
// After the RENDER_RESPONSE phase, copy the area we need to cache from the response buffer
// and insert it into our cache
subscribeToRequestAfterPhase(RENDER_RESPONSE, new Callback.Void() {
@Override
public void invoke() {
processPostRenderResponsePhase(context, bufferedResponse);
}
});
subscribeToRequestAfterPhase(RENDER_RESPONSE, () -> processPostRenderResponsePhase(context, bufferedResponse));
}
}
@@ -22,10 +22,8 @@
import javax.faces.application.ResourceDependency;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponent;
import javax.faces.component.UIForm;
import javax.faces.component.UIInput;
import javax.faces.component.visit.VisitCallback;
import javax.faces.component.visit.VisitContext;
import javax.faces.component.visit.VisitHint;
import javax.faces.component.visit.VisitResult;
@@ -124,21 +122,17 @@ public void encodeChildren(FacesContext context) throws IOException {
}
final StringBuilder clientIds = new StringBuilder();
form.visitTree(VisitContext.createVisitContext(context, null, VISIT_HINTS), new VisitCallback() {
@Override
public VisitResult visit(VisitContext context, UIComponent component) {
if (component instanceof UIInput && !((UIInput) component).isValid()) {
if (clientIds.length() > 0) {
clientIds.append(',');
}
String clientId = component.getClientId(context.getFacesContext());
clientIds.append('"').append(clientId).append('"');
form.visitTree(VisitContext.createVisitContext(context, null, VISIT_HINTS), (visitContext, component) -> {
if (component instanceof UIInput && !((UIInput) component).isValid()) {
if (clientIds.length() > 0) {
clientIds.append(',');
}
return VisitResult.ACCEPT;
String clientId = component.getClientId(visitContext.getFacesContext());
clientIds.append('"').append(clientId).append('"');
}
return VisitResult.ACCEPT;
});
if (clientIds.length() > 0) {
@@ -211,13 +211,7 @@ protected void process(final FacesContext context, final PhaseId phaseId) {
return;
}
process(context, getModel(phaseId), new Callback.Returning<Void>() {
@Override
public Void invoke() {
processTreeNode(context, phaseId);
return null;
}
});
process(context, getModel(phaseId), () -> processTreeNode(context, phaseId));
}
/**
@@ -238,21 +232,18 @@ public boolean visitTree(final VisitContext context, final VisitCallback callbac
return false;
}
return process(context.getFacesContext(), model, new Callback.Returning<Boolean>() {
@Override
public Boolean invoke() {
VisitResult result = context.invokeVisitCallback(Tree.this, callback);
return process(context.getFacesContext(), model, () -> {
VisitResult result = context.invokeVisitCallback(Tree.this, callback);
if (result == VisitResult.COMPLETE) {
return true;
}
if (result == VisitResult.ACCEPT && !context.getSubtreeIdsToVisit(Tree.this).isEmpty()) {
return visitTreeNode(context, callback);
}
if (result == VisitResult.COMPLETE) {
return true;
}
return false;
if (result == VisitResult.ACCEPT && !context.getSubtreeIdsToVisit(Tree.this).isEmpty()) {
return visitTreeNode(context, callback);
}
return false;
});
}
@@ -268,21 +259,18 @@ public void broadcast(FacesEvent event) {
TreeFacesEvent treeEvent = (TreeFacesEvent) event;
final FacesEvent wrapped = treeEvent.getWrapped();
process(context, treeEvent.getNode(), new Callback.Returning<Void>() {
@Override
public Void invoke() {
UIComponent source = wrapped.getComponent();
pushComponentToEL(context, getCompositeComponentParent(source));
process(context, treeEvent.getNode(), () -> {
UIComponent source = wrapped.getComponent();
pushComponentToEL(context, getCompositeComponentParent(source));
try {
source.broadcast(wrapped);
}
finally {
popComponentFromEL(context);
}
return null;
try {
source.broadcast(wrapped);
}
finally {
popComponentFromEL(context);
}
return null;
});
}
else {
@@ -300,16 +288,13 @@ public Void invoke() {
* @see TreeModel#getLevel()
* @see TreeInsertChildren
*/
protected void processTreeNode(final FacesContext context, final PhaseId phaseId) {
processTreeNode(phaseId, new Callback.ReturningWithArgument<Void, TreeNode>() {
@Override
public Void invoke(TreeNode treeNode) {
if (treeNode != null) {
treeNode.process(context, phaseId);
}
return null;
protected <R> R processTreeNode(final FacesContext context, final PhaseId phaseId) {
return processTreeNode(phaseId, (treeNode) -> {
if (treeNode != null) {
treeNode.process(context, phaseId);
}
return (R) null;
});
}
@@ -325,15 +310,12 @@ public Void invoke(TreeNode treeNode) {
* @see TreeInsertChildren
*/
protected boolean visitTreeNode(final VisitContext context, final VisitCallback callback) {
return processTreeNode(PhaseId.ANY_PHASE, new Callback.ReturningWithArgument<Boolean, TreeNode>() {
@Override
public Boolean invoke(TreeNode treeNode) {
if (treeNode != null) {
return treeNode.visitTree(context, callback);
}
return false;
return processTreeNode(PhaseId.ANY_PHASE, (treeNode) -> {
if (treeNode != null) {
return treeNode.visitTree(context, callback);
}
return false;
});
}
@@ -87,21 +87,18 @@ protected void process(final FacesContext context, final PhaseId phaseId) {
return;
}
process(context, new Callback.ReturningWithArgument<Void, Tree>() {
@Override
public Void invoke(Tree tree) {
if (tree.getCurrentModelNode() != null) {
for (TreeModel childModelNode : (Iterable<TreeModel>) tree.getCurrentModelNode()) {
tree.setCurrentModelNode(context, childModelNode);
if (isRendered()) {
processSuper(context, phaseId);
}
process(context, (tree) -> {
if (tree.getCurrentModelNode() != null) {
for (TreeModel childModelNode : (Iterable<TreeModel>) tree.getCurrentModelNode()) {
tree.setCurrentModelNode(context, childModelNode);
if (isRendered()) {
processSuper(context, phaseId);
}
}
return null;
}
return null;
});
}
@@ -123,21 +120,18 @@ public boolean visitTree(final VisitContext context, final VisitCallback callbac
return false;
}
return process(context.getFacesContext(), new Callback.ReturningWithArgument<Boolean, Tree>() {
@Override
public Boolean invoke(Tree tree) {
if (tree.getCurrentModelNode() != null) {
for (TreeModel childModelNode : (Iterable<TreeModel>) tree.getCurrentModelNode()) {
tree.setCurrentModelNode(context.getFacesContext(), childModelNode);
return process(context.getFacesContext(), (tree) -> {
if (tree.getCurrentModelNode() != null) {
for (TreeModel childModelNode : (Iterable<TreeModel>) tree.getCurrentModelNode()) {
tree.setCurrentModelNode(context.getFacesContext(), childModelNode);
if (TreeNodeItem.super.visitTree(context, callback)) {
return true;
}
if (TreeNodeItem.super.visitTree(context, callback)) {
return true;
}
}
return false;
}
return false;
});
}
@@ -68,38 +68,26 @@
// Private constants ----------------------------------------------------------------------------------------------
private enum Type {
LT(new Callback.ReturningWithArgument<Boolean, List<Comparable>>() {
@Override
public Boolean invoke(List<Comparable> values) {
return new ArrayList<>(new TreeSet<>(values)).equals(values);
}
LT((values) -> {
return new ArrayList<>(new TreeSet<>(values)).equals(values);
}),
LTE(new Callback.ReturningWithArgument<Boolean, List<Comparable>>() {
@Override
public Boolean invoke(List<Comparable> values) {
List<Comparable> sortedValues = new ArrayList<>(values);
Collections.sort(sortedValues);
return sortedValues.equals(values);
}
LTE((values) -> {
List<Comparable> sortedValues = new ArrayList<>(values);
Collections.sort(sortedValues);
return sortedValues.equals(values);
}),
GT(new Callback.ReturningWithArgument<Boolean, List<Comparable>>() {
@Override
public Boolean invoke(List<Comparable> values) {
List<Comparable> sortedValues = new ArrayList<>(new TreeSet<>(values));
Collections.reverse(sortedValues);
return sortedValues.equals(values);
}
GT((values) -> {
List<Comparable> sortedValues = new ArrayList<>(new TreeSet<>(values));
Collections.reverse(sortedValues);
return sortedValues.equals(values);
}),
GTE(new Callback.ReturningWithArgument<Boolean, List<Comparable>>() {
@Override
public Boolean invoke(List<Comparable> values) {
List<Comparable> sortedValues = new ArrayList<>(values);
Collections.sort(sortedValues, Collections.reverseOrder());
return sortedValues.equals(values);
}
GTE((values) -> {
List<Comparable> sortedValues = new ArrayList<>(values);
Collections.sort(sortedValues, Collections.reverseOrder());
return sortedValues.equals(values);
});
private Callback.ReturningWithArgument<Boolean, List<Comparable>> callback;
@@ -40,7 +40,6 @@
import javax.faces.render.Renderer;
import org.omnifaces.component.messages.OmniMessages;
import org.omnifaces.util.Callback;
/**
* This renderer is the default renderer of {@link OmniMessages}. It's basically copypasted from Mojarra 2.2,
@@ -134,11 +133,8 @@ else if (messages.isEmpty()) {
messages.addAll(context.getMessageList(forComponent.getClientId(context)));
if (!(forComponent instanceof UIInput)) {
forEachComponent(context).fromRoot(forComponent).ofTypes(UIInput.class).invoke(new Callback.WithArgument<UIInput>() {
@Override
public void invoke(UIInput input) {
messages.addAll(context.getMessageList(input.getClientId(context)));
}
forEachComponent(context).fromRoot(forComponent).ofTypes(UIInput.class).invoke((input) -> {
messages.addAll(context.getMessageList(input.getClientId(context)));
});
}
}
@@ -29,7 +29,6 @@
import javax.faces.view.facelets.TagHandler;
import org.omnifaces.component.input.Form;
import org.omnifaces.util.Callback;
/**
* <p>
@@ -99,12 +98,7 @@ public void apply(FaceletContext context, final UIComponent parent) throws IOExc
// We can't use hasInvokedSubmit() before the component is added to view, because the client ID isn't available.
// Hence, we subscribe this check to after phase of restore view.
subscribeToRequestAfterPhase(RESTORE_VIEW, new Callback.Void() {
@Override
public void invoke() {
processIgnoreValidationFailed((UICommand) parent);
}
});
subscribeToRequestAfterPhase(RESTORE_VIEW, () -> processIgnoreValidationFailed((UICommand) parent));
}
/**
Oops, something went wrong.

0 comments on commit 4d3d123

Please sign in to comment.