Coverage Report - com.sun.javafx.runtime.Util
 
Classes in this File Line Coverage Branch Coverage Complexity
Util
79%
31/39
80%
16/20
0
 
 1  
 package com.sun.javafx.runtime;
 2  
 
 3  
 import java.lang.reflect.Array;
 4  
 import java.net.MalformedURLException;
 5  
 import java.net.URL;
 6  
 
 7  
 import com.sun.javafx.runtime.location.SequenceLocation;
 8  
 import com.sun.javafx.runtime.sequence.Sequence;
 9  
 
 10  
 /**
 11  
  * Utility class for various static utility methods, such as methods that launder generic type errors that are
 12  
  * known to be safe.
 13  
  *
 14  
  * @author Brian Goetz
 15  
  */
 16  0
 public class Util {
 17  
     @SuppressWarnings("unchecked")
 18  
     public static<T> T[] newObjectArray(int size) {
 19  13699
         return (T[]) new Object[size];
 20  
     }
 21  
 
 22  
     @SuppressWarnings("unchecked")
 23  
     public static<T> Sequence<T>[] newSequenceArray(int size) {
 24  139
         return (Sequence<T>[]) new Sequence[size];
 25  
     }
 26  
 
 27  
     @SuppressWarnings("unchecked")
 28  
     public static<T> SequenceLocation<T>[] newSequenceLocationArray(int size) {
 29  38
         return (SequenceLocation<T>[]) new SequenceLocation[size];
 30  
     }
 31  
 
 32  
     public static int powerOfTwo(int current, int desired) {
 33  4661
         int capacity = current == 0 ? 1 : current;
 34  24882
         while (capacity < desired)
 35  20221
             capacity <<= 1;
 36  4661
         return capacity;
 37  
     }
 38  
 
 39  
     /**
 40  
      * Return the default value for a type.
 41  
      * @param clazz the class to use to determine default value Class<T>
 42  
      * @return the default value
 43  
      */
 44  
     @SuppressWarnings("unchecked")
 45  
     public static<T> T defaultValue(Class clazz) {
 46  7
         if (clazz == Integer.class)
 47  0
             return (T) Integer.valueOf(0);
 48  7
         else if (clazz == Double.class)
 49  0
             return (T) Double.valueOf(0.0);
 50  7
         else if (clazz == Boolean.class)
 51  0
             return (T) Boolean.FALSE;
 52  7
         else if (clazz == String.class)
 53  2
             return (T) "";
 54  
         else
 55  5
             return null;
 56  
     }
 57  
 
 58  
     @SuppressWarnings("unchecked")
 59  
     public static<T> T[] newArray(Class<?> clazz, int size) {
 60  151
         return (T[]) Array.newInstance(clazz, size);
 61  
     }
 62  
 
 63  
     @SuppressWarnings("unchecked")
 64  
     public static<T> T[] replaceSlice(T[] array, int startPos, int endPos, T[] newElements) {
 65  55
         int insertedCount = newElements.length;
 66  55
         int deletedCount = endPos - startPos + 1;
 67  55
         int netAdded = insertedCount - deletedCount;
 68  55
         if (netAdded == 0) {
 69  1
             System.arraycopy(newElements, 0, array, startPos, insertedCount);
 70  1
             return array;
 71  
         }
 72  
         else {
 73  54
             T[] temp = (T[]) newArray(array.getClass().getComponentType(), array.length + netAdded);
 74  54
             System.arraycopy(array, 0, temp, 0, startPos);
 75  54
             System.arraycopy(newElements, 0, temp, startPos, insertedCount);
 76  54
             System.arraycopy(array, endPos + 1, temp, startPos + insertedCount, array.length - (endPos + 1));
 77  54
             return temp;
 78  
         }
 79  
     }
 80  
 
 81  
     /**
 82  
      * Returns the __FILE__ pseudo-variable for a module.
 83  
      * 
 84  
      * @param moduleClass the fully-qualified name of the module class
 85  
      * @return the resource URL to the module's class
 86  
      */
 87  
     public static URL get__FILE__(Class<?> moduleClass) {
 88  
         try {
 89  3
             String resource = moduleClass.getName().replace(".", "/") + ".class";
 90  3
             return moduleClass.getClassLoader().getResource(resource);
 91  0
         } catch (Throwable t) {
 92  0
             return null;
 93  
         }
 94  
     }
 95  
     
 96  
     /**
 97  
      * Returns the __DIR__ pseudo-variable for a module.
 98  
      * @param __FILE__ the module's __FILE__ pseudo-variable
 99  
      * @return the module's __DIR__ URL
 100  
      */
 101  
     public static URL get__DIR__(URL __FILE__) {
 102  
         try {
 103  2
             return __FILE__ == null ? null : new java.net.URL(__FILE__, ".");
 104  0
         } catch (MalformedURLException ex) {
 105  0
             return null;
 106  
         }
 107  
     }
 108  
 
 109  
     public static<T> boolean isEqual(T oldValue, T newValue) {
 110  4725
         if (oldValue == null) {
 111  3794
             return newValue == null;
 112  
         } else
 113  931
             return oldValue.equals(newValue);
 114  
     }
 115  
 }