View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.util;
23  
24  import java.util.Optional;
25  import java.util.concurrent.Callable;
26  import java.util.concurrent.atomic.AtomicBoolean;
27  
28  /** Only one function application can be threaded through the needle eye at a time. */
29  public final class NeedleEye {
30    private final AtomicBoolean running = new AtomicBoolean(false);
31  
32    /**
33     * Apply function <code>f</code> only if no other thread currently applies a function using this needle eye.
34     *
35     * @return the result of <code>f</code> or none if another function is currently being applied.
36     */
37    public <A> Optional<A> apply(Callable<A> f) {
38      if (running.compareAndSet(false, true)) {
39        try {
40          return Optional.ofNullable(f.call());
41        } catch (Exception e) {
42          throw new RuntimeException("Exception in NeedleEye function", e);
43        } finally {
44          running.set(false);
45        }
46      } else {
47        return Optional.empty();
48      }
49    }
50  }