Changeset 1179

Show
Ignore:
Timestamp:
05/27/08 07:41:53 (3 months ago)
Author:
phoenix
Message:

Simplified ExceptionAdapter?, added some more util methods. (now commited in the right place)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/descent.core/src-util/melnorme/miscutil/ArrayUtil.java

    r675 r1179  
    11package melnorme.miscutil; 
    22 
     3import static melnorme.miscutil.Assert.assertTrue; 
     4 
    35import java.lang.reflect.Array; 
     6import java.util.List; 
    47 
    58public class ArrayUtil { 
     
    7578        return false; 
    7679    } 
     80     
     81    /** Return the index of the first occurrence of elem in array, or -1 if no occurrences. */ 
     82    public static <T> int indexOf(T[] array, T elem) { 
     83        for (int i = 0; i < array.length; i++) { 
     84            if(array[i].equals(elem)) 
     85                return i; 
     86        } 
     87        return -1; 
     88    } 
     89     
     90    /** Return the index of the first occurrence of elem in array, or -1 if no occurrences. */ 
     91    public static int indexOf(byte[] array, byte elem) { 
     92        for (int i = 0; i < array.length; i++) { 
     93            if(array[i] == elem) 
     94                return i; 
     95        } 
     96        return -1; 
     97    } 
     98 
    7799     
    78100    /** 
     
    152174        return copy; 
    153175    } 
     176     
     177    /** Copies src array range [0 .. src.length] to dest array starting at destIx. */ 
     178    public static void copyToRange(byte[] src, byte[] dest, int destIx) { 
     179        assertTrue(src.length < dest.length - destIx); 
     180        System.arraycopy(src, 0, dest, destIx, src.length); 
     181    } 
     182 
     183    /** Create an array from the given list, with the given run-time  
     184     * component type. 
     185     * If the list is null, a zero-length array is created. */ 
     186    @SuppressWarnings("unchecked") 
     187    public static <T> T[] createFrom(List<T> list, Class<T> cpType) { 
     188        if(list == null) 
     189            return (T[])Array.newInstance(cpType, 0); 
     190     
     191        return list.toArray((T[])Array.newInstance(cpType, list.size())); 
     192    } 
     193 
     194    /** Creates an array with the same size as the given list. 
     195     * If the list is null, a zero-length array is created. */ 
     196    @SuppressWarnings("unchecked") 
     197    public static <T> T[] newSameSize(List<?> list, Class<T> cpType) { 
     198        if(list == null) 
     199            return (T[])Array.newInstance(cpType, 0); 
     200         
     201        return (T[])Array.newInstance(cpType, list.size()); 
     202    } 
     203 
     204    /** Finds the index in the given array of the element that 
     205     * equal given elem. */ 
     206    public static <T> int getIndexOfEquals(T[] arr, T elem) { 
     207        for (int i = 0; i < arr.length; i++) { 
     208            if(arr[i].equals(elem)); 
     209                return i; 
     210        } 
     211        return -1; 
     212    } 
    154213 
    155214 
  • trunk/descent.core/src-util/melnorme/miscutil/Assert.java

    r711 r1179  
    8282     
    8383    /** Causes an inconditional assertion failure, with message msg. 
     84     * Never returns, even though return type is not void. 
    8485     */ 
    85     public static void fail(String msg) { 
     86    public static AssertionFailedException fail(String msg) { 
    8687        throw new AssertionFailedException("ASSERT FAIL:" + msg); 
    8788    } 
     
    8990    /** Like {@link #fail(String)} with empty message.  
    9091     */ 
    91     public static void fail() { 
     92    public static AssertionFailedException fail() { 
    9293        throw new AssertionFailedException("ASSERT FAIL"); 
    9394    } 
     
    139140    /** Causes an inconditional assertion failure, with message msg. 
    140141     */ 
    141     public static void assertFail(String msg) { 
    142         Assert.fail(msg); 
     142    public static AssertionFailedException assertFail(String msg) { 
     143        return Assert.fail(msg); 
    143144    } 
    144145     
    145146    /** Like {@link #fail(String)} with empty message.  
    146147     */ 
    147     public static void assertFail() { 
    148         Assert.fail(); 
     148    public static AssertionFailedException assertFail() { 
     149        return Assert.fail(); 
    149150    } 
    150151     
  • trunk/descent.core/src-util/melnorme/miscutil/ExceptionAdapter.java

    r646 r1179  
    11package melnorme.miscutil; 
     2 
     3import java.io.IOException; 
    24 
    35 
     
    3638*/ 
    3739     
    38     protected void printStackTrace(melnorme.miscutil.log.IPrinter pr) { 
    39         synchronized (pr) { 
    40             pr.println(this); 
    41             StackTraceElement[] trace = originalException.getStackTrace(); 
    42             for (int i=0; i < trace.length; i++) { 
    43                 pr.print("\tat " + trace[i]); 
    44                 if(i == checkedLength) 
    45                     pr.print(" [UNCHECKED]"); 
    46                 pr.println(); 
    47             } 
     40     
     41    protected void printStackTrace(Appendable pr) { 
     42        synchronized(pr) { 
     43            try { 
     44                pr.append(this.toString()); 
     45                StackTraceElement[] trace = originalException.getStackTrace(); 
     46                for (int i=0; i < trace.length; i++) { 
     47                    pr.append("\tat " + trace[i]); 
     48                    if(i == checkedLength) 
     49                        pr.append(" [UNCHECKED]"); 
     50                    pr.append("\n"); 
     51                } 
     52            } catch (IOException e) { 
     53                melnorme.miscutil.Assert.assertFail(); 
     54            } 
    4855        } 
    4956    } 
    5057     
    51     public void printStackTrace(java.io.PrintStream ps) { 
    52         printStackTrace(new melnorme.miscutil.log.StreamPrinter(ps)); 
     58    @Override 
     59    public void printStackTrace(java.io.PrintStream ps) {        
     60        printStackTrace(ps); 
    5361    } 
    5462 
     63    @Override 
    5564    public void printStackTrace(java.io.PrintWriter pw) { 
    56         printStackTrace(new melnorme.miscutil.log.WriterPrinter(pw)); 
     65        printStackTrace(pw); 
    5766    } 
    5867 
     
    6271    } 
    6372 
     73    @Override 
    6474    public String toString() { 
    6575        //String name = getClass().getName(); 
  • trunk/descent.core/src-util/melnorme/miscutil/IteratorUtil.java

    r646 r1179  
    66public class IteratorUtil {  
    77 
    8     public static final Iterator EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator(); 
     8    public static final Iterator<?> EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator(); 
    99     
    1010    @SuppressWarnings("unchecked") 
    1111    public static <T> Iterator<T> getEMPTY_ITERATOR() { 
    12         return EMPTY_ITERATOR; 
     12        return (Iterator<T>) EMPTY_ITERATOR; 
    1313    } 
    1414 
  • trunk/descent.core/src-util/melnorme/miscutil/StringUtil.java

    r671 r1179  
    8282            return new String[0]; 
    8383        String[] strs = new String[coll.size()]; 
    84         Iterator iter = coll.iterator(); 
     84        Iterator<?> iter = coll.iterator(); 
    8585        for (int i = 0; i < strs.length; i++) { 
    8686            strs[i] = iter.next().toString(); 
  • trunk/descent.core/src-util/melnorme/miscutil/tree/TreeDepthRecon.java

    r653 r1179  
    3939     
    4040     
     41    @Override 
    4142    public boolean enterNode(IElement element) { 
    4243        depth++; 
     
    5657    } 
    5758     
     59    @Override 
    5860    public void leaveNode(IElement element) { 
    5961        depth--;