Skip to content

Commit

Permalink
added dependency 'org.danekja:jdk-serializable-functional' in version…
Browse files Browse the repository at this point in the history
… 1.9.0
  • Loading branch information
astrapi69 committed Sep 15, 2023
1 parent feceea6 commit bd22f5a
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 170 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Overview

The model-data project provides classes for modeling objects and model binding.
The model-data project is ispired from model pattern of the [wicket project](https://wicket.apache.org/) and most classes have been forked and modified.

Lombok is used for not generating boilerplate source code.
The model-data module library provides classes for modeling objects and model binding. The model-data project is
inspired from the model of the [wicket project](https://wicket.apache.org/) and most classes have been forked and modified. But wicket does not
provide the model as a module library, instead it is embedded in the core module. So the model-data module library is
indented to be used also in other ui projects that need the binding functionality without importing the whole
wicket-core module library.

> Please support this project by simply putting a Github <!-- Place this tag where you want the button to render. -->
<a class="github-button" href="https://github.com/lightblueseas/model-data" data-icon="octicon-star" aria-label="Star lightblueseas/model-data on GitHub">Star ⭐</a>
Expand All @@ -13,6 +14,10 @@ Lombok is used for not generating boilerplate source code.
> If you love this project [![donation](https://img.shields.io/badge/donate-❤-ff2244.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GVBTWLRAZ7HB8)

## Component Model

Starting with version 3.1, the library is also a Java module.

## License

The source code comes under the liberal Apache License V2.0, making model-data great for all types of applications.
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ lombokVersion=1.18.28
objenesisVersion=3.3
commonsLang3Version=3.13.0
jobjCloneVersion=3.4
jdkSerializableFunctionalVersion=1.9.0
##############################
# test dependencies versions #
##############################
Expand Down
1 change: 1 addition & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dependencies {
annotationProcessor("org.projectlombok:lombok:$lombokVersion")
implementation("org.apache.commons:commons-lang3:$commonsLang3Version")
implementation("org.objenesis:objenesis:$objenesisVersion")
implementation("org.danekja:jdk-serializable-functional:$jdkSerializableFunctionalVersion")
testImplementation("org.hamcrest:hamcrest-all:$hamcrestAllVersion")
testImplementation("io.github.astrapi69:test-object:$testObjectVersion")
testImplementation("io.github.astrapi69:jobj-clone:$jobjCloneVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package io.github.astrapi69.model;

import org.danekja.java.util.function.serializable.SerializableBiConsumer;
import org.danekja.java.util.function.serializable.SerializableFunction;

import io.github.astrapi69.model.api.IModel;
import io.github.astrapi69.model.api.SerializableBiConsumer;
import io.github.astrapi69.model.api.SerializableFunction;

/**
* <code>LambdaBindingModel</code> is a basic implementation of an <code>IModel</code> that can bind
Expand Down
115 changes: 73 additions & 42 deletions src/main/java/io/github/astrapi69/model/LambdaModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*/
package io.github.astrapi69.model;

import java.util.Objects;

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;

import io.github.astrapi69.model.api.IModel;
import io.github.astrapi69.model.api.SerializableBiConsumer;
import io.github.astrapi69.model.api.SerializableConsumer;
import io.github.astrapi69.model.api.SerializableFunction;
import io.github.astrapi69.model.api.SerializableSupplier;

/**
* <code>LambdaModel</code> is a basic implementation of an <code>IModel</code> that uses a
Expand All @@ -41,56 +44,78 @@ private LambdaModel()
}

/**
* Create a {@link LambdaModel} for a given target. Usage:
* Create a read-only {@link IModel}. Usage:
*
* <pre>
* {@code
* LambdaModel.of(personModel, Person::getName)
* LambdaModel.of(person::getName)
* }
* </pre>
*
* The target model will be detached automatically.
* Note that {@link IModel} is a {@code FunctionalInterface} and you can also use a lambda
* directly as a model.
*
* @param target
* target for getter and setter
* @param getter
* used to get a value
* @param <X>
* target model object type
* @param <T>
* used to get value
* @return model
*
* @param <R>
* model object type
*/
public static <R> IModel<R> of(SerializableSupplier<R> getter)
{
return getter::get;
}

/**
* Create a {@link LambdaModel}. Usage:
*
* <pre>
* {@code
* LambdaModel.of(person::getName, person::setName)
* }
* </pre>
*
* @param getter
* used to get value
* @param setter
* used to set value
* @return model
*
* @param <T>
* model object type
*/
public static <X, T> IModel<T> of(final IModel<X> target,
final SerializableFunction<X, T> getter)
public static <T> IModel<T> of(final SerializableSupplier<T> getter,
final SerializableConsumer<T> setter)
{

Objects.nonNull(getter);
Objects.nonNull(setter);
return new LambdaModel<T>()
{
private static final long serialVersionUID = 1L;

@Override
public void attach()
{
target.attach();
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void detach()
{
target.detach();
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public T getObject()
{
final X x = target.getObject();
if (x == null)
{
return null;
}
return getter.apply(x);
return getter.get();
}

@Override
public void setObject(final T t)
{
setter.accept(t);
}
};
}
Expand Down Expand Up @@ -123,6 +148,9 @@ public T getObject()
public static <X, T> IModel<T> of(final IModel<X> target,
final SerializableFunction<X, T> getter, final SerializableBiConsumer<X, T> setter)
{
Objects.nonNull(target);
Objects.nonNull(getter);
Objects.nonNull(setter);
return new LambdaModel<T>()
{
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -163,25 +191,29 @@ public void setObject(final T t)
}

/**
* Create a {@link LambdaModel}. Usage:
* Create a {@link LambdaModel} for a given target. Usage:
*
* <pre>
* {@code
* LambdaModel.of(person::getName, person::setName)
* LambdaModel.of(personModel, Person::getName)
* }
* </pre>
*
* @param getter
* used to get value
* @param setter
* used to set value
* @return model
* The target model will be detached automatically.
*
* @param target
* target for getter and setter
* @param getter
* used to get a value
* @param <X>
* target model object type
* @param <T>
* model object type
*
* @return model
*/
public static <T> IModel<T> of(final SerializableSupplier<T> getter,
final SerializableConsumer<T> setter)
public static <X, T> IModel<T> of(final IModel<X> target,
final SerializableFunction<X, T> getter)
{

return new LambdaModel<T>()
Expand All @@ -191,25 +223,24 @@ public static <T> IModel<T> of(final SerializableSupplier<T> getter,
@Override
public void attach()
{
throw new UnsupportedOperationException("Not supported yet.");
target.attach();
}

@Override
public void detach()
{
throw new UnsupportedOperationException("Not supported yet.");
target.detach();
}

@Override
public T getObject()
{
return getter.get();
}

@Override
public void setObject(final T t)
{
setter.accept(t);
final X x = target.getObject();
if (x == null)
{
return null;
}
return getter.apply(x);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

import java.io.Serial;

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;

import io.github.astrapi69.model.api.IModel;
import io.github.astrapi69.model.api.SerializableBiConsumer;
import io.github.astrapi69.model.api.SerializableConsumer;
import io.github.astrapi69.model.api.SerializableFunction;
import io.github.astrapi69.model.api.SerializableSupplier;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
Expand Down
Loading

0 comments on commit bd22f5a

Please sign in to comment.