You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2016/04/15 16:31:41 UTC

svn commit: r1739305 - /uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/Misc.java

Author: schor
Date: Fri Apr 15 14:31:41 2016
New Revision: 1739305

URL: http://svn.apache.org/viewvc?rev=1739305&view=rev
Log:
[UIMA-4897] Add elide and caller trace methods

Modified:
    uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/Misc.java

Modified: uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/Misc.java
URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/Misc.java?rev=1739305&r1=1739304&r2=1739305&view=diff
==============================================================================
--- uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/Misc.java (original)
+++ uima/uimaj/trunk/uimaj-core/src/main/java/org/apache/uima/util/Misc.java Fri Apr 15 14:31:41 2016
@@ -19,8 +19,71 @@
 
 package org.apache.uima.util;
 
+import java.util.regex.Pattern;
+
 public class Misc {
 
+  private static final Pattern whitespace = Pattern.compile("\\s");
+
+  public static String replaceWhiteSpace(String s, String replacement) {
+    return whitespace.matcher(s).replaceAll(replacement);
+  }
+  
+  
+  /**
+   * @param s starting frames above invoker
+   * @param n max number of callers to return
+   * @return  x called by: y ...
+   */
+  public static StringBuilder getCallers(final int s, final int n) {
+    StackTraceElement[] e = Thread.currentThread().getStackTrace();
+    StringBuilder sb = new StringBuilder();
+    for (int i = s + 2; i < s + n + 3; i++) {
+      if (i >= e.length) break;
+      if (i != s + 2) {
+        sb.append("  called_by: ");
+      }
+      sb.append(formatcaller(e[i]));      
+    }
+    return sb;
+  }
+
+  /**
+   * @return the name of the caller in the stack
+   */
+  public static String getCaller() {
+    StackTraceElement[] e = Thread.currentThread().getStackTrace();
+    return formatcaller(e[4]) + "called by: " + formatcaller(e[5]);
+  }
+  
+  private static String formatcaller(StackTraceElement e) {
+    String n = e.getClassName();
+    return n.substring(1 + n.lastIndexOf('.')) + "." + e.getMethodName() + "[" + e.getLineNumber() + "]";
+  }
+  
+
+  
+  public static final String blanks = "                                                     ";
+  public static final String dots = "...";
+  
+  public static String elide(String s, int n) {
+    return elide(s, n, true);
+  }
+  
+  public static String elide(String s, int n, boolean pad) {
+    if (s == null) {
+      s = "null";
+    }
+    int sl = s.length();
+    if (sl <= n) {
+      return s + (pad ? blanks.substring(0, n - sl) : "");
+    }
+    int dl = 1; // (n < 11) ? 1 : (n < 29) ? 2 : 3;  // number of dots
+    int ss = (n - dl) / 2;
+    int ss2 = ss + (( ss * 2 == (n - dl)) ? 0 : 1);
+    return s.substring(0, ss) + dots.substring(0, dl) + s.substring(sl - ss2);
+  }  
+
   /**
    * 
    * @param name of property
@@ -33,6 +96,8 @@ public class Misc {
   }
   
 //  public static void main(String[] args) {
+//    System.out.println(elide("uninflectedWord", 11));
+//  }
 //    System.out.println("should be false - not defined: " + getNoValueSystemProperty("foo"));
 //    System.setProperty("foo", "");
 //    System.out.println("should be true - defined, 0 len str value: " + getNoValueSystemProperty("foo"));