Skip to content

Commit

Permalink
JSpecify annotations applied to common modules
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorndarri committed Dec 13, 2024
1 parent 674ee4e commit e3e7827
Show file tree
Hide file tree
Showing 116 changed files with 1,019 additions and 313 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Codion Change Log
==================

## 0.18.23
### is.codion.common
- JSpecify annotations applied to common modules.
## is.codion.common.core
- ValueObserver.getOrThrow() added.
## is.codion.common.db
Expand Down
4 changes: 4 additions & 0 deletions common/core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
dependencies {
api(libs.jspecify)
}

tasks.register<WriteProperties>("writeVersion") {
group = "build"
description = "Writes the current framework version to a file available as a resource"
Expand Down
6 changes: 3 additions & 3 deletions common/core/src/main/java/is/codion/common/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public static PropertyValue<Character> characterValue(String key, char defaultVa
* @return a configuration value builder
*/
public static PropertyValue<String> stringValue(String key) {
return stringValue(key, null);
return STORE.stringValue(key);
}

/**
Expand All @@ -196,7 +196,7 @@ public static PropertyValue<String> stringValue(String key, String defaultValue)
* @return a configuration value builder
*/
public static <T extends Enum<T>> PropertyValue<T> enumValue(String key, Class<T> enumClass) {
return enumValue(key, enumClass, null);
return STORE.enumValue(key, enumClass);
}

/**
Expand Down Expand Up @@ -242,7 +242,7 @@ public static <T> PropertyValue<List<T>> listValue(String key, Function<String,
* @return a configuration value builder
*/
public static <T> PropertyValue<T> value(String key, Function<String, T> parser) {
return value(key, parser, null);
return STORE.value(key, parser, Objects::toString);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions common/core/src/main/java/is/codion/common/Nulls.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package is.codion.common;

import org.jspecify.annotations.Nullable;

import java.util.Arrays;
import java.util.Objects;

Expand All @@ -33,15 +35,15 @@ private Nulls() {}
* @param object the object to check
* @return true if the object is non null
*/
public static boolean nonNull(Object object) {
public static boolean nonNull(@Nullable Object object) {
return Objects.nonNull(object);
}

/**
* @param objects the objects to check
* @return true if none of the given objects are null
*/
public static boolean nonNull(Object... objects) {
public static boolean nonNull(@Nullable Object... objects) {
if (objects == null) {
return false;
}
Expand Down
8 changes: 5 additions & 3 deletions common/core/src/main/java/is/codion/common/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package is.codion.common;

import org.jspecify.annotations.Nullable;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -39,7 +41,7 @@ private Serializer() {}
* @return a byte array representing the serialized object, an empty byte array in case of null
* @throws IOException in case of an exception
*/
public static byte[] serialize(Object object) throws IOException {
public static byte[] serialize(@Nullable Object object) throws IOException {
if (object != null) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
new ObjectOutputStream(byteArrayOutputStream).writeObject(object);
Expand All @@ -54,11 +56,11 @@ public static byte[] serialize(Object object) throws IOException {
* Deserializes the given byte array into a T, null or an empty byte array result in a null return value
* @param bytes a byte array representing the serialized object
* @param <T> the type of the object represented in the byte array
* @return the deserialized object
* @return the deserialized object, null in case of an empty {@code bytes} array
* @throws IOException in case of an exception
* @throws ClassNotFoundException in case the deserialized class is not found
*/
public static <T> T deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
public static <T> @Nullable T deserialize(byte @Nullable[] bytes) throws IOException, ClassNotFoundException {
if (bytes != null && bytes.length > 0) {
return (T) new ObjectInputStream(new ByteArrayInputStream(bytes)).readObject();
}
Expand Down
12 changes: 7 additions & 5 deletions common/core/src/main/java/is/codion/common/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import is.codion.common.property.PropertyValue;

import org.jspecify.annotations.Nullable;

import java.io.Serial;
import java.io.Serializable;
import java.text.Collator;
Expand Down Expand Up @@ -73,7 +75,7 @@ public static <T> List<T> collate(List<T> values) {
* @see #DEFAULT_COLLATOR_LANGUAGE
*/
public static <T> Comparator<T> collator() {
return collator(new Locale(DEFAULT_COLLATOR_LANGUAGE.get()));
return collator(new Locale(DEFAULT_COLLATOR_LANGUAGE.getOrThrow()));
}

/**
Expand Down Expand Up @@ -116,7 +118,7 @@ public static String leftPad(String string, int length, char padChar) {
* @param commaSeparatedValues a String with comma separated values
* @return the trimmed values
*/
public static List<String> parseCommaSeparatedValues(String commaSeparatedValues) {
public static List<String> parseCommaSeparatedValues(@Nullable String commaSeparatedValues) {
if (nullOrEmpty(commaSeparatedValues)) {
return Collections.emptyList();
}
Expand Down Expand Up @@ -150,7 +152,7 @@ private static String padString(String string, int length, char padChar, boolean
* @param string the string to check
* @return true if the given string is null or empty, false otherwise
*/
public static boolean nullOrEmpty(String string) {
public static boolean nullOrEmpty(@Nullable String string) {
return string == null || string.isEmpty();
}

Expand All @@ -159,7 +161,7 @@ public static boolean nullOrEmpty(String string) {
* @param strings the strings to check
* @return true if one of the given strings is null or empty or if no arguments are provided, false otherwise
*/
public static boolean nullOrEmpty(String... strings) {
public static boolean nullOrEmpty(@Nullable String... strings) {
if (strings == null || strings.length == 0) {
return true;
}
Expand All @@ -182,7 +184,7 @@ private static final class SpaceAwareComparator<T> implements Comparator<T>, Ser

private final Locale locale;

private transient Collator collator;
private transient @Nullable Collator collator;

private SpaceAwareComparator(Locale locale) {
this.locale = locale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@

import is.codion.common.observer.Observer;

import org.jspecify.annotations.Nullable;

import java.util.function.Consumer;

final class DefaultEvent<T> implements Event<T> {

private final Object lock = new Object();

private DefaultObserver<T> observer;
private @Nullable DefaultObserver<T> observer;

@Override
public void run() {
accept(null);
}

@Override
public void accept(T data) {
public void accept(@Nullable T data) {
if (observer != null) {
observer.notifyListeners(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import is.codion.common.observer.Observer;

import org.jspecify.annotations.Nullable;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.LinkedHashSet;
Expand All @@ -34,10 +36,10 @@ final class DefaultObserver<T> implements Observer<T> {

private final Object lock = new Object();

private Set<Runnable> listeners;
private Set<Consumer<? super T>> consumers;
private List<WeakReference<Runnable>> weakListeners;
private List<WeakReference<Consumer<? super T>>> weakConsumers;
private @Nullable Set<Runnable> listeners;
private @Nullable Set<Consumer<? super T>> consumers;
private @Nullable List<WeakReference<Runnable>> weakListeners;
private @Nullable List<WeakReference<Consumer<? super T>>> weakConsumers;

@Override
public boolean addConsumer(Consumer<? super T> consumer) {
Expand Down Expand Up @@ -121,7 +123,7 @@ public boolean removeWeakConsumer(Consumer<? super T> consumer) {
}
}

void notifyListeners(T data) {
void notifyListeners(@Nullable T data) {
for (Runnable listener : listeners()) {
listener.run();
}
Expand Down
4 changes: 3 additions & 1 deletion common/core/src/main/java/is/codion/common/event/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import is.codion.common.observer.Observer;

import org.jspecify.annotations.Nullable;

import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -79,7 +81,7 @@ public interface Event<T> extends Runnable, Consumer<T>, Observer<T> {
* @param data data associated with the event
*/
@Override
void accept(T data);
void accept(@Nullable T data);

/**
* @return an observer notified each time this event occurs
Expand Down
23 changes: 23 additions & 0 deletions common/core/src/main/java/is/codion/common/event/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* This file is part of Codion.
*
* Codion is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Codion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Codion. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (c) 2023 - 2024, Björn Darri Sigurðsson.
*/
/**
* <p>Event related classes.
*/
@org.jspecify.annotations.NullMarked
package is.codion.common.event;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package is.codion.common.format;

import org.jspecify.annotations.Nullable;

import java.io.Serial;
import java.io.Serializable;
import java.time.chrono.IsoChronology;
Expand Down Expand Up @@ -49,7 +51,7 @@ final class DefaultLocaleDateTimePattern implements LocaleDateTimePattern, Seria

private final String delimiter;
private final boolean fourDigitYear;
private final String timePattern;
private final @Nullable String timePattern;

private DefaultLocaleDateTimePattern(DefaultBuilder builder) {
this.delimiter = requireNonNull(builder.delimiter, "delimiter must be specified");
Expand Down Expand Up @@ -92,7 +94,7 @@ private static String datePattern(Locale locale, String delimiter, boolean fourD
}

private static String dateTimePattern(Locale locale, String delimiter, boolean fourDigitYear,
String timePattern) {
@Nullable String timePattern) {
requireNonNull(locale);
String datePattern = DateTimeFormatterBuilder.
getLocalizedDateTimePattern(FormatStyle.SHORT, null, IsoChronology.INSTANCE, locale).toLowerCase(locale);
Expand Down Expand Up @@ -142,7 +144,7 @@ static final class DefaultBuilder implements Builder {

private String delimiter = ".";
private boolean fourDigitYear = true;
private String timePattern;
private @Nullable String timePattern;

@Override
public Builder delimiter(String delimiter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* This file is part of Codion.
*
* Codion is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Codion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Codion. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (c) 2023 - 2024, Björn Darri Sigurðsson.
*/
/**
* <p>Formatting related classes.
*/
@org.jspecify.annotations.NullMarked
package is.codion.common.format;
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package is.codion.common.item;

import org.jspecify.annotations.Nullable;

import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
Expand All @@ -27,18 +29,18 @@ abstract class AbstractItem<T> implements Item<T>, Serializable {
@Serial
private static final long serialVersionUID = 1;

private final T value;
private final @Nullable T value;

/**
* Creates a new Item.
* @param value the value, may be null
*/
AbstractItem(T value) {
AbstractItem(@Nullable T value) {
this.value = value;
}

@Override
public final T value() {
public final @Nullable T value() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package is.codion.common.item;

import org.jspecify.annotations.Nullable;

import java.io.Serial;
import java.io.Serializable;

Expand All @@ -32,7 +34,7 @@ final class DefaultItem<T> extends AbstractItem<T> implements Serializable {

private final String caption;

DefaultItem(T value, String caption) {
DefaultItem(@Nullable T value, String caption) {
super(value);
this.caption = requireNonNull(caption, "Item caption may not be null");
}
Expand Down
Loading

0 comments on commit e3e7827

Please sign in to comment.