Skip to content

Commit

Permalink
Add scoped attribute getters with supplier
Browse files Browse the repository at this point in the history
  • Loading branch information
BalusC committed Sep 10, 2017
1 parent 702fac4 commit 7d1bf0d
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/main/java/org/omnifaces/util/Faces.java
Expand Up @@ -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;
Expand Down Expand Up @@ -2024,6 +2025,20 @@ public static <T> T getRequestAttribute(String name) {
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.
Expand Down Expand Up @@ -2072,6 +2087,20 @@ public static <T> T getFlashAttribute(String name) {
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.
Expand Down Expand Up @@ -2119,6 +2148,20 @@ public static <T> T getViewAttribute(String name) {
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.
Expand Down Expand Up @@ -2166,6 +2209,20 @@ public static <T> T getSessionAttribute(String name) {
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.
Expand Down Expand Up @@ -2213,6 +2270,20 @@ public static <T> T getApplicationAttribute(String name) {
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.
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/org/omnifaces/util/FacesLocal.java
Expand Up @@ -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;
Expand Down Expand Up @@ -1388,6 +1389,20 @@ public static <T> T getRequestAttribute(FacesContext context, String name) {
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)
*/
Expand Down Expand Up @@ -1420,6 +1435,20 @@ public static <T> T getFlashAttribute(FacesContext context, String name) {
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)
*/
Expand Down Expand Up @@ -1452,6 +1481,20 @@ public static <T> T getViewAttribute(FacesContext context, String name) {
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)
*/
Expand Down Expand Up @@ -1484,6 +1527,20 @@ public static <T> T getSessionAttribute(FacesContext context, String name) {
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)
*/
Expand Down Expand Up @@ -1516,6 +1573,20 @@ public static <T> T getApplicationAttribute(FacesContext context, String name) {
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)
*/
Expand Down

0 comments on commit 7d1bf0d

Please sign in to comment.