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

org.apache.wicket.model.Model Maven / Gradle / Ivy

Go to download

Pax Wicket Service is an OSGi extension of the Wicket framework, allowing for dynamic loading and unloading of Wicket components and pageSources.

There is a newer version: 5.0.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.wicket.Component;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.model.util.MapModel;
import org.apache.wicket.model.util.WildcardCollectionModel;
import org.apache.wicket.model.util.WildcardListModel;
import org.apache.wicket.model.util.WildcardSetModel;
import org.apache.wicket.util.lang.Objects;


/**
 * Model is the basic implementation of an IModel. It just wraps a simple
 * model object. The model object must be serializable, as it is stored in the session. If you have
 * large objects to store, consider using {@link org.apache.wicket.model.LoadableDetachableModel}
 * instead of this class.
 * 
 * @author Chris Turner
 * @author Eelco Hillenius
 * 
 * @param 
 *            The type of the Model Object
 */
public class Model implements IModel
{
	private static final long serialVersionUID = 1L;

	/** Backing object. */
	private T object;

	/**
	 * Construct the model without providing an object.
	 */
	public Model()
	{
	}

	/**
	 * Construct the model, setting the given object as the wrapped object.
	 * 
	 * @param object
	 *            The model object proper
	 */
	public Model(final T object)
	{
		setObject(object);
	}

	/**
	 * @param 
	 *            type of key inside map
	 * @param 
	 *            type of value inside map
	 * @param map
	 *            The Map, which may or may not be Serializable
	 * @deprecated see {@link Model#of(Map)}
	 * @return A Model object wrapping the Map
	 */
	@Deprecated
	public static  IModel> valueOf(final Map map)
	{
		return of(map);
	}

	/**
	 * @param 
	 *            type of object inside list
	 * @param list
	 *            The List, which may or may not be Serializable
	 * @deprecated see {@link Model#of(List)}
	 * @return A Model object wrapping the List
	 */
	@Deprecated
	public static  IModel> valueOf(final List list)
	{
		return of(list);
	}

	/**
	 * Factory method for models that contain lists. This factory method will automatically rebuild
	 * a nonserializable list into a serializable one.
	 * 
	 * @param 
	 *            model type
	 * @param list
	 *            The List, which may or may not be Serializable
	 * @deprecated see {@link Model#ofList(List)}
	 * @return A Model object wrapping the List
	 */
	@Deprecated
	public static  IModel> of(final List list)
	{
		return new WildcardListModel(list);
	}

	/**
	 * Factory method for models that contain lists. This factory method will automatically rebuild
	 * a nonserializable list into a serializable one.
	 * 
	 * @param 
	 *            model type
	 * @param list
	 *            The List, which may or may not be Serializable
	 * @return A Model object wrapping the List
	 */
	public static  IModel> ofList(final List list)
	{
		return new WildcardListModel(list);
	}

	/**
	 * Factory method for models that contain maps. This factory method will automatically rebuild a
	 * nonserializable map into a serializable one.
	 * 
	 * @param 
	 *            key type in map
	 * @param 
	 *            value type in map
	 * @param map
	 *            The Map, which may or may not be Serializable
	 * 
	 * @deprecated use {@link #ofMap(Map)}
	 * 
	 * @return A Model object wrapping the Map
	 */
	@Deprecated
	public static  IModel> of(final Map map)
	{
		return new MapModel(map);
	}

	/**
	 * Factory method for models that contain maps. This factory method will automatically rebuild a
	 * nonserializable map into a serializable one.
	 * 
	 * @param 
	 *            key type in map
	 * @param 
	 *            value type in map
	 * @param map
	 *            The Map, which may or may not be Serializable
	 * @return A Model object wrapping the Map
	 */
	public static  IModel> ofMap(final Map map)
	{
		return new MapModel(map);
	}

	/**
	 * Factory method for models that contain sets. This factory method will automatically rebuild a
	 * nonserializable set into a serializable one.
	 * 
	 * @param 
	 *            model type
	 * @param set
	 *            The Set, which may or may not be Serializable
	 * @deprecated replace by {@link Model#ofSet(java.util.Set)}.
	 * @return A Model object wrapping the Set
	 */
	@Deprecated
	public static  IModel> of(final Set set)
	{
		return new WildcardSetModel(set);
	}

	/**
	 * Factory method for models that contain sets. This factory method will automatically rebuild a
	 * nonserializable set into a serializable one.
	 * 
	 * @param 
	 *            model type
	 * @param set
	 *            The Set, which may or may not be Serializable
	 * @return A Model object wrapping the Set
	 */
	public static  IModel> ofSet(final Set set)
	{
		return new WildcardSetModel(set);
	}

	/**
	 * Factory method for models that contain collections. This factory method will automatically
	 * rebuild a nonserializable collection into a serializable {@link ArrayList}.
	 * 
	 * @param 
	 *            model type
	 * @param set
	 *            The Collection, which may or may not be Serializable
	 * @return A Model object wrapping the Set
	 */
	public static  IModel> of(final Collection set)
	{
		return new WildcardCollectionModel(set);
	}


	/**
	 * Factory methods for Model which uses type inference to make code shorter. Equivalent to
	 * new Model(object).
	 * 
	 * @param 
	 * @param object
	 * @return Model that contains object
	 */
	public static  Model of(T object)
	{
		return new Model(object);
	}

	/**
	 * Factory methods for Model which uses type inference to make code shorter. Equivalent to
	 * new Model().
	 * 
	 * @param 
	 * @param object
	 * @return Model that contains object
	 */
	public static  Model of()
	{
		return new Model();
	}

	/**
	 * @see org.apache.wicket.model.IModel#getObject()
	 */
	public T getObject()
	{
		return object;
	}

	/**
	 * Set the model object; calls setObject(java.io.Serializable). The model object must be
	 * serializable, as it is stored in the session
	 * 
	 * @param object
	 *            the model object
	 * @see org.apache.wicket.model.IModel#setObject(Object)
	 */
	public void setObject(final T object)
	{
		if (object != null)
		{
			if (!(object instanceof Serializable))
			{
				throw new WicketRuntimeException("Model object must be Serializable");
			}
		}
		this.object = object;
	}

	/**
	 * @see org.apache.wicket.model.IDetachable#detach()
	 */
	public void detach()
	{
		if (object instanceof IDetachable)
		{
			((IDetachable)object).detach();
		}
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer("Model:classname=[");
		sb.append(getClass().getName()).append("]");
		sb.append(":object=[").append(object).append("]");
		return sb.toString();
	}

	// TODO These methods are for helping people upgrade. Remove after
	// deprecation release.
	/**
	 * @param component
	 * @return the model object
	 * @deprecated replace by {@link IModel#getObject()}.
	 */
	@Deprecated
	public final Object getObject(Component component)
	{
		throw new UnsupportedOperationException();
	}

	/**
	 * @param component
	 * @param object
	 * @deprecated replace by {@link IModel#setObject(Object)}.
	 */
	@Deprecated
	public final void setObject(Component component, Object object)
	{
		throw new UnsupportedOperationException();
	}

	@Override
	public int hashCode()
	{
		return Objects.hashCode(object);
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
		{
			return true;
		}
		if (!(obj instanceof Model))
		{
			return false;
		}
		Model that = (Model)obj;
		return Objects.equal(object, that.object);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy