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