Author: steve.ebersole(a)jboss.com
Date: 2009-03-05 16:51:53 -0500 (Thu, 05 Mar 2009)
New Revision: 16089
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/util/StringHelper.java
Log:
HHH-3800 - Allow chopping of class names in various logging scenarios
Modified:
core/branches/Branch_3_3/core/src/main/java/org/hibernate/util/StringHelper.java
===================================================================
---
core/branches/Branch_3_3/core/src/main/java/org/hibernate/util/StringHelper.java 2009-03-05
21:50:15 UTC (rev 16088)
+++
core/branches/Branch_3_3/core/src/main/java/org/hibernate/util/StringHelper.java 2009-03-05
21:51:53 UTC (rev 16089)
@@ -154,7 +154,7 @@
public static String unqualify(String qualifiedName) {
int loc = qualifiedName.lastIndexOf(".");
- return ( loc < 0 ) ? qualifiedName : qualifiedName.substring(
qualifiedName.lastIndexOf(".") + 1 );
+ return ( loc < 0 ) ? qualifiedName : qualifiedName.substring( loc + 1 );
}
public static String qualifier(String qualifiedName) {
@@ -162,6 +162,58 @@
return ( loc < 0 ) ? "" : qualifiedName.substring( 0, loc );
}
+ /**
+ * Collapses a name. Mainly intended for use with classnames, where an example might
serve best to explain.
+ * Imagine you have a class named
<samp>'org.hibernate.util.StringHelper'</samp>; calling collapse on
that
+ * classname will result in <samp>'o.h.u.StringHelper'<samp>.
+ *
+ * @param name The name to collapse.
+ * @return The collapsed name.
+ */
+ public static String collapse(String name) {
+ int breakPoint = name.lastIndexOf( '.' );
+ if ( breakPoint < 0 ) {
+ return name;
+ }
+ return collapseQualifier( name.substring( 0, breakPoint ), true ) + name.substring(
breakPoint ); // includes last '.'
+ }
+
+ /**
+ * Given a qualifier, collapse it.
+ *
+ * @param qualifier The qualifier to collapse.
+ * @param includeDots Should we include the dots in the collapsed form?
+ *
+ * @return The collapsed form.
+ */
+ public static String collapseQualifier(String qualifier, boolean includeDots) {
+ StringTokenizer tokenizer = new StringTokenizer( qualifier, "." );
+ String collapsed = Character.toString( tokenizer.nextToken().charAt( 0 ) );
+ while ( tokenizer.hasMoreTokens() ) {
+ if ( includeDots ) {
+ collapsed += '.';
+ }
+ collapsed += tokenizer.nextToken().charAt( 0 );
+ }
+ return collapsed;
+ }
+
+ /**
+ * Partially unqualifies a qualified name. For example, with a base of
'org.hibernate' the name
+ * 'org.hibernate.util.StringHelper' would become 'util.StringHelper'.
+ *
+ * @param name The (potentially) qualified name.
+ * @param qualifierBase The qualifier base.
+ *
+ * @return The name itself, or the partially unqualified form if it begins with the
qualifier base.
+ */
+ public static String partiallyUnqualify(String name, String qualifierBase) {
+ if ( ! name.startsWith( qualifierBase ) ) {
+ return name;
+ }
+ return name.substring( qualifierBase.length() + 1 ); // +1 to include the following
'.'
+ }
+
public static String[] suffix(String[] columns, String suffix) {
if ( suffix == null ) return columns;
String[] qualified = new String[columns.length];
Show replies by date