Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we benefit from a DelayedValue implementation? #396

Open
mvanaken opened this issue Aug 1, 2023 · 1 comment
Open

Can we benefit from a DelayedValue implementation? #396

mvanaken opened this issue Aug 1, 2023 · 1 comment

Comments

@mvanaken
Copy link
Contributor

mvanaken commented Aug 1, 2023

There are several ways to produce values in Metal. The implementations of Value in metal are:

  • CoreValue
  • NotAValue
  • and ParseValue, which is also a CoreValue

The ValueExpression interface uses list semantics, and applies the conversion to every value and creates a CoreValue from it. However, in combination with nth(), only one value is used, so we only need to create this single CoreValue instead.

So the question is: can we delay the creation of CoreValues when we actually need it?

public class DelayedValue<T> implements Value {

    private final T item;
    private final Encoding encoding;
    private final BiFunction<T, Encoding, Value> converter;
    private Value delayedValue;

    public DelayedValue(final T item, final Encoding encoding, final BiFunction<T, Encoding, Value> converter) {
        this.item = item;
        this.encoding = encoding;
        this.converter = converter;
    }

    private void init() {
        if (delayedValue == null) {
            delayedValue = converter.apply(item, encoding);
        }
    }

    @Override
    public Slice slice() {
        init();
        return delayedValue.slice();
    }

    @Override
    public Encoding encoding() {
        return encoding;
    }

    @Override
    public byte[] value() {
        init();
        return delayedValue.value();
    }

    @Override
    public BigInteger length() {
        init();
        return delayedValue.length();
    }

    @Override
    public BigInteger asNumeric() {
        init();
        return delayedValue.asNumeric();
    }

    @Override
    public String asString() {
        init();
        return delayedValue.asString();
    }

    @Override
    public BitSet asBitSet() {
        init();
        return delayedValue.asBitSet();
    }

    @Override
    public String toString() {
        if (delayedValue == null) {
            return "(delayed value)";
        }
        return delayedValue.toString();
    }

    @Override
    public boolean equals(final Object obj) {
        init();
        return Util.notNullAndSameClass(delayedValue, obj)
            && Objects.equals(delayedValue, obj);
    }

    @Override
    public int hashCode() {
        init();
        return Objects.hash(delayedValue);
    }
}
@mvanaken
Copy link
Contributor Author

mvanaken commented Aug 1, 2023

@rdvdijk and @Carly-1, this is the implementation of the DelayedValue as discussed last week. See if you can find an example in your codebase that may benefit from this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant