In JSF an component can be rendered or not using the EL empty operator
rendered="#{not empty myBean.myList}"
As I've understood the operator works both as null-check, but also check checks if the list is empty.
I want to do empty checks on some objects of my own custom class, which interface(s) or parts of interfaces do I need to implement? Which interface is the empty operator compatible with?
The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.
Solution 1 - Use empty Operator The empty operator return true if the operand is null, an empty String, empty array, empty Map, or empty List; false, otherwise. You can learn more about the empty operator and if the tag of JSTL in the class Head First Servlet and JSP book.
See ${#strings. isEmpty(variable)} . It will check for empty or null and call trim() .
The main difference between null and empty is that the null is used to refer to nothing while empty is used to refer to a unique string with zero length. A String refers to a sequence of characters.
From EL 2.2 specification (get the one below "Click here to download the spec for evaluation"):
1.10 Empty Operator -
empty AThe
emptyoperator is a prefix operator that can be used to determine if a value is null or empty.To evaluate
empty A
- If
Aisnull, returntrue- Otherwise, if
Ais the empty string, then returntrue- Otherwise, if
Ais an empty array, then returntrue- Otherwise, if
Ais an emptyMap, returntrue- Otherwise, if
Ais an emptyCollection, returntrue- Otherwise return
false
So, considering the interfaces, it works on Collection and Map only. In your case, I think Collection is the best option. Or, if it's a Javabean-like object, then Map. Either way, under the covers, the isEmpty() method is used for the actual check. On interface methods which you can't or don't want to implement, you could throw UnsupportedOperationException.
Using BalusC's suggestion of implementing Collection i can now hide my primefaces p:dataTable using not empty operator on my dataModel that extends javax.faces.model.ListDataModel
Code sample:
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import javax.faces.model.ListDataModel;
import org.primefaces.model.SelectableDataModel;
public class EntityDataModel extends ListDataModel<Entity> implements
Collection<Entity>, SelectableDataModel<Entity>, Serializable {
public EntityDataModel(List<Entity> data) { super(data); }
@Override
public Entity getRowData(String rowKey) {
// In a real app, a more efficient way like a query by rowKey should be
// implemented to deal with huge data
List<Entity> entitys = (List<Entity>) getWrappedData();
for (Entity entity : entitys) {
if (Integer.toString(entity.getId()).equals(rowKey)) return entity;
}
return null;
}
@Override
public Object getRowKey(Entity entity) {
return entity.getId();
}
@Override
public boolean isEmpty() {
List<Entity> entity = (List<Entity>) getWrappedData();
return (entity == null) || entity.isEmpty();
}
// ... other not implemented methods of Collection...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With