1 package valhalla.vector;
   2 
   3 import jdk.experimental.value.ValueType;
   4 
   5 import java.lang.reflect.Field;
   6 import java.lang.reflect.Modifier;
   7 import java.util.concurrent.Callable;
   8 import java.util.stream.Stream;
   9 
  10 public class Utils {
  11     static void assertEquals(Object o1, Object o2) {
  12         if ((o1 == null && o2 != null) || !o1.equals(o2)) {
  13             throw new AssertionError(o1 + " not equals to " + o2);
  14         }
  15     }
  16 
  17     public interface RunnableWithThrowable {
  18         void run() throws Throwable;
  19     }
  20 
  21     public static void run(RunnableWithThrowable r) {
  22         try {
  23             r.run();
  24         } catch (Throwable e) {
  25             throw new Error(e);
  26         }
  27     }
  28 
  29     public interface CallableWithThrowable<T> {
  30         T call() throws Throwable;
  31     }
  32 
  33     public static <T> T compute(CallableWithThrowable<T> c) {
  34         try {
  35             return c.call();
  36         } catch (Throwable e) {
  37             throw new Error(e);
  38         }
  39     }
  40 
  41     // Copied from jdk.experimental.value.ValueType
  42     public static Field[] valueFields(ValueType vt) {
  43         int valFieldMask = Modifier.FINAL;
  44         return Stream.of(vt.sourceClass().getDeclaredFields())
  45                 .filter(f -> (f.getModifiers() & (valFieldMask | Modifier.STATIC)) == valFieldMask)
  46                 .toArray(Field[]::new);
  47     }
  48 
  49 }