Skip to content

Commit

Permalink
CAUSEWAY-3859: Java record refactoring (part 48)
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-huber committed Feb 26, 2025
1 parent 5cfc8c8 commit 8cc310b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ Optional<Item<T>> getItem(final IModel<T> model) {
}

private static int rowIndex(final IModel<?> model) {
return model instanceof DataRowWkt
? ((DataRowWkt)model).getRowIndex()
return model instanceof DataRowWkt dataRowWkt
? dataRowWkt.rowIndex()
: -1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import java.util.ArrayList;
import java.util.stream.Collectors;

import org.apache.wicket.model.ChainingModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import org.apache.causeway.core.metamodel.object.ManagedObject;
import org.apache.causeway.core.metamodel.object.ManagedObjects;
import org.apache.causeway.core.metamodel.objectmanager.memento.ObjectMemento;

import org.jspecify.annotations.NonNull;
import lombok.extern.log4j.Log4j2;

/**
Expand All @@ -37,32 +36,14 @@
* with the parent {@link UiAttributeWkt}, allowing also for pending values.
*/
@Log4j2
public class AttributeModelWithMultiChoice
extends ChainingModel<ArrayList<ObjectMemento>>
implements
AttributeModelWithChoice<ArrayList<ObjectMemento>> {

private static final long serialVersionUID = 1L;

// -- FACTORY

public static AttributeModelWithMultiChoice chain(final @NonNull UiAttributeWkt attributeModel) {
return new AttributeModelWithMultiChoice(attributeModel);
}

// -- CONSTRUCTION

private AttributeModelWithMultiChoice(final UiAttributeWkt attributeModel) {
super(attributeModel); // chaining to attributeModel
}

public record AttributeModelWithMultiChoice(
/**
* chaining idiom: the {@link UiAttributeWkt} we are chained to
*/
@Override
public UiAttributeWkt attributeModel() {
return (UiAttributeWkt) super.getTarget();
}
UiAttributeWkt attributeModel)
implements
IModel<ArrayList<ObjectMemento>>,
AttributeModelWithChoice<ArrayList<ObjectMemento>> {

@Override
public ArrayList<ObjectMemento> getObject() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,25 @@
*/
package org.apache.causeway.viewer.wicket.model.models;

import org.apache.wicket.model.ChainingModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import org.apache.causeway.core.metamodel.objectmanager.memento.ObjectMemento;

import org.jspecify.annotations.NonNull;

/**
* For widgets that use a <tt>org.wicketstuff.select2.Select2Choice</tt>;
* synchronizes the {@link Model} of the <tt>Select2Choice</tt>
* with the parent {@link UiAttributeWkt}.
*/
//@Log4j2
public class AttributeModelWithSingleChoice
extends ChainingModel<ObjectMemento>
implements
AttributeModelWithChoice<ObjectMemento> {

private static final long serialVersionUID = 1L;

// -- FACTORY

public static AttributeModelWithSingleChoice chain(final @NonNull UiAttributeWkt attributeModel) {
return new AttributeModelWithSingleChoice(attributeModel);
}

// -- CONSTRUCTION

private AttributeModelWithSingleChoice(final UiAttributeWkt attributeModel) {
super(attributeModel); // chaining to attributeModel
}

public record AttributeModelWithSingleChoice(
/**
* chaining idiom: the {@link UiAttributeWkt} we are chained to
*/
@Override
public UiAttributeWkt attributeModel() {
return (UiAttributeWkt) super.getTarget();
}
UiAttributeWkt attributeModel)
implements
IModel<ObjectMemento>,
AttributeModelWithChoice<ObjectMemento> {

@Override
public ObjectMemento getObject() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@
*/
package org.apache.causeway.viewer.wicket.model.models;

import org.apache.wicket.model.ChainingModel;

import org.apache.wicket.model.IModel;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

import org.apache.causeway.commons.internal.base._Casts;
import org.apache.causeway.core.metamodel.object.ManagedObject;
import org.apache.causeway.core.metamodel.object.ManagedObjects;

import org.jspecify.annotations.NonNull;

/**
* @param <T> foreign type
* @param <V> scalar value type
*/
public abstract class ScalarConvertingModel<T, V>
extends ChainingModel<T> {
implements IModel<T> {

private static final long serialVersionUID = 1L;

private final UiAttributeWkt attributeModel;

protected ScalarConvertingModel(final @NonNull UiAttributeWkt attributeModel) {
super(attributeModel);
this.attributeModel = attributeModel;
}

@Override
Expand Down Expand Up @@ -68,7 +68,7 @@ public T getObject() {
// -- HELPER

protected UiAttributeWkt attributeModel() {
return (UiAttributeWkt) super.getTarget();
return attributeModel;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
*/
package org.apache.causeway.viewer.wicket.model.models;

import org.apache.wicket.model.ChainingModel;
import org.apache.wicket.model.IModel;
import org.jspecify.annotations.NonNull;

import org.springframework.util.ClassUtils;

Expand All @@ -27,24 +28,19 @@
import org.apache.causeway.core.metamodel.object.ManagedObject;
import org.apache.causeway.core.metamodel.object.MmUnwrapUtils;

import lombok.Getter;
import org.jspecify.annotations.NonNull;

/**
* Wraps and unwraps the contained value within {@link ManagedObject},
* as provided by a {@link UiAttributeWkt}.
*/
public class ScalarUnwrappingModel<T>
extends ChainingModel<T> {

private static final long serialVersionUID = 1L;

@Getter @NonNull private final Class<T> type;
public record ScalarUnwrappingModel<T>(
Class<T> type,
UiAttributeWkt attributeModel) implements IModel<T> {

// canonical constructor
public ScalarUnwrappingModel(
final @NonNull Class<T> type,
final @NonNull UiAttributeWkt attributeModel) {
super(attributeModel);
this.attributeModel = attributeModel;
this.type = type;
_Assert.assertTrue(attributeModel.getElementType().isAssignableFrom(type), ()->
String.format("cannot possibly unwrap model of type %s into target type %s",
Expand Down Expand Up @@ -82,8 +78,4 @@ private T unwrap(final ManagedObject objectAdapter) {
return _Casts.uncheckedCast(pojo);
}

private UiAttributeWkt attributeModel() {
return (UiAttributeWkt) super.getTarget();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,18 @@

import java.util.Optional;

import org.apache.wicket.model.ChainingModel;
import org.apache.wicket.model.IModel;

import org.apache.causeway.core.metamodel.tabular.DataRow;
import org.apache.causeway.core.metamodel.tabular.DataTableInteractive;

import lombok.Getter;

public final class DataRowWkt
extends ChainingModel<DataRow> {

private static final long serialVersionUID = 1L;

public static DataRowWkt chain(
final CollectionModel collectionModel,
final DataRow dataRow) {
return new DataRowWkt(collectionModel, dataRow);
}

@Getter private final int rowIndex;

private DataRowWkt(
final CollectionModel collectionModel,
final DataRow dataRow) {
super(collectionModel);
this.rowIndex = dataRow.rowIndex();
}
public record DataRowWkt(
int rowIndex,
CollectionModel collectionModel)
implements IModel<DataRow> {

@Override
public final DataRow getObject() {
public DataRow getObject() {
return dataRow().orElse(null);
}

Expand All @@ -70,10 +53,6 @@ public boolean isTableDataLoaded() {

// -- HELPER

private CollectionModel collectionModel() {
return (CollectionModel) super.getTarget();
}

private DataTableInteractive dataTable() {
return collectionModel().getObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public DataTableInteractive getDataTableModel() {

@Override
public IModel<DataRow> model(final DataRow dataRow) {
return DataRowWkt.chain(collectionModel, dataRow);
return new DataRowWkt(dataRow.rowIndex(), collectionModel);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.wicket.model.LambdaModel;
import org.apache.wicket.model.Model;
import org.wicketstuff.select2.AbstractSelect2Choice;

import org.apache.causeway.commons.internal.base._Casts;
import org.apache.causeway.core.metamodel.object.ManagedObject;
import org.apache.causeway.core.metamodel.objectmanager.memento.ObjectMemento;
Expand All @@ -42,11 +43,11 @@ static Select2 create(
var choiceProvider = new ChoiceProvider(attributeModel);
var select2 = attributeModel.isSingular()
? new SingleChoice(id,
AttributeModelWithSingleChoice.chain(attributeModel),
new AttributeModelWithSingleChoice(attributeModel),
attributeModel,
choiceProvider)
: new MultiChoice(id,
_Casts.uncheckedCast(AttributeModelWithMultiChoice.chain(attributeModel)),
_Casts.uncheckedCast(new AttributeModelWithMultiChoice(attributeModel)),
attributeModel,
choiceProvider);
var component = select2.component();
Expand Down

0 comments on commit 8cc310b

Please sign in to comment.