|
| 1 | +package com.vladsch.flexmark.util.data; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.NotNull; |
| 4 | +import org.jetbrains.annotations.Nullable; |
| 5 | + |
| 6 | +public class DataKey<T> extends DataKeyBase<T> { |
| 7 | + /** |
| 8 | + * Creates a DataKey with non-null data value and factory |
| 9 | + * <p> |
| 10 | + * Use this constructor to ensure that factory is never called with null data holder value |
| 11 | + * |
| 12 | + * @param name See {@link #getName()}. |
| 13 | + * @param defaultValue default to use when data holder is null |
| 14 | + * @param factory data value factory for creating a new default value for the key for a non-null data holder |
| 15 | + */ |
| 16 | + public DataKey(@NotNull String name, @NotNull T defaultValue, @NotNull DataNotNullValueFactory<T> factory) { |
| 17 | + super(name, defaultValue, factory); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates a DataKey with a computed default value dynamically. |
| 22 | + * <p> |
| 23 | + * On construction will invoke factory with null data holder to get the default value |
| 24 | + * |
| 25 | + * @param name See {@link #getName()}. |
| 26 | + * @param factory data value factory for creating a new default value for the key |
| 27 | + */ |
| 28 | + public DataKey(@NotNull String name, @NotNull DataNotNullValueNullableFactory<T> factory) { |
| 29 | + super(name, factory.apply(null), factory); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates a NullableDataKey with a dynamic default value taken from a value of another key |
| 34 | + * <p> |
| 35 | + * does not cache the returned default value but will always delegate to another key until this key |
| 36 | + * gets its own value set. |
| 37 | + * |
| 38 | + * @param name See {@link #getName()}. |
| 39 | + * @param defaultKey The NullableDataKey to take the default value from at time of construction. |
| 40 | + */ |
| 41 | + public DataKey(String name, DataKey<T> defaultKey) { |
| 42 | + this(name, defaultKey.getDefaultValue(), defaultKey::get); |
| 43 | + } |
| 44 | + |
| 45 | + public DataKey(String name, T defaultValue) { |
| 46 | + this(name, defaultValue, options -> defaultValue); |
| 47 | + } |
| 48 | + |
| 49 | + @NotNull |
| 50 | + public DataNotNullValueFactory<T> getFactory() { |
| 51 | + return (DataNotNullValueFactory<T>) super.getFactory(); |
| 52 | + } |
| 53 | + |
| 54 | + @NotNull |
| 55 | + public T getDefaultValue() { |
| 56 | + return super.getDefaultValue(); |
| 57 | + } |
| 58 | + |
| 59 | + @NotNull |
| 60 | + public T getDefaultValue(@NotNull DataHolder holder) { |
| 61 | + return super.getDefaultValue(holder); |
| 62 | + } |
| 63 | + |
| 64 | + @NotNull |
| 65 | + public T get(@Nullable DataHolder holder) { |
| 66 | + return super.get(holder); |
| 67 | + } |
| 68 | + |
| 69 | + @NotNull |
| 70 | + public MutableDataHolder set(@NotNull MutableDataHolder holder, @NotNull T value) { |
| 71 | + return super.set(holder, value); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String toString() { |
| 76 | + // factory applied to null in constructor, no sense doing it again here |
| 77 | + T defaultValue = getDefaultValue(); |
| 78 | + return "DataKey<" + defaultValue.getClass().getName().substring(defaultValue.getClass().getPackage().getName().length() + 1) + "> " + getName(); |
| 79 | + } |
| 80 | +} |
0 commit comments