Skip to content

Commit

Permalink
Remove PropertyValue and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
agentgt committed Feb 8, 2024
1 parent 2eedc98 commit dfcb1cd
Showing 1 changed file with 0 additions and 204 deletions.
204 changes: 0 additions & 204 deletions core/src/main/java/io/jstach/rainbowgum/LogProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -814,56 +814,6 @@ private static void validateKeyParameters(String key, Set<String> parameters) {
}
}

/**
* A lazily retrieved value.
*
* @param <T> value type.
*/
sealed interface Value<T> {

/**
* Maps a property value to a new property value.
* @param <U> new value type.
* @param mapper function.
* @return new property value.
*/
public <U> Value<U> map(PropertyFunction<? super T, ? extends U, ? super Exception> mapper);

/**
* Gets the value.
* @return value or <code>null</code>.
*/
public @Nullable T valueOrNull();

/**
* Gets a value if there is if not uses the fallback.
* @param fallback maybe <code>null</code>.
* @return value.
*/
default @Nullable T valueOrNull(@Nullable T fallback) {
var v = valueOrNull();
if (v != null) {
return v;
}
return fallback;
}

/**
* Gets the value and will fail with {@link NoSuchElementException} if there is no
* value.
* @return value.
* @throws NoSuchElementException if there is no value.
*/
public T value() throws NoSuchElementException;

/**
* Append description of value.
* @param sb appended to.
*/
public void describe(StringBuilder sb);

}

/**
* The result of a property fetched from properties.
*
Expand Down Expand Up @@ -1046,160 +996,6 @@ sealed interface RequiredResult<T> extends Result<T> {

}

/**
* Multiple properties tried in order.
*
* @param <T> property type.
* @param propertyValues property values
*/
record PropertiesValue<T>(List<Value<T>> propertyValues) implements Value<T> {

@Override
public <U> Value<U> map(PropertyFunction<? super T, ? extends U, ? super Exception> mapper) {

List<Value<U>> values = new ArrayList<>();
for (var v : propertyValues) {
values.add(v.map(mapper));
}
return new PropertiesValue<>(values);
}

@Override
public @Nullable T valueOrNull() {
for (var pv : propertyValues) {
var v = pv.valueOrNull();
if (v != null) {
return v;
}
}
return null;
}

@Override
public T value() throws NoSuchElementException {
var t = valueOrNull();
if (t != null) {
return t;
}
StringBuilder error = new StringBuilder();
error.append("Property not found for: ");
describe(error);
throw new NoSuchElementException(error.toString());
}

@Override
public void describe(StringBuilder sb) {
for (var v : propertyValues) {
v.describe(sb);
sb.append(", ");
}
}

}

/**
* A property value is a property and its lazily retrieved value.
*
* @param <T> value type.
* @param property backing property.
* @param properties the log properties instance.
*/
record PropertyValue<T>(Property<T> property, LogProperties properties) implements Value<T> {
/**
* Maps a property value to a new property value.
* @param <U> new value type.
* @param mapper function.
* @return new property value.
*/
public <U> PropertyValue<U> map(PropertyFunction<? super T, ? extends U, ? super Exception> mapper) {
return new PropertyValue<>(property.map(mapper), properties);
}

/**
* Gets the value.
* @return value or <code>null</code>.
*/
public @Nullable T valueOrNull() {
return result().valueOrNull();
}

private Result<T> result() {
return property.get(properties);
}

/**
* Gets a value if there if not uses the fallback.
* @param fallback maybe <code>null</code>.
* @return value.
*/
public @Nullable T valueOrNull(@Nullable T fallback) {
var v = valueOrNull();
if (v != null) {
return v;
}
return fallback;
}

/**
* Convenience that turns a value into an optional.
* @return optional.
*/
public Optional<T> optional() {
return Optional.ofNullable(valueOrNull());
}

/**
* Gets the value and will fail with {@link NoSuchElementException} if there is no
* value.
* @return value.
* @throws NoSuchElementException if there is no value.
*/
public T value() throws NoSuchElementException {
return result().value();
}

/**
* Gets a value if there if not uses the fallback if not null otherwise throws an
* exception.
* @param fallback maybe <code>null</code>.
* @return value.
* @throws NoSuchElementException if no property and fallback is
* <code>null</code>.
*/
public T value(@Nullable T fallback) throws NoSuchElementException {
return result().value(fallback);
}

/**
* Gets a value if there if not uses the fallback if not null otherwise throws an
* exception.
* @param fallback maybe <code>null</code>.
* @return value.
* @throws NoSuchElementException if no property and fallback is
* <code>null</code>.
*/
public T value(@SuppressWarnings("exports") Supplier<? extends @Nullable T> fallback)
throws NoSuchElementException {
return result().value(fallback);
}

/**
* Set a property if its not null.
* @param consumer first parameter is first key and second parameter is non null
* value.
*/
public void set(BiConsumer<String, T> consumer) {
T value = valueOrNull();
if (value != null) {
consumer.accept(property.key(), value);
}
}

public void describe(StringBuilder b) {
b.append(property);
}
}

/**
* A property description.
*
Expand Down

0 comments on commit dfcb1cd

Please sign in to comment.