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  
23  package org.opencastproject.util.data;
24  
25  import static org.opencastproject.util.data.functions.Misc.chuck;
26  
27  import org.opencastproject.util.data.functions.Functions;
28  
29  /**
30   * Function of arity 0, i.e. a constant function.
31   *
32   * @see X
33   */
34  public abstract class Function0<A> {
35    /** Apply function yielding a constant value. Don't be tempted to become impure! */
36    public abstract A apply();
37  
38    /** Apply this function, then pass the result to <code>f</code>. */
39    public <B> Function0<B> then(final Function<A, B> f) {
40      return Functions.then(Function0.this, f);
41    }
42  
43    /** Apply this function and ignore its result, then apply <code>f</code>. */
44    public <B> Function0<B> then(final Function0<B> f) {
45      return Functions.then(Function0.this, f);
46    }
47  
48    /** Version of {@link Function0} that allows for throwing a checked exception. */
49    public abstract static class X<A> extends Function0<A> {
50      @Override
51      public final A apply() {
52        try {
53          return xapply();
54        } catch (Exception e) {
55          return chuck(e);
56        }
57      }
58  
59      /**
60       * Apply function to <code>a</code>. Any thrown exception gets "chucked" so that you may
61       * catch them as is. See {@link Functions#chuck(Throwable)} for details.
62       */
63      public abstract A xapply() throws Exception;
64    }
65  }