Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,48 @@ public PRESENTATION convertToPresentation(T value,
};
}

/**
* Wraps the given {@code converter} with a converter that handles both null
* presentation and null model values:
* <ul>
* <li>If the model value is null, the converter returns null. Otherwise, it
* delegates to the wrapped converter.</li>
* <li>If the presentation value is null, the converter returns null.
* Otherwise, it delegates to the wrapped converter.</li>
* </ul>
* Example use:
*
* <pre>
* {@code
* Converter<String, LocalDate> converter = Converter
* .nullSafeConverter(Converter.from(LocalDate::parse,
* LocalDate::toString, Exception::getMessage));
* }
* </pre>
*
* @param <P>
* the presentation type
* @param <M>
* the model type
* @param converter
* the converter to delegate to when the value being converted is
* not null
* @return a converter that handles both null presentation and null model
* values
*/
static <P, M> Converter<P, M> nullSafeConverter(Converter<P, M> converter) {
return new Converter<P, M>() {
@Override
public Result<M> convertToModel(P value, ValueContext context) {
return value == null ? Result.ok(null)
: converter.convertToModel(value, context);
}

@Override
public P convertToPresentation(M value, ValueContext context) {
return value == null ? null
: converter.convertToPresentation(value, context);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2000-2026 Vaadin Ltd.
*
* Licensed 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 com.vaadin.flow.data.converter;

import org.junit.jupiter.api.Test;

import com.vaadin.flow.data.binder.ValueContext;

import static java.util.Objects.requireNonNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

class NullSafeConverterTest {

record ValueObject(String value) {
ValueObject {
requireNonNull(value);
}
}

private final Converter<String, ValueObject> converter = Converter
.nullSafeConverter(Converter.from(ValueObject::new,
ValueObject::value, Exception::getMessage));

@Test
void nullPresentationConvertsToNullModel() {
var result = converter.convertToModel(null, new ValueContext());
assertFalse(result.isError());
assertNull(result.getOrThrow(RuntimeException::new));
}

@Test
void nonNullPresentationConvertsToNonNullModel() {
var result = converter.convertToModel("hello", new ValueContext());
assertFalse(result.isError());
assertEquals(new ValueObject("hello"),
result.getOrThrow(RuntimeException::new));
}

@Test
void nullModelConvertsToNullPresentation() {
var result = converter.convertToPresentation(null, new ValueContext());
assertNull(result);
}

@Test
void nonNullModelConvertsToNonNullPresentation() {
var result = converter.convertToPresentation(new ValueObject("hello"),
new ValueContext());
assertEquals("hello", result);
}
}
Loading