/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed 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 leap.lang;

public class Threads {

    public static void sleep(long ms) {
        Try.throwUnchecked(() -> Thread.sleep(ms));
    }

    protected Threads() {

    }
}

@@ -631,16 +631,99 @@
 .lang;%0A%0A
+import java.util.concurrent.TimeoutException;%0Aimport java.util.function.Supplier;%0A%0A
 public c
@@ -837,16 +837,844 @@
 %0A    %7D%0A%0A
+    public static %3CT%3E T waitAndGet(Supplier%3CT%3E func) throws TimeoutException %7B%0A        return waitAndGet(func, 1000L);%0A    %7D%0A%0A    public static %3CT%3E T waitAndGet(Supplier%3CT%3E func, long maxWait) throws TimeoutException %7B%0A        long timeout = maxWait;%0A        final long start = System.currentTimeMillis();%0A%0A        do%7B%0A            T item = func.get();%0A            if(null != item) %7B%0A                return item;%0A            %7D%0A%0A            //decrease the timeout%0A            timeout = maxWait - (System.currentTimeMillis() - start);%0A            if(timeout %3C= 0L) %7B%0A                //time out.%0A                break;%0A            %7D%0A%0A            try %7B%0A                Thread.sleep(1);%0A            %7D catch (InterruptedException e) %7B%0A%0A            %7D%0A%0A        %7Dwhile(timeout %3E 0);%0A%0A        throw new TimeoutException(%22timeout%22);%0A    %7D%0A%0A
     prot

add 'waitAndGet' at 'Threads'