-
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.
also use getStaticSafeValue() for enums
- Loading branch information
Showing
1 changed file
with
50 additions
and
46 deletions.
There are no files selected for viewing
96 changes: 50 additions & 46 deletions
96
observable/src/main/java/io/github/mmm/value/observable/enumeration/ReadableEnumValue.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,46 +1,50 @@ | ||
/* 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.enumeration; | ||
|
||
import java.util.Objects; | ||
|
||
import io.github.mmm.value.ReadableTypedValue; | ||
import io.github.mmm.value.observable.object.ReadableSimpleValue; | ||
|
||
/** | ||
* {@link ReadableTypedValue} with {@link Enum} {@link #get() value}. | ||
* | ||
* @param <E> type of {@link Enum} {@link #getValue() value}. | ||
* @since 1.0.0 | ||
*/ | ||
public interface ReadableEnumValue<E extends Enum<E>> extends ReadableSimpleValue<E> { | ||
|
||
@Override | ||
default String getAsString() { | ||
|
||
E e = get(); | ||
if (e == null) { | ||
return null; | ||
} | ||
return e.toString(); | ||
} | ||
|
||
/** | ||
* Same as {@link #get()}. | ||
* | ||
* @return the current {@link #get() value}. | ||
*/ | ||
default E getValue() { | ||
|
||
return get(); | ||
} | ||
|
||
@Override | ||
default E getSafe() { | ||
|
||
E e = get(); | ||
Objects.requireNonNull(e); | ||
return e; | ||
} | ||
|
||
} | ||
/* 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.enumeration; | ||
|
||
import io.github.mmm.value.ReadableTypedValue; | ||
import io.github.mmm.value.observable.object.ReadableSimpleValue; | ||
|
||
/** | ||
* {@link ReadableTypedValue} with {@link Enum} {@link #get() value}. | ||
* | ||
* @param <E> type of {@link Enum} {@link #getValue() value}. | ||
* @since 1.0.0 | ||
*/ | ||
public interface ReadableEnumValue<E extends Enum<E>> extends ReadableSimpleValue<E> { | ||
|
||
@Override | ||
default String getAsString() { | ||
|
||
E e = get(); | ||
if (e == null) { | ||
return null; | ||
} | ||
return e.toString(); | ||
} | ||
|
||
/** | ||
* Same as {@link #get()}. | ||
* | ||
* @return the current {@link #get() value}. | ||
*/ | ||
default E getValue() { | ||
|
||
return get(); | ||
} | ||
|
||
@Override | ||
default E getStaticSafeValue() { | ||
|
||
Class<E> valueClass = getValueClass(); | ||
if (valueClass == null) { | ||
return null; | ||
} | ||
E[] constants = valueClass.getEnumConstants(); | ||
if (constants.length == 0) { | ||
return null; | ||
} | ||
return constants[0]; | ||
} | ||
|
||
} |