
com.databasesandlife.util.wicket.EmptyListIsSpecialWrapperModel Maven / Gradle / Ivy
package com.databasesandlife.util.wicket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.apache.wicket.model.IModel;
/**
* Wraps an underlying model, but sets a pre-defined list in the case the user doesn't enter anything.
*
* If the user enters greater than zero options, they are passed to the wrapped model.
* Otherwise, if the user doesn't enter any options, some "default" list is set in the wrapped model.
* The "default" list may be null.
*
* @author This source is copyright Adrian Smith and licensed under the LGPL 3.
* @see Project on GitHub
*/
@SuppressWarnings("serial")
public class EmptyListIsSpecialWrapperModel implements IModel> {
protected List emptyValues;
protected IModel> dataModel;
/** @param e which values to be saved in the underlying model, in the case the user doesn't enter anything */
public EmptyListIsSpecialWrapperModel(List e, IModel> m) {
emptyValues = e;
dataModel=m;
}
@Override public void detach() { }
@Override public void setObject(List userData) {
dataModel.setObject(userData.isEmpty() ? emptyValues : userData);
}
/** Returns data to user i.e. show the user empty list, if model = empty values */
@Override public List getObject() {
List dataValues = dataModel.getObject();
if (dataValues == null && emptyValues == null) return new ArrayList();
if (dataValues == null || emptyValues == null) return dataValues; // not the same if only one is null
if (new HashSet(dataValues).equals(new HashSet(emptyValues))) return new ArrayList();
return dataValues;
}
}