>From Michael Blow <[email protected]>:

Michael Blow has uploaded this change for review. ( 
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
---
A 
hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/util/Lazy.java
1 file changed, 53 insertions(+), 0 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb 
refs/changes/87/20287/1

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: 1
Gerrit-Owner: Michael Blow <[email protected]>
Gerrit-MessageType: newchange

Reply via email to