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.data.functions;
23  
24  import static org.opencastproject.util.data.Monadics.mlist;
25  import static org.opencastproject.util.data.Option.some;
26  
27  import org.opencastproject.util.data.Effect;
28  import org.opencastproject.util.data.Function;
29  import org.opencastproject.util.data.Function2;
30  import org.opencastproject.util.data.Option;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  /** {@link Option} related functions. */
36  public final class Options {
37    private Options() {
38    }
39  
40    /** m (m a) -> m a */
41    public static <A> Option<A> join(Option<Option<A>> a) {
42      return a.bind(Functions.<Option<A>> identity());
43    }
44  
45    public static <A> Function<Option<A>, List<A>> asList() {
46      return new Function<Option<A>, List<A>>() {
47        @Override
48        public List<A> apply(Option<A> a) {
49          return a.list();
50        }
51      };
52    }
53  
54    /** Returns some(message) if predicate is false, none otherwise. */
55    public static Option<String> toOption(boolean predicate, String message) {
56      return predicate ? Option.<String> none() : some(message);
57    }
58  
59    /** Sequence a list of options. [Option a] -&gt; Option [a] */
60    public static <A> Option<List<A>> sequenceOpt(List<Option<A>> as) {
61      final List<A> seq = mlist(as).foldl(new ArrayList<A>(), new Function2<List<A>, Option<A>, List<A>>() {
62        @Override
63        public List<A> apply(List<A> sum, Option<A> o) {
64          for (A a : o) {
65            sum.add(a);
66            return sum;
67          }
68          return sum;
69        }
70      });
71      return some(seq);
72    }
73  
74    /** Apply effect <code>e</code> to the result of <code>f</code> which is then returned. */
75    public static <A, B> Function<A, Option<B>> foreach(final Function<? super A, ? extends Option<? extends B>> f,
76            final Effect<? super B> e) {
77      return new Function<A, Option<B>>() {
78        @Override
79        public Option<B> apply(A a) {
80          return (Option<B>) f.apply(a).foreach(e);
81        }
82      };
83    }
84  
85  }