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.EqualsUtil.eq;
25  
26  import org.opencastproject.util.data.Function;
27  import org.opencastproject.util.data.Option;
28  
29  import java.util.List;
30  import java.util.Optional;
31  
32  /** Various functions not directly bound to any type. */
33  public final class Misc {
34    private Misc() {
35    }
36  
37    private static <T extends Throwable, A> A castGeneric(Throwable t) throws T {
38      // The cast to T does not happen here but _after_ returning from the method at _assignment_ time
39      // But since there is no variable assignment. The Throwable is just thrown.
40      throw (T) t;
41    }
42  
43    /**
44     * Throw a checked exception like a RuntimeException removing any needs to declare a throws clause.
45     *
46     * This technique has been described by James Iry at
47     * http://james-iry.blogspot.de/2010/08/on-removing-java-checked-exceptions-by.html
48     */
49    public static <A> A chuck(Throwable t) {
50      return Misc.<RuntimeException, A> castGeneric(t);
51    }
52  
53    /** {@link #chuck(Throwable)} as a function. */
54    public static <A extends Throwable, B> Function<A, B> chuck() {
55      return new Function<A, B>() {
56        @Override
57        public B apply(Throwable throwable) {
58          return chuck(throwable);
59        }
60      };
61    }
62  
63    /** Cast from A to B with special treatment of the Number classes. */
64    public static <A, B> B cast(A v, Class<B> to) {
65      if (Number.class.isAssignableFrom(v.getClass())) {
66        if (eq(Integer.class, to)) {
67          return (B) ((Object) (((Number) v).intValue()));
68        } else if (eq(Long.class, to)) {
69          return (B) ((Object) (((Number) v).longValue()));
70        } else if (eq(Double.class, to)) {
71          return (B) ((Object) (((Number) v).doubleValue()));
72        } else if (eq(Float.class, to)) {
73          return (B) ((Object) (((Number) v).floatValue()));
74        } else if (eq(Short.class, to)) {
75          return (B) ((Object) (((Number) v).shortValue()));
76        } else if (eq(Byte.class, to)) {
77          return (B) ((Object) (((Number) v).byteValue()));
78        } else {
79          return (B) v;
80        }
81      } else if (to.isAssignableFrom(v.getClass())) {
82        return (B) v;
83      } else {
84        throw new ClassCastException(v.getClass().getName() + " is not of type " + to.getName());
85      }
86    }
87  
88    /** Cast from A to B. */
89    public static <A, B> Function<A, B> cast() {
90      return new Function<A, B>() {
91        @Override
92        public B apply(A a) {
93          return (B) a;
94        }
95      };
96    }
97  
98    /** Widening cast. */
99    public static <A> List<A> widen(List<? extends A> xs) {
100     return (List<A>) xs;
101   }
102 
103   /** Widening cast. */
104   public static <A> Option<A> widen(Option<? extends A> xs) {
105     return (Option<A>) xs;
106   }
107 
108   /** Widening cast. */
109   public static <A> Optional<A> widen(Optional<? extends A> xs) {
110     return (Optional<A>) xs;
111   }
112 
113   public static <A> Function<A, A> ifThen(final A predicate, final A b) {
114     return new Function<A, A>() {
115       @Override
116       public A apply(A a) {
117         return predicate.equals(a) ? b : a;
118       }
119     };
120   }
121 }