com.databasesandlife.util.wicket.EmptyListIsSpecialWrapperModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
package com.databasesandlife.util.wicket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import org.apache.wicket.model.IModel;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
* Wraps an underlying model, and exposes the empty list if the underlying model has a particular value.
* That particular value of the underlying model 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 @CheckForNull List specialValueInUnderlyingModel;
protected @Nonnull IModel> underlyingModel;
/** @param specialValue which values to be saved in the underlying model, in the case the user doesn't enter anything */
public EmptyListIsSpecialWrapperModel(@CheckForNull List specialValue, @Nonnull IModel> model) {
specialValueInUnderlyingModel = specialValue;
underlyingModel = model;
}
@Override public void detach() { }
/**
* Takes data from Wicket and applies it to the underlying model.
* Checks to see if the data from Wicket is empty and, if so, sets the underlying model with the special value.
*/
@Override public void setObject(List userData) {
underlyingModel.setObject(userData.isEmpty() ? specialValueInUnderlyingModel : userData);
}
/**
* Takes data from the underlying model, and passes it to Wicket.
* If the underlying model contains the special value, then pass the empty list to Wicket.
* Otherwise, simply pass the underlying model on to Wicket.
* The exception being that if the underlying model is null, we return the empty list, that makes more sense for Wicket.
*/
@Override public @Nonnull List getObject() {
var underlyingValue = underlyingModel.getObject();
var underlyingSet = underlyingValue == null ? null : new HashSet<>(underlyingValue);
var specialValueSet = specialValueInUnderlyingModel == null ? null : new HashSet<>(specialValueInUnderlyingModel);
if (Objects.equals(underlyingSet, specialValueSet)) return new ArrayList<>();
else if (underlyingValue == null) return new ArrayList<>();
else return underlyingValue;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy