All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nitorcreations.wicket.model.NullWrapperModel Maven / Gradle / Ivy

There is a newer version: 1.7
Show newest version
package com.nitorcreations.wicket.model;

import java.io.Serializable;

import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.lang.Args;

/**
 * Returns the model object of the wrapped {@link org.apache.wicket.model.IModel}, if it is not {@code null}. Otherwise, returns the auxiliary
 * value.
 *
 * @param 
 */
public class NullWrapperModel extends AbstractReadOnlyModel {
    private static final long serialVersionUID = -2722807116471271339L;

    private final IModel target;

    private final IModel returnIfNull;

    /**
     * Create a model that returns {@code target}'s model object if not null
     * and {@code returnIfNull} otherwise.
     *
     * @param target the value to return if it is not {@code null}
     * @param returnIfNull the value to return if {@code target} is null
     */
    public NullWrapperModel(IModel target, T returnIfNull) {
        this(target, Model.of(Args.notNull(returnIfNull, "returnIfNull")));
    }

    /**
     * Create a model that returns {@code target}'s model object if not null
     * and {@code returnIfNull} otherwise.
     *
     * @param target the value to return if it is not {@code null}
     * @param returnIfNull the value to return if {@code target} is null
     */
    public NullWrapperModel(IModel target, IModel returnIfNull) {
        this.target = Args.notNull(target, "target model");
        this.returnIfNull = Args.notNull(returnIfNull, "returnIfNull");
    }

    @Override
    public T getObject() {
        if (target.getObject() == null) {
            return returnIfNull.getObject();
        }
        return target.getObject();
    }

    @Override
    public void detach() {
        this.target.detach();
        this.returnIfNull.detach();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy