Permalink
Browse files

Add scoped attribute getters with supplier

  • Loading branch information...
BalusC committed Sep 10, 2017
1 parent 702fac4 commit 7d1bf0ded334e6d448f37d6d10b618225573efeb
Showing with 142 additions and 0 deletions.
  1. +71 −0 src/main/java/org/omnifaces/util/Faces.java
  2. +71 −0 src/main/java/org/omnifaces/util/FacesLocal.java
@@ -28,6 +28,7 @@
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.function.Supplier;
import javax.el.ELContext;
import javax.el.ELResolver;
@@ -2024,6 +2025,20 @@ public static String getRealPath(String webContentPath) {
return FacesLocal.getRequestAttribute(getContext(), name);
}
/**
* Returns the request scope attribute value associated with the given name, or computes the supplied value if absent.
* @param <T> The expected return type.
* @param name The request scope attribute name.
* @param computeIfAbsent The computed request scope attribute value when absent. Useful if it represents a collection, map or bean.
* @return The request scope attribute value associated with the given name.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see ExternalContext#getRequestMap()
* @since 3.0
*/
public static <T> T getRequestAttribute(String name, Supplier<T> computeIfAbsent) {
return FacesLocal.getRequestAttribute(getContext(), name, computeIfAbsent);
}
/**
* Sets the request scope attribute value associated with the given name.
* @param name The request scope attribute name.
@@ -2072,6 +2087,20 @@ public static Flash getFlash() {
return FacesLocal.getFlashAttribute(getContext(), name);
}
/**
* Returns the flash scope attribute value associated with the given name, or computes the supplied value if absent.
* @param <T> The expected return type.
* @param name The flash scope attribute name.
* @param computeIfAbsent The computed flash scope attribute value when absent. Useful if it represents a collection, map or bean.
* @return The flash scope attribute value associated with the given name.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see ExternalContext#getFlash()
* @since 3.0
*/
public static <T> T getFlashAttribute(String name, Supplier<T> computeIfAbsent) {
return FacesLocal.getFlashAttribute(getContext(), name, computeIfAbsent);
}
/**
* Sets the flash scope attribute value associated with the given name.
* @param name The flash scope attribute name.
@@ -2119,6 +2148,20 @@ public static void setFlashAttribute(String name, Object value) {
return FacesLocal.getViewAttribute(getContext(), name);
}
/**
* Returns the view scope attribute value associated with the given name, or computes the supplied value if absent.
* @param <T> The expected return type.
* @param name The view scope attribute name.
* @param computeIfAbsent The computed view scope attribute value when absent. Useful if it represents a collection, map or bean.
* @return The view scope attribute value associated with the given name.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see ExternalContext#getViewMap()
* @since 3.0
*/
public static <T> T getViewAttribute(String name, Supplier<T> computeIfAbsent) {
return FacesLocal.getViewAttribute(getContext(), name, computeIfAbsent);
}
/**
* Sets the view scope attribute value associated with the given name.
* @param name The view scope attribute name.
@@ -2166,6 +2209,20 @@ public static void setViewAttribute(String name, Object value) {
return FacesLocal.getSessionAttribute(getContext(), name);
}
/**
* Returns the session scope attribute value associated with the given name, or computes the supplied value if absent.
* @param <T> The expected return type.
* @param name The session scope attribute name.
* @param computeIfAbsent The computed session scope attribute value when absent. Useful if it represents a collection, map or bean.
* @return The session scope attribute value associated with the given name.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see ExternalContext#getSessionMap()
* @since 3.0
*/
public static <T> T getSessionAttribute(String name, Supplier<T> computeIfAbsent) {
return FacesLocal.getSessionAttribute(getContext(), name, computeIfAbsent);
}
/**
* Sets the session scope attribute value associated with the given name.
* @param name The session scope attribute name.
@@ -2213,6 +2270,20 @@ public static void setSessionAttribute(String name, Object value) {
return FacesLocal.getApplicationAttribute(getContext(), name);
}
/**
* Returns the application scope attribute value associated with the given name, or computes the supplied value if absent.
* @param <T> The expected return type.
* @param name The application scope attribute name.
* @param computeIfAbsent The computed application scope attribute value when absent. Useful if it represents a collection, map or bean.
* @return The application scope attribute value associated with the given name.
* @throws ClassCastException When <code>T</code> is of wrong type.
* @see ExternalContext#getApplicationMap()
* @since 3.0
*/
public static <T> T getApplicationAttribute(String name, Supplier<T> computeIfAbsent) {
return FacesLocal.getApplicationAttribute(getContext(), name, computeIfAbsent);
}
/**
* Sets the application scope attribute value associated with the given name.
* @param name The application scope attribute name.
@@ -51,6 +51,7 @@
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.function.Supplier;
import java.util.logging.Logger;
import javax.el.ELContext;
@@ -1388,6 +1389,20 @@ public static String getRealPath(FacesContext context, String webContentPath) {
return (T) getRequestMap(context).get(name);
}
/**
* @see Faces#getRequestAttribute(String, Supplier)
*/
public static <T> T getRequestAttribute(FacesContext context, String name, Supplier<T> computeIfAbsent) {
T value = getRequestAttribute(context, name);
if (value == null) {
value = computeIfAbsent.get();
setRequestAttribute(context, name, value);
}
return value;
}
/**
* @see Faces#setRequestAttribute(String, Object)
*/
@@ -1420,6 +1435,20 @@ public static Flash getFlash(FacesContext context) {
return (T) getFlash(context).get(name);
}
/**
* @see Faces#getFlashAttribute(String, Supplier)
*/
public static <T> T getFlashAttribute(FacesContext context, String name, Supplier<T> computeIfAbsent) {
T value = getFlashAttribute(context, name);
if (value == null) {
value = computeIfAbsent.get();
setFlashAttribute(context, name, value);
}
return value;
}
/**
* @see Faces#setFlashAttribute(String, Object)
*/
@@ -1452,6 +1481,20 @@ public static void setFlashAttribute(FacesContext context, String name, Object v
return (T) getViewMap(context).get(name);
}
/**
* @see Faces#getViewAttribute(String, Supplier)
*/
public static <T> T getViewAttribute(FacesContext context, String name, Supplier<T> computeIfAbsent) {
T value = getViewAttribute(context, name);
if (value == null) {
value = computeIfAbsent.get();
setViewAttribute(context, name, value);
}
return value;
}
/**
* @see Faces#setViewAttribute(String, Object)
*/
@@ -1484,6 +1527,20 @@ public static void setViewAttribute(FacesContext context, String name, Object va
return (T) getSessionMap(context).get(name);
}
/**
* @see Faces#getSessionAttribute(String, Supplier)
*/
public static <T> T getSessionAttribute(FacesContext context, String name, Supplier<T> computeIfAbsent) {
T value = getSessionAttribute(context, name);
if (value == null) {
value = computeIfAbsent.get();
setSessionAttribute(context, name, value);
}
return value;
}
/**
* @see Faces#setSessionAttribute(String, Object)
*/
@@ -1516,6 +1573,20 @@ public static void setSessionAttribute(FacesContext context, String name, Object
return (T) getApplicationMap(context).get(name);
}
/**
* @see Faces#getApplicationAttribute(String, Supplier)
*/
public static <T> T getApplicationAttribute(FacesContext context, String name, Supplier<T> computeIfAbsent) {
T value = getApplicationAttribute(context, name);
if (value == null) {
value = computeIfAbsent.get();
setApplicationAttribute(context, name, value);
}
return value;
}
/**
* @see Faces#setApplicationAttribute(String, Object)
*/

0 comments on commit 7d1bf0d

Please sign in to comment.