Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright 2017, OpenCensus Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package io.opencensus.common;
     18 
     19 /*>>>
     20 import org.checkerframework.checker.nullness.qual.Nullable;
     21 */
     22 
     23 /**
     24  * Commonly used {@link Function} instances.
     25  *
     26  * @since 0.5
     27  */
     28 public final class Functions {
     29   private Functions() {}
     30 
     31   private static final Function<Object, /*@Nullable*/ Void> RETURN_NULL =
     32       new Function<Object, /*@Nullable*/ Void>() {
     33         @Override
     34         @javax.annotation.Nullable
     35         public Void apply(Object ignored) {
     36           return null;
     37         }
     38       };
     39 
     40   private static final Function<Object, Void> THROW_ILLEGAL_ARGUMENT_EXCEPTION =
     41       new Function<Object, Void>() {
     42         @Override
     43         public Void apply(Object ignored) {
     44           throw new IllegalArgumentException();
     45         }
     46       };
     47 
     48   private static final Function<Object, Void> THROW_ASSERTION_ERROR =
     49       new Function<Object, Void>() {
     50         @Override
     51         public Void apply(Object ignored) {
     52           throw new AssertionError();
     53         }
     54       };
     55 
     56   private static final Function<Object, /*@Nullable*/ String> RETURN_TO_STRING =
     57       new Function<Object, /*@Nullable*/ String>() {
     58         @Override
     59         public /*@Nullable*/ String apply(Object input) {
     60           return input == null ? null : input.toString();
     61         }
     62       };
     63 
     64   /**
     65    * A {@code Function} that always ignores its argument and returns {@code null}.
     66    *
     67    * @return a {@code Function} that always ignores its argument and returns {@code null}.
     68    * @since 0.5
     69    */
     70   public static <T> Function<Object, /*@Nullable*/ T> returnNull() {
     71     // It is safe to cast a producer of Void to anything, because Void is always null.
     72     @SuppressWarnings("unchecked")
     73     Function<Object, /*@Nullable*/ T> function = (Function<Object, /*@Nullable*/ T>) RETURN_NULL;
     74     return function;
     75   }
     76 
     77   /**
     78    * A {@code Function} that always ignores its argument and returns a constant value.
     79    *
     80    * @return a {@code Function} that always ignores its argument and returns a constant value.
     81    * @since 0.5
     82    */
     83   public static <T> Function<Object, T> returnConstant(final T constant) {
     84     return new Function<Object, T>() {
     85       @Override
     86       public T apply(Object ignored) {
     87         return constant;
     88       }
     89     };
     90   }
     91 
     92   /**
     93    * A {@code Function} that always returns the {@link #toString()} value of the input.
     94    *
     95    * @return a {@code Function} that always returns the {@link #toString()} value of the input.
     96    * @since 0.17
     97    */
     98   public static Function<Object, /*@Nullable*/ String> returnToString() {
     99     return RETURN_TO_STRING;
    100   }
    101 
    102   /**
    103    * A {@code Function} that always ignores its argument and throws an {@link
    104    * IllegalArgumentException}.
    105    *
    106    * @return a {@code Function} that always ignores its argument and throws an {@link
    107    *     IllegalArgumentException}.
    108    * @since 0.5
    109    */
    110   public static <T> Function<Object, T> throwIllegalArgumentException() {
    111     // It is safe to cast this function to have any return type, since it never returns a result.
    112     @SuppressWarnings("unchecked")
    113     Function<Object, T> function = (Function<Object, T>) THROW_ILLEGAL_ARGUMENT_EXCEPTION;
    114     return function;
    115   }
    116 
    117   /**
    118    * A {@code Function} that always ignores its argument and throws an {@link AssertionError}.
    119    *
    120    * @return a {@code Function} that always ignores its argument and throws an {@code
    121    *     AssertionError}.
    122    * @since 0.6
    123    */
    124   public static <T> Function<Object, T> throwAssertionError() {
    125     // It is safe to cast this function to have any return type, since it never returns a result.
    126     @SuppressWarnings("unchecked")
    127     Function<Object, T> function = (Function<Object, T>) THROW_ASSERTION_ERROR;
    128     return function;
    129   }
    130 }
    131