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

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

Go to download

A module that creates a .jar from the classes in wicket, wicket-util and wicket-request modules in order to create a valid OSGi bundle of the wicket framework.

There is a newer version: 10.1.1
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 org.apache.wicket.util.lang.Args;
import org.danekja.java.util.function.serializable.SerializableBiConsumer;
import org.danekja.java.util.function.serializable.SerializableConsumer;
import org.danekja.java.util.function.serializable.SerializableFunction;
import org.danekja.java.util.function.serializable.SerializableSupplier;

/**
 * LambdaModel is a basic implementation of an IModel that uses a
 * serializable {@link java.util.function.Supplier} to get the object and
 * {@link java.util.function.Consumer} to set it.
 *
 * @param 
 *            The type of the Model Object
 */
public abstract class LambdaModel implements IModel
{
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor hidden, instantiation is done using one of the factory methods
	 */
	private LambdaModel()
	{
	}

	@Override
	public void setObject(T t)
	{
		throw new UnsupportedOperationException("setObject(Object) not supported");
	}

	/**
	 * Create a read-only {@link IModel}. Usage:
	 * 
	 * 
	 * {@code
	 *     LambdaModel.of(person::getName)
	 * }
	 * 
* * Note that {@link IModel} is a {@code FunctionalInterface} and you can also use a lambda * directly as a model. * * @param getter * used to get value * @return model * * @param * model object type */ public static IModel of(SerializableSupplier getter) { return getter::get; } /** * Create a {@link LambdaModel}. Usage: * *
	 * {@code
	 * 	LambdaModel.of(person::getName, person::setName)
	 * }
	 * 
* * @param getter * used to get value * @param setter * used to set value * @return model * * @param * model object type */ public static IModel of(SerializableSupplier getter, SerializableConsumer setter) { Args.notNull(getter, "getter"); Args.notNull(setter, "setter"); return new LambdaModel() { private static final long serialVersionUID = 1L; @Override public R getObject() { return getter.get(); } @Override public void setObject(R r) { setter.accept(r); } }; } /** * Create a {@link LambdaModel} for a given target. Usage: * *
	 * {@code
	 * 	LambdaModel.of(personModel, Person::getName)
	 * }
	 * 
* * The target model will be detached automatically. * * @param target * target for getter and setter * @param getter * used to get a value * @param * target model object type * @param * model object type * * @return model */ public static IModel of(IModel target, SerializableFunction getter) { Args.notNull(target, "target"); Args.notNull(getter, "getter"); return new LambdaModel() { private static final long serialVersionUID = 1L; @Override public R getObject() { X x = target.getObject(); if (x == null) { return null; } return getter.apply(x); } @Override public void detach() { target.detach(); } }; } /** * Create a {@link LambdaModel} for a given target. Usage: * *
	 * {@code
	 * 	LambdaModel.of(personModel, Person::getName, Person::setName)
	 * }
	 * 
* * The target model will be detached automatically. * * @param target * target for getter and setter * @param getter * used to get a value * @param setter * used to set a value * * @param * target model object type * @param * model object type * * @return model * @see IModel#flatMap(SerializableFunction) */ public static IModel of(IModel target, SerializableFunction getter, SerializableBiConsumer setter) { Args.notNull(target, "target"); Args.notNull(getter, "getter"); Args.notNull(setter, "setter"); return new LambdaModel() { private static final long serialVersionUID = 1L; @Override public R getObject() { X x = target.getObject(); if (x == null) { return null; } return getter.apply(x); } @Override public void setObject(R r) { X x = target.getObject(); if (x != null) { setter.accept(x, r); } } @Override public void detach() { target.detach(); } }; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy