Skip to content

Commit

Permalink
Let nullable ScoredValue factory methods return Value<V> instead of S…
Browse files Browse the repository at this point in the history
…coredValue<V> #1644

Nullable ScoredValue factory methods now declare Value as return type as an empty value cannot be associated without a score. Either both pieces of information should be present or absent but partial existence doesn't fit the programming model.
  • Loading branch information
mp911de committed Mar 5, 2021
1 parent a085658 commit 5698df7
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 67 deletions.
22 changes: 11 additions & 11 deletions src/main/java/io/lettuce/core/GeoValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ private GeoValue(GeoCoordinates coordinates, V value) {
}

/**
* Creates a {@link GeoValue} from a {@code key} and an {@link Optional}. The resulting value contains the value from the
* Creates a {@link Value} from a {@code key} and an {@link Optional}. The resulting value contains the value from the
* {@link Optional} if a value is present. Value is empty if the {@link Optional} is empty.
*
* @param coordinates the score
* @param coordinates the score.
* @param optional the optional. May be empty but never {@code null}.
* @return the {@link GeoValue}
* @return the {@link Value}.
*/
public static <T extends V, V> Value<V> from(GeoCoordinates coordinates, Optional<T> optional) {

Expand All @@ -67,12 +67,12 @@ public static <T extends V, V> Value<V> from(GeoCoordinates coordinates, Optiona
}

/**
* Creates a {@link GeoValue} from a {@code coordinates} and {@code value}. The resulting value contains the value if the
* Creates a {@link Value} from a {@code coordinates} and {@code value}. The resulting value contains the value if the
* {@code value} is not null.
*
* @param coordinates the coordinates.
* @param value the value. May be {@code null}.
* @return the {@link GeoValue}
* @return the {@link Value}.
*/
public static <T extends V, V> Value<V> fromNullable(GeoCoordinates coordinates, T value) {

Expand Down Expand Up @@ -101,7 +101,7 @@ public static <T extends V, V> GeoValue<V> just(double longitude, double latitud
*
* @param coordinates the coordinates.
* @param value the value. Must not be {@code null}.
* @return the {@link GeoValue}
* @return the {@link GeoValue}.
*/
public static <T extends V, V> GeoValue<V> just(GeoCoordinates coordinates, T value) {

Expand Down Expand Up @@ -170,9 +170,9 @@ public String toString() {
* Returns a {@link GeoValue} consisting of the results of applying the given function to the value of this element. Mapping
* is performed only if a {@link #hasValue() value is present}.
*
* @param <R> The element type of the new stream
* @param mapper a stateless function to apply to each element
* @return the new {@link GeoValue}
* @param <R> element type of the new {@link GeoValue}.
* @param mapper a stateless function to apply to each element.
* @return the new {@link GeoValue}.
*/
@SuppressWarnings("unchecked")
public <R> GeoValue<R> map(Function<? super V, ? extends R> mapper) {
Expand All @@ -190,8 +190,8 @@ public <R> GeoValue<R> map(Function<? super V, ? extends R> mapper) {
* Returns a {@link GeoValue} consisting of the results of applying the given function to the {@link GeoCoordinates} of this
* element. Mapping is performed only if a {@link #hasValue() value is present}.
*
* @param mapper a stateless function to apply to each element
* @return the new {@link GeoValue}
* @param mapper a stateless function to apply to each element.
* @return the new {@link GeoValue}.
*/
public GeoValue<V> mapCoordinates(Function<? super GeoCoordinates, ? extends GeoCoordinates> mapper) {

Expand Down
35 changes: 17 additions & 18 deletions src/main/java/io/lettuce/core/ScoredValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
* @author Will Glozer
* @author Mark Paluch
*/
@SuppressWarnings("serial")
public class ScoredValue<V> extends Value<V> {

private static final ScoredValue<Object> EMPTY = new ScoredValue<>(0, null);
Expand All @@ -48,36 +47,36 @@ private ScoredValue(double score, V value) {
}

/**
* Creates a {@link ScoredValue} from a {@code key} and an {@link Optional}. The resulting value contains the value from the
* Creates a {@link Value} from a {@code key} and an {@link Optional}. The resulting value contains the value from the
* {@link Optional} if a value is present. Value is empty if the {@link Optional} is empty.
*
* @param score the score
* @param score the score.
* @param optional the optional. May be empty but never {@code null}.
* @return the {@link ScoredValue}
* @return the {@link Value}.
*/
public static <T extends V, V> ScoredValue<V> from(double score, Optional<T> optional) {
public static <T extends V, V> Value<V> from(double score, Optional<T> optional) {

LettuceAssert.notNull(optional, "Optional must not be null");

if (optional.isPresent()) {
return new ScoredValue<>(score, optional.get());
}

return fromNullable(score, null);
return empty();
}

/**
* Creates a {@link ScoredValue} from a {@code score} and {@code value}. The resulting value contains the value if the
* Creates a {@link Value} from a {@code score} and {@code value}. The resulting value contains the value if the
* {@code value} is not null.
*
* @param score the score
* @param score the score.
* @param value the value. May be {@code null}.
* @return the {@link ScoredValue}
* @return the {@link Value}.
*/
public static <T extends V, V> ScoredValue<V> fromNullable(double score, T value) {
public static <T extends V, V> Value<V> fromNullable(double score, T value) {

if (value == null) {
return new ScoredValue<>(score, null);
return empty();
}

return new ScoredValue<>(score, value);
Expand All @@ -95,9 +94,9 @@ public static <V> ScoredValue<V> empty() {
/**
* Creates a {@link ScoredValue} from a {@code key} and {@code value}. The resulting value contains the value.
*
* @param score the score
* @param score the score.
* @param value the value. Must not be {@code null}.
* @return the {@link ScoredValue}
* @return the {@link ScoredValue}.
*/
public static <T extends V, V> ScoredValue<V> just(double score, T value) {

Expand Down Expand Up @@ -143,9 +142,9 @@ public String toString() {
* Returns a {@link ScoredValue} consisting of the results of applying the given function to the value of this element.
* Mapping is performed only if a {@link #hasValue() value is present}.
*
* @param <R> The element type of the new stream
* @param mapper a stateless function to apply to each element
* @return the new {@link ScoredValue}
* @param <R> element type of the new {@link ScoredValue}.
* @param mapper a stateless function to apply to each element.
* @return the new {@link ScoredValue}.
*/
@SuppressWarnings("unchecked")
public <R> ScoredValue<R> map(Function<? super V, ? extends R> mapper) {
Expand All @@ -163,8 +162,8 @@ public <R> ScoredValue<R> map(Function<? super V, ? extends R> mapper) {
* Returns a {@link ScoredValue} consisting of the results of applying the given function to the score of this element.
* Mapping is performed only if a {@link #hasValue() value is present}.
*
* @param mapper a stateless function to apply to each element
* @return the new {@link ScoredValue}
* @param mapper a stateless function to apply to each element.
* @return the new {@link ScoredValue} .
*/
@SuppressWarnings("unchecked")
public ScoredValue<V> mapScore(Function<? super Number, ? extends Number> mapper) {
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/io/lettuce/core/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected Value(V value) {
* value is present. Value is empty if the {@link Optional} is empty.
*
* @param optional the optional. May be empty but never {@code null}.
* @return the {@link Value}
* @return the {@link Value}.
*/
public static <T extends V, V> Value<V> from(Optional<T> optional) {

Expand All @@ -79,7 +79,7 @@ public static <T extends V, V> Value<V> from(Optional<T> optional) {
* Creates a {@link Value} from a {@code value}. The resulting value contains the value if the {@code value} is not null.
*
* @param value the value. May be {@code null}.
* @return the {@link Value}
* @return the {@link Value}.
*/
public static <T extends V, V> Value<V> fromNullable(T value) {

Expand All @@ -93,7 +93,7 @@ public static <T extends V, V> Value<V> fromNullable(T value) {
/**
* Returns an empty {@code Value} instance. No value is present for this instance.
*
* @return the {@link Value}
* @return the {@link Value}.
*/
public static <V> Value<V> empty() {
return (Value<V>) EMPTY;
Expand All @@ -103,7 +103,7 @@ public static <V> Value<V> empty() {
* Creates a {@link Value} from a {@code value}. The resulting value contains the value.
*
* @param value the value. Must not be {@code null}.
* @return the {@link Value}
* @return the {@link Value}.
*/
public static <T extends V, V> Value<V> just(T value) {

Expand Down Expand Up @@ -137,8 +137,8 @@ public String toString() {
/**
* If a value is present in this {@code Value}, returns the value, otherwise throws {@code NoSuchElementException}.
*
* @return the non-null value held by this {@code Optional}
* @throws NoSuchElementException if there is no value present
* @return the non-null value held by this {@code Optional}.
* @throws NoSuchElementException if there is no value present.
*
* @see Value#hasValue()
*/
Expand All @@ -154,7 +154,7 @@ public V getValue() {
/**
* Return {@code true} if there is a value present, otherwise {@code false}.
*
* @return {@code true} if there is a value present, otherwise {@code false}
* @return {@code true} if there is a value present, otherwise {@code false}.
*/
public boolean hasValue() {
return value != null;
Expand All @@ -163,7 +163,7 @@ public boolean hasValue() {
/**
* Return {@code true} if there is no value present, otherwise {@code false}.
*
* @return {@code true} if there is no value present, otherwise {@code false}
* @return {@code true} if there is no value present, otherwise {@code false}.
* @since 6.1
*/
public boolean isEmpty() {
Expand All @@ -174,8 +174,8 @@ public boolean isEmpty() {
* Return the value if present, otherwise invoke {@code other} and return the result of that invocation.
*
* @param otherSupplier a {@code Supplier} whose result is returned if no value is present. Must not be {@code null}.
* @return the value if present otherwise the result of {@code other.get()}
* @throws NullPointerException if value is not present and {@code other} is null
* @return the value if present otherwise the result of {@code other.get()}.
* @throws NullPointerException if value is not present and {@code other} is null.
*/
public V getValueOrElseGet(Supplier<V> otherSupplier) {

Expand All @@ -190,8 +190,8 @@ public V getValueOrElseGet(Supplier<V> otherSupplier) {
/**
* Return the value if present, otherwise return {@code other}.
*
* @param other the value to be returned if there is no value present, may be null
* @return the value, if present, otherwise {@code other}
* @param other the value to be returned if there is no value present, may be null.
* @return the value, if present, otherwise {@code other}.
*/
public V getValueOrElse(V other) {

Expand Down Expand Up @@ -225,9 +225,9 @@ public <X extends Throwable> V getValueOrElseThrow(Supplier<? extends X> excepti
* Returns a {@link Value} consisting of the results of applying the given function to the value of this element. Mapping is
* performed only if a {@link #hasValue() value is present}.
*
* @param <R> The element type of the new value
* @param mapper a stateless function to apply to each element
* @return the new {@link Value}
* @param <R> The element type of the new value.
* @param mapper a stateless function to apply to each element.
* @return the new {@link Value}.
*/
@SuppressWarnings("unchecked")
public <R> Value<R> map(Function<? super V, ? extends R> mapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import java.util.Collections;
import java.util.List;

import io.lettuce.core.internal.LettuceStrings;
import io.lettuce.core.ScoredValue;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.internal.LettuceAssert;
import io.lettuce.core.internal.LettuceStrings;

/**
* {@link List} of values and their associated scores.
Expand Down Expand Up @@ -60,7 +60,7 @@ public void set(ByteBuffer bytes) {
@Override
public void set(double number) {

subscriber.onNext(output, ScoredValue.fromNullable(number, value));
subscriber.onNext(output, ScoredValue.just(number, value));
value = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import java.nio.ByteBuffer;

import io.lettuce.core.internal.LettuceStrings;
import io.lettuce.core.ScoredValue;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.internal.LettuceStrings;

/**
* A single {@link ScoredValue}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import java.nio.ByteBuffer;

import io.lettuce.core.internal.LettuceStrings;
import io.lettuce.core.ScoredValue;
import io.lettuce.core.ScoredValueScanCursor;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.internal.LettuceStrings;

/**
* {@link io.lettuce.core.ScoredValueScanCursor} for scan cursor output.
Expand Down Expand Up @@ -51,7 +51,9 @@ protected void setOutput(ByteBuffer bytes) {

@Override
public void set(double number) {
output.getValues().add(ScoredValue.fromNullable(number, value));
if (value != null) {
output.getValues().add(ScoredValue.just(number, value));
}
value = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import java.nio.ByteBuffer;

import io.lettuce.core.internal.LettuceStrings;
import io.lettuce.core.ScoredValue;
import io.lettuce.core.StreamScanCursor;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.internal.LettuceStrings;

/**
* Streaming-Output of of values and their associated scores. Returns the count of all values (including null).
Expand Down Expand Up @@ -53,7 +53,11 @@ protected void setOutput(ByteBuffer bytes) {

@Override
public void set(double number) {
channel.onValue(ScoredValue.fromNullable(number, value));

if (value != null) {
channel.onValue(ScoredValue.just(number, value));
}

value = null;
output.setCount(output.getCount() + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import java.nio.ByteBuffer;

import io.lettuce.core.internal.LettuceStrings;
import io.lettuce.core.ScoredValue;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.internal.LettuceStrings;

/**
* Streaming-Output of of values and their associated scores. Returns the count of all values (including null).
Expand Down Expand Up @@ -54,7 +54,7 @@ public void set(ByteBuffer bytes) {
@Override
public void set(double number) {

channel.onValue(ScoredValue.fromNullable(number, value));
channel.onValue(ScoredValue.just(number, value));
value = null;
output = output.longValue() + 1;
}
Expand Down
Loading

0 comments on commit 5698df7

Please sign in to comment.