Skip to content

Commit

Permalink
moved parse to readable, require getValueClass and remove *TypedValue…
Browse files Browse the repository at this point in the history
…, renamed temporal to time
  • Loading branch information
hohwille committed Oct 3, 2024
1 parent e7ab2d9 commit d4bea3d
Show file tree
Hide file tree
Showing 109 changed files with 1,453 additions and 1,293 deletions.
19 changes: 0 additions & 19 deletions core/src/main/java/io/github/mmm/value/ReadableTypedValue.java

This file was deleted.

124 changes: 65 additions & 59 deletions core/src/main/java/io/github/mmm/value/ReadableValue.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,65 @@
/* 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 #getFallbackSafeValue()}.
* So unless {@link #getFallbackSafeValue()} also returns {@code null} this method a null-safe variant of
* {@link #get()}.
* @see #getFallbackSafeValue()
*/
default V getSafe() {

V value = get();
if (value == null) {
value = getFallbackSafeValue();
}
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 getFallbackSafeValue();

/**
* 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 #getFallbackSafeValue()}. So unless {@link #getFallbackSafeValue()} also returns {@code null} this
* method a null-safe variant of {@link #get()}.
* @see #getFallbackSafeValue()
*/
default V getSafe() {

V value = get();
if (value == null) {
value = getFallbackSafeValue();
}
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 getFallbackSafeValue();

/**
* 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();
}

/**
* @return the {@link Class} reflecting the type of {@link #get() value}. Even if the {@link #get() value} is
* {@code null} the property must support returning the value class.
*/
Class<V> getValueClass();

}
170 changes: 88 additions & 82 deletions core/src/main/java/io/github/mmm/value/SimplePath.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,88 @@
/* 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 getFallbackSafeValue() {

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 getFallbackSafeValue() {

return null;
}

@Override
public String getName() {

return this.name;
}

@Override
public ReadablePath parentPath() {

return this.parent;
}

@Override
public Class<Object> getValueClass() {

return Object.class;
}

@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;
}
}
}

}
14 changes: 0 additions & 14 deletions core/src/main/java/io/github/mmm/value/TypedPropertyPath.java

This file was deleted.

Loading

0 comments on commit d4bea3d

Please sign in to comment.