>From Michael Blow <[email protected]>: Michael Blow has submitted this change. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/20287 )
Change subject: [NO ISSUE][*HYR][MISC] += Lazy helper for deferred initialization ...................................................................... [NO ISSUE][*HYR][MISC] += Lazy helper for deferred initialization Ext-ref: MB-68256 Change-Id: I9912c03cf3949dae86eb8af4f159143f660061d4 Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/20287 Reviewed-by: Michael Blow <[email protected]> Reviewed-by: Ali Alsuliman <[email protected]> Integration-Tests: Jenkins <[email protected]> Tested-by: Jenkins <[email protected]> --- A hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/Lazy.java 1 file changed, 58 insertions(+), 0 deletions(-) Approvals: Michael Blow: Looks good to me, but someone else must approve Ali Alsuliman: Looks good to me, approved Jenkins: Verified; Verified diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/Lazy.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/Lazy.java new file mode 100644 index 0000000..28dbf75 --- /dev/null +++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/Lazy.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.hyracks.api.util; + +import java.util.concurrent.FutureTask; +import java.util.function.Supplier; + +/** + * A thread-safe lazy initializer. + * @param <T> + */ +public class Lazy<T> { + private final FutureTask<T> task; + + public Lazy(Supplier<T> supplier) { + this.task = new FutureTask<>(supplier::get); + } + + public T get() { + task.run(); // idempotent + try { + return task.get(); + } catch (Exception e) { + throw new RuntimeException("Lazy initialization failed", e); + } + } +} -- To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/20287 To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings Gerrit-Project: asterixdb Gerrit-Branch: trinity Gerrit-Change-Id: I9912c03cf3949dae86eb8af4f159143f660061d4 Gerrit-Change-Number: 20287 Gerrit-PatchSet: 2 Gerrit-Owner: Michael Blow <[email protected]> Gerrit-Reviewer: Ali Alsuliman <[email protected]> Gerrit-Reviewer: Anon. E. Moose #1000171 Gerrit-Reviewer: Hussain Towaileb <[email protected]> Gerrit-Reviewer: Jenkins <[email protected]> Gerrit-Reviewer: Michael Blow <[email protected]> Gerrit-MessageType: merged
