-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added getStaticSafeValue() as nice simplification
- Loading branch information
Showing
29 changed files
with
1,003 additions
and
988 deletions.
There are no files selected for viewing
103 changes: 59 additions & 44 deletions
103
core/src/main/java/io/github/mmm/value/ReadableValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,59 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.value; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* This interface gives read access to the {@link #get() value} of an object. | ||
* | ||
* @param <V> type of the {@link #get() value}. | ||
* @since 1.0.0 | ||
*/ | ||
public abstract interface ReadableValue<V> extends Supplier<V> { | ||
|
||
/** | ||
* @return the current value of this object. May be {@code null} unless otherwise stated. | ||
*/ | ||
@Override | ||
V get(); | ||
|
||
/** | ||
* @return the same as {@link #get()} but trying to avoid returning {@code null} where possible. So a neutral element | ||
* is returned instead of {@code null} for each type supporting this (e.g. "" for {@link String}, {@code 0} | ||
* for any kind of {@link Number}, {@link Boolean#FALSE}, empty collection, etc.). | ||
*/ | ||
V getSafe(); | ||
|
||
/** | ||
* Null-safe access to {@link #get()}. | ||
* | ||
* @param <T> type of the {@link #get() value} | ||
* @param value the {@link ReadableValue} to unwrap. | ||
* @return the {@link #get() value} of the {@link ReadableValue}. Will be {@code null} if the given | ||
* {@link ReadableValue} is {@code null} or its {@link #get() value} is {@code null}. | ||
*/ | ||
static <T> T get(ReadableValue<T> value) { | ||
|
||
if (value == null) { | ||
return null; | ||
} | ||
return value.get(); | ||
} | ||
|
||
} | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.value; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* This interface gives read access to the {@link #get() value} of an object. | ||
* | ||
* @param <V> type of the {@link #get() value}. | ||
* @since 1.0.0 | ||
*/ | ||
public abstract interface ReadableValue<V> extends Supplier<V> { | ||
|
||
/** | ||
* @return the current value of this object. May be {@code null} unless otherwise stated. | ||
*/ | ||
@Override | ||
V get(); | ||
|
||
/** | ||
* @return the value of {@link #get()} but in case this is {@code null} it will return {@link #getStaticSafeValue()}. | ||
* So unless {@link #getStaticSafeValue()} also returns {@code null} this method a null-safe variant of | ||
* {@link #get()}. | ||
* @see #getStaticSafeValue() | ||
*/ | ||
default V getSafe() { | ||
|
||
V value = get(); | ||
if (value == null) { | ||
value = getStaticSafeValue(); | ||
} | ||
return value; | ||
} | ||
|
||
/** | ||
* @return a neutral element to be used instead of {@code null} if supported for this type of value (e.g. "" for | ||
* {@link String}, {@code 0} for any kind of {@link Number}, {@link Boolean#FALSE}, empty collection, etc.). | ||
* May still be {@code null} for types that have no such neutral element. | ||
*/ | ||
V getStaticSafeValue(); | ||
|
||
/** | ||
* Null-safe access to {@link #get()}. | ||
* | ||
* @param <T> type of the {@link #get() value} | ||
* @param value the {@link ReadableValue} to unwrap. | ||
* @return the {@link #get() value} of the {@link ReadableValue}. Will be {@code null} if the given | ||
* {@link ReadableValue} is {@code null} or its {@link #get() value} is {@code null}. | ||
*/ | ||
static <T> T get(ReadableValue<T> value) { | ||
|
||
if (value == null) { | ||
return null; | ||
} | ||
return value.get(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,82 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.value; | ||
|
||
/** | ||
* Implementation of {@link PropertyPath} for a plain path without {@link #get() value}. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public final class SimplePath implements PropertyPath<Object> { | ||
|
||
/** Name of the {@link #path() path} property for marshalling. */ | ||
public static final String NAME_PATH = "path"; | ||
|
||
private final ReadablePath parent; | ||
|
||
private final String name; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param parent the {@link #parentPath() parent path} or {@code null} for root. | ||
* @param name the {@link #getName() name} and {@link #pathSegment() segment}. | ||
*/ | ||
public SimplePath(ReadablePath parent, String name) { | ||
|
||
super(); | ||
this.parent = parent; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public Object get() { | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public Object getSafe() { | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
|
||
return this.name; | ||
} | ||
|
||
@Override | ||
public ReadablePath parentPath() { | ||
|
||
return this.parent; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
return path(); | ||
} | ||
|
||
/** | ||
* @param path the entire {@link #path()}. | ||
* @return the given {@code path} parsed as {@link SimplePath}. | ||
*/ | ||
public static SimplePath of(String path) { | ||
|
||
int start = 0; | ||
SimplePath result = null; | ||
while (true) { | ||
int i = path.indexOf('.', start); | ||
if (i == -1) { | ||
return new SimplePath(result, path.substring(start)); | ||
} else { | ||
String segment = path.substring(start, i); | ||
result = new SimplePath(result, segment); | ||
start = i + 1; | ||
} | ||
} | ||
} | ||
|
||
} | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.value; | ||
|
||
/** | ||
* Implementation of {@link PropertyPath} for a plain path without {@link #get() value}. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public final class SimplePath implements PropertyPath<Object> { | ||
|
||
/** Name of the {@link #path() path} property for marshalling. */ | ||
public static final String NAME_PATH = "path"; | ||
|
||
private final ReadablePath parent; | ||
|
||
private final String name; | ||
|
||
/** | ||
* The constructor. | ||
* | ||
* @param parent the {@link #parentPath() parent path} or {@code null} for root. | ||
* @param name the {@link #getName() name} and {@link #pathSegment() segment}. | ||
*/ | ||
public SimplePath(ReadablePath parent, String name) { | ||
|
||
super(); | ||
this.parent = parent; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public Object get() { | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public Object getStaticSafeValue() { | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
|
||
return this.name; | ||
} | ||
|
||
@Override | ||
public ReadablePath parentPath() { | ||
|
||
return this.parent; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
return path(); | ||
} | ||
|
||
/** | ||
* @param path the entire {@link #path()}. | ||
* @return the given {@code path} parsed as {@link SimplePath}. | ||
*/ | ||
public static SimplePath of(String path) { | ||
|
||
int start = 0; | ||
SimplePath result = null; | ||
while (true) { | ||
int i = path.indexOf('.', start); | ||
if (i == -1) { | ||
return new SimplePath(result, path.substring(start)); | ||
} else { | ||
String segment = path.substring(start, i); | ||
result = new SimplePath(result, segment); | ||
start = i + 1; | ||
} | ||
} | ||
} | ||
|
||
} |
84 changes: 40 additions & 44 deletions
84
observable/src/main/java/io/github/mmm/value/observable/booleans/ReadableBooleanValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,40 @@ | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.value.observable.booleans; | ||
|
||
import io.github.mmm.value.ReadableTypedValue; | ||
import io.github.mmm.value.observable.object.ReadableSimpleValue; | ||
|
||
/** | ||
* {@link ReadableTypedValue} with {@link Boolean} {@link #get() value}. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public interface ReadableBooleanValue extends ReadableSimpleValue<Boolean> { | ||
|
||
@Override | ||
default Class<Boolean> getValueClass() { | ||
|
||
return Boolean.class; | ||
} | ||
|
||
/** | ||
* @return the current value as primitive {@code boolean}. | ||
* @see #get() | ||
*/ | ||
default boolean getValue() { | ||
|
||
Boolean value = get(); | ||
if (value == null) { | ||
return false; | ||
} | ||
return value.booleanValue(); | ||
} | ||
|
||
@Override | ||
default Boolean getSafe() { | ||
|
||
Boolean value = get(); | ||
if (value == null) { | ||
return Boolean.FALSE; | ||
} | ||
return value; | ||
} | ||
|
||
} | ||
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 | ||
* http://www.apache.org/licenses/LICENSE-2.0 */ | ||
package io.github.mmm.value.observable.booleans; | ||
|
||
import io.github.mmm.value.ReadableTypedValue; | ||
import io.github.mmm.value.observable.object.ReadableSimpleValue; | ||
|
||
/** | ||
* {@link ReadableTypedValue} with {@link Boolean} {@link #get() value}. | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public interface ReadableBooleanValue extends ReadableSimpleValue<Boolean> { | ||
|
||
@Override | ||
default Class<Boolean> getValueClass() { | ||
|
||
return Boolean.class; | ||
} | ||
|
||
/** | ||
* @return the current value as primitive {@code boolean}. | ||
* @see #get() | ||
*/ | ||
default boolean getValue() { | ||
|
||
Boolean value = get(); | ||
if (value == null) { | ||
return false; | ||
} | ||
return value.booleanValue(); | ||
} | ||
|
||
@Override | ||
default Boolean getStaticSafeValue() { | ||
|
||
return Boolean.FALSE; | ||
} | ||
|
||
} |
Oops, something went wrong.