Skip to content

Commit

Permalink
cleanup nullability annotations; small nullability related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Nov 3, 2024
1 parent 9200412 commit 434bd32
Show file tree
Hide file tree
Showing 75 changed files with 330 additions and 205 deletions.
8 changes: 4 additions & 4 deletions utility-db/src/main/java/com/dua3/utility/db/DbUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public final class DbUtil {
try {
// load properties
Map<Object, Object> p;
try (InputStream in = DbUtil.class.getResourceAsStream("jdbc_drivers.properties")) {
p = LangUtil.loadProperties(in);
String resource = "jdbc_drivers.properties";
try (InputStream in = DbUtil.class.getResourceAsStream(resource)) {
p = LangUtil.loadProperties(Objects.requireNonNull(in, "resource nott found: " + resource));
}

// parse entries
Expand Down Expand Up @@ -287,8 +288,7 @@ public static DataSource createDataSource(Driver driver, String url, String user
throws SQLException {
LangUtil.check(driver.acceptsURL(url), () -> new SQLException("URL not accepted by driver"));

JdbcDataSource ds = new JdbcDataSource();
ds.setDriver(driver);
JdbcDataSource ds = new JdbcDataSource(driver);
ds.setUrl(url);
ds.setUser(user);
ds.setPassword(password);
Expand Down
17 changes: 5 additions & 12 deletions utility-db/src/main/java/com/dua3/utility/db/JdbcDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public class JdbcDataSource implements DataSource {
private @Nullable String url;
private @Nullable PrintWriter logWriter;
private int loginTimeout;
private @Nullable Driver driver;
private Driver driver;

/**
* Constructor.
*
* @param driver the driver
*/
public JdbcDataSource() {
// nop
public JdbcDataSource(Driver driver) {
this.driver = driver;
}

private void log(String message) {
Expand All @@ -43,15 +45,6 @@ private void log(String message) {
}
}

/**
* Set the JDBC driver for this instance.
*
* @param driver the driver
*/
public void setDriver(Driver driver) {
this.driver = driver;
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException("getParentLogger() is not supported");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public NamedParameterStatement(Connection connection, String query) throws SQLEx
statement = connection.prepareStatement(parsedQuery);
}

private static JDBCType getParameterType(ParameterMetaData meta, int index) {
private static @Nullable JDBCType getParameterType(ParameterMetaData meta, int index) {
try {
return JDBCType.valueOf(meta.getParameterType(index));
} catch (SQLException e) {
Expand Down Expand Up @@ -281,7 +281,7 @@ private <T> void setNonNullWithLongArg(String name, T value, long arg, SetParame
}
}

private <T, U> void setWithObjectArg(SQLType type, String name, @Nullable T value, @Nullable U arg, SetParameterObject<T, U> setter) throws SQLException {
private <T, U> void setWithObjectArg(SQLType type, String name, @Nullable T value, @Nullable U arg, SetParameterObject<T, @Nullable U> setter) throws SQLException {
if (value == null) {
setNull(name, type);
} else {
Expand Down Expand Up @@ -1152,7 +1152,7 @@ private interface SetParameterObject<T, U> {
public static class ParameterInfo {
final String name;
final List<Integer> indexes = new ArrayList<>();
JDBCType type;
@Nullable JDBCType type;

ParameterInfo(String name) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class AbstractDialogBuilder<D extends @NonNull Dialog<R>, B exte
extends AbstractDialogPaneBuilder<D, B, R> {

private final BiConsumer<D, String> titleSetter;
private @Nullable final Window parentWindow;
private final @Nullable Window parentWindow;
private @Nullable String title;

protected AbstractDialogBuilder(@Nullable Window parentWindow) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AlertBuilder
private ButtonType @Nullable[] buttons;
private @Nullable ButtonType defaultButton;

AlertBuilder(AlertType type, Window parentWindow) {
AlertBuilder(AlertType type, @Nullable Window parentWindow) {
super(parentWindow);
setDialogSupplier(() -> new Alert(type));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import org.jspecify.annotations.Nullable;

import java.util.function.Supplier;

Expand All @@ -17,7 +18,7 @@
*
* @param <T> the input result type
*/
public class ChoiceInputControl<T> implements InputControl<T> {
public class ChoiceInputControl<T extends @Nullable Object> implements InputControl<T> {

private final ComboBox<ChoiceOption.Choice<T>> control;
private final ChoiceOption<T> option;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dua3.utility.fx.controls;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import org.jspecify.annotations.Nullable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.Property;
Expand Down Expand Up @@ -36,7 +38,7 @@
*
* @param <T> the type of the items contained in the ComboBox
*/
public class ComboBoxEx<T> extends CustomControl<HBox> implements InputControl<T> {
public class ComboBoxEx<T extends @Nullable Object> extends CustomControl<HBox> implements InputControl<T> {
private static final Logger LOG = LogManager.getLogger(ComboBoxEx.class);

private @Nullable Comparator<? super T> comparator = null;
Expand Down Expand Up @@ -143,6 +145,11 @@ protected void updateItem(@Nullable T item, boolean empty) {
}

private void editItem() {
if (edit == null) {
LOG.warn("editing not supported");
return;
}

int idx = comboBox.getSelectionModel().getSelectedIndex();
if (idx >= 0) {
T item = items.get(idx);
Expand All @@ -157,7 +164,7 @@ private void editItem() {
}

private void addItem() {
Optional.ofNullable(add.get()).ifPresent(item -> {
Optional.ofNullable(add).map(Supplier::get).ifPresent(item -> {
items.add(item);
comboBox.getSelectionModel().select(item);
sortItems();
Expand Down Expand Up @@ -280,11 +287,11 @@ public void reset() {

@Override
public ReadOnlyBooleanProperty validProperty() {
return null; // FIXME
return new SimpleBooleanProperty(true);
}

@Override
public ReadOnlyStringProperty errorProperty() {
return null; // FIXME
return new SimpleStringProperty("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class FileChooserBuilder {
private Path initialDir = USER_HOME;
private String initialFileName = "";
private List<ExtensionFilter> filters = new ArrayList<>();
private ExtensionFilter selectedFilter = null;
private @Nullable ExtensionFilter selectedFilter = null;

FileChooserBuilder() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* These properties are updated based on the path selected by the user and the
* specified validation function.</p>
*/
public class FileInput extends CustomControl<HBox> implements InputControl<Path> {
public class FileInput extends CustomControl<HBox> implements InputControl<@Nullable Path> {

private static final StringConverter<Path> PATH_CONVERTER = new PathConverter();

Expand All @@ -60,11 +60,11 @@ public Path fromString(@Nullable String s) {
}
}

private final ObjectProperty<Path> value = new SimpleObjectProperty<>();
private final ObjectProperty<@Nullable Path> value = new SimpleObjectProperty<>();

private final FileDialogMode mode;
private final FileChooser.ExtensionFilter[] filters;
private final Supplier<Path> dflt;
private final Supplier<@Nullable Path> dflt;

private final StringProperty error = new SimpleStringProperty("");
private final BooleanProperty valid = new SimpleBooleanProperty(true);
Expand All @@ -83,7 +83,7 @@ public FileInput(
boolean existingOnly,
Supplier<Path> dflt,
Collection<FileChooser.ExtensionFilter> filters,
Function<Path, Optional<String>> validate) {
Function<@Nullable Path, Optional<String>> validate) {
super(new HBox());

getStyleClass().setAll("file-input");
Expand Down Expand Up @@ -208,7 +208,7 @@ public static Function<Path, Optional<String>> defaultValidate(FileDialogMode mo
};
}

private Path getPath() {
private @Nullable Path getPath() {
return value.get();
}

Expand All @@ -223,7 +223,7 @@ public void reset() {
}

@Override
public Property<Path> valueProperty() {
public Property<@Nullable Path> valueProperty() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ default <T> B comboBoxEx(
@Nullable Supplier<T> add,
@Nullable BiPredicate<ComboBoxEx<T>, T> remove,
Function<T, String> format,
Supplier<T> dflt,
Supplier<@Nullable T> dflt,
Class<T> cls,
Collection<T> items
) {
Expand Down Expand Up @@ -333,7 +333,7 @@ <T> B comboBoxEx(
* @param items the items to choose from
* @return {@code this}
*/
default <T> B radioList(
default <T extends @Nullable Object> B radioList(
String id,
String label,
Supplier<T> dflt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -41,7 +42,7 @@ public class InputGrid extends GridPane {
private static final String MARKER_OK = "";

protected final BooleanProperty valid = new SimpleBooleanProperty(false);
private Collection<Meta<?>> data = null;
private Collection<Meta<?>> data = Collections.emptyList();
private int columns = 1;

/**
Expand Down Expand Up @@ -166,12 +167,12 @@ public void reset() {
*
* @param <T> the input's value type
*/
static final class Meta<T> {
static final class Meta<T extends @Nullable Object> {
final String id;
final Class<T> cls;
final Supplier<? extends T> dflt;
final InputControl<? super T> control;
final Label label;
final @Nullable Label label;
final Label marker = new Label();

Meta(String id, @Nullable String label, Class<T> cls, Supplier<? extends T> dflt, InputControl<? super T> control) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static class ControlWrapper implements InputControl<Void> {

private final Node node;

private final Property<Void> value = new SimpleObjectProperty<>(null);
private final Property<Void> value = new SimpleObjectProperty<>();
private final BooleanProperty valid = new SimpleBooleanProperty(true);
private final ReadOnlyStringProperty error = new SimpleStringProperty("");

Expand Down Expand Up @@ -120,15 +120,15 @@ private <T> InputGridBuilder doAdd(String id, @Nullable String label, Class<T> t

@Override
public InputGridBuilder addNode(String id, @Nullable String label, Node node) {
Meta<Void> meta = new Meta<>(id, label, Void.class, null, new ControlWrapper(node));
Meta<Void> meta = new Meta<>(id, label, Void.class, () -> null, new ControlWrapper(node));
Meta<?> prev = data.put(id, meta);
LangUtil.check(prev == null, "Input with id '" + id + "' already defined");
return this;
}

@Override
public InputGridBuilder addNode(String id, Node node) {
Meta<Void> meta = new Meta<>(id, null, Void.class, null, new ControlWrapper(node));
Meta<Void> meta = new Meta<>(id, null, Void.class, () -> null, new ControlWrapper(node));
Meta<?> prev = data.put(id, meta);
LangUtil.check(prev == null, "Input with id '" + id + "' already defined");
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public class OptionsPane extends GridPane implements InputControl<Arguments> {
*/
protected static final Logger LOG = LogManager.getLogger(OptionsPane.class);
private static final Insets INSETS = new Insets(2);
private final InputControl.State<Arguments> state;
private final InputControl.State<@Nullable Arguments> state;
private final Supplier<? extends Collection<Option<?>>> options;
private final Supplier<Arguments> dflt;
private final Supplier<@Nullable Arguments> dflt;
private final Map<Option<?>, InputControl<?>> items = new LinkedHashMap<>();

/**
Expand All @@ -71,7 +71,7 @@ public OptionsPane(Collection<Option<?>> optionSet, Arguments currentValues) {
public OptionsPane(Supplier<? extends Collection<Option<?>>> options, Supplier<Arguments> dflt) {
this.options = options;
this.dflt = dflt;
Property<Arguments> value = new SimpleObjectProperty<>();
Property<@Nullable Arguments> value = new SimpleObjectProperty<>();
this.state = new State<>(value, dflt);
}

Expand Down Expand Up @@ -130,7 +130,7 @@ public void init() {
}

@SuppressWarnings("unchecked")
private <T> InputControl<T> createControl(Arguments values, Option<T> option) {
private <T extends @Nullable Object> InputControl<T> createControl(Arguments values, Option<T> option) {
if (option instanceof ChoiceOption<T> co) {
return new ChoiceInputControl<>(co, supplyDefault(co, values));
} else if (option instanceof Flag f) {
Expand Down Expand Up @@ -189,7 +189,7 @@ public void reset() {
}

@Override
public Property<Arguments> valueProperty() {
public Property<@Nullable Arguments> valueProperty() {
return state.valueProperty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @param <T> The type of items that will be represented as radio buttons.
*/
public class RadioPane<T> extends VBox implements InputControl<T> {
public class RadioPane<T extends @Nullable Object> extends VBox implements InputControl<T> {

protected static final Logger LOG = LogManager.getLogger(RadioPane.class);
private static final double SPACING = 4;
Expand Down Expand Up @@ -58,7 +58,7 @@ public RadioPane(Collection<T> items, @Nullable T currentValue, Function<T, Opti
}

// update state when selected toggle changes
Property<T> property = new SimpleObjectProperty<>();
Property<@Nullable T> property = new SimpleObjectProperty<>();
group.selectedToggleProperty().addListener((v, o, n) -> {
Toggle toggle = group.getSelectedToggle();
property.setValue(toggle != null ? (T) toggle.getUserData() : null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.scene.control.Control;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

import java.util.Optional;
import java.util.function.Function;
Expand All @@ -17,7 +18,7 @@
* @param <C> the type of the control, which must extend from Control
* @param <R> the type of the value held by the input control
*/
public class SimpleInputControl<C extends @NonNull Control, R> implements InputControl<R> {
public class SimpleInputControl<C extends Control, R extends @Nullable Object> implements InputControl<R> {

private final C control;
private final State<R> state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.beans.value.ObservableNumberValue;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import org.jspecify.annotations.Nullable;

import java.util.function.BiFunction;
import java.util.function.DoubleConsumer;
Expand Down Expand Up @@ -168,7 +169,7 @@ public SliderBuilder bind(ObservableNumberValue value) {
* @param value the property to be bound bidirectionally with the slider's value property.
* @return this instance of {@code SliderBuilder} for method chaining.
*/
public SliderBuilder bindBidirectional(Property<Number> value) {
public SliderBuilder bindBidirectional(Property<@Nullable Number> value) {
slider.valueProperty().bindBidirectional(value);
return this;
}
Expand Down
Loading

0 comments on commit 434bd32

Please sign in to comment.