You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/12/18 15:29:49 UTC
svn commit: r357465 - in /directory/asn1/trunk/codec/src:
main/java/org/apache/asn1/codec/util/StringUtils.java
test/org/apache/asn1/codec/util/StringUtilsTest.java
Author: elecharny
Date: Sun Dec 18 06:29:42 2005
New Revision: 357465
URL: http://svn.apache.org/viewcvs?rev=357465&view=rev
Log:
Added utilities function to transform a List and a Map
to a String. These two function will be used by many toString()
methods.
Modified:
directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java
directory/asn1/trunk/codec/src/test/org/apache/asn1/codec/util/StringUtilsTest.java
Modified: directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java
URL: http://svn.apache.org/viewcvs/directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java?rev=357465&r1=357464&r2=357465&view=diff
==============================================================================
--- directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java (original)
+++ directory/asn1/trunk/codec/src/main/java/org/apache/asn1/codec/util/StringUtils.java Sun Dec 18 06:29:42 2005
@@ -17,6 +17,9 @@
package org.apache.asn1.codec.util;
import java.io.UnsupportedEncodingException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
/**
* Little helper class. Nothing that should stay here, but I need those
@@ -1283,5 +1286,76 @@
{
return new byte[]{};
}
+ }
+
+ /**
+ * Utility method that return a String representation of a list
+ * @param list The list to transform to a string
+ * @return A csv string
+ */
+ public static String listToString( List list )
+ {
+ if ( ( list == null ) || ( list.size() == 0 ) )
+ {
+ return "";
+ }
+
+ StringBuffer sb = new StringBuffer();
+ boolean isFirst = true;
+
+ Iterator iter = list.iterator();
+
+ while ( iter.hasNext() )
+ {
+ if ( isFirst)
+ {
+ isFirst = false;
+ }
+ else
+ {
+ sb.append( ", " );
+ }
+
+ sb.append( iter.next() );
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * Utility method that return a String representation of a map. The elements
+ * will be represented as "key = value"
+ * @param map The map to transform to a string
+ * @return A csv string
+ */
+ public static String mapToString( Map map )
+ {
+ if ( ( map == null ) || ( map.size() == 0 ) )
+ {
+ return "";
+ }
+
+ StringBuffer sb = new StringBuffer();
+ boolean isFirst = true;
+
+ Iterator iter = map.keySet().iterator();
+
+ while ( iter.hasNext() )
+ {
+ if ( isFirst)
+ {
+ isFirst = false;
+ }
+ else
+ {
+ sb.append( ", " );
+ }
+
+ Object key = iter.next();
+ sb.append( key );
+ sb.append( " = '" ).append( map.get( key ) ).append( "'" );
+ }
+
+ return sb.toString();
}
}
Modified: directory/asn1/trunk/codec/src/test/org/apache/asn1/codec/util/StringUtilsTest.java
URL: http://svn.apache.org/viewcvs/directory/asn1/trunk/codec/src/test/org/apache/asn1/codec/util/StringUtilsTest.java?rev=357465&r1=357464&r2=357465&view=diff
==============================================================================
--- directory/asn1/trunk/codec/src/test/org/apache/asn1/codec/util/StringUtilsTest.java (original)
+++ directory/asn1/trunk/codec/src/test/org/apache/asn1/codec/util/StringUtilsTest.java Sun Dec 18 06:29:42 2005
@@ -1,6 +1,11 @@
package org.apache.asn1.codec.util;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import junit.framework.Assert;
import junit.framework.TestCase;
@@ -52,4 +57,51 @@
Assert.assertEquals(0x7347A563, res);
}
*/
+
+ public void testListToString()
+ {
+ List list = new ArrayList();
+
+ list.add( "elem1" );
+ list.add( "elem2" );
+ list.add( "elem3" );
+
+ Assert.assertEquals( "elem1, elem2, elem3", StringUtils.listToString( list ) );
+ }
+
+ public void testMapToString()
+ {
+ class Value
+ {
+ String name;
+ int val;
+
+ public Value( String name, int val)
+ {
+ this.name = name;
+ this.val = val;
+ }
+
+ public String toString()
+ {
+ return "[" + name + ", " + val + "]";
+ }
+ }
+ Map map = new HashMap();
+
+ map.put( "elem1", new Value( "name1", 1) );
+ map.put( "elem2", new Value( "name2", 2) );
+ map.put( "elem3", new Value( "name3", 3) );
+
+ String result = StringUtils.mapToString( map );
+
+ boolean res = "elem1 = '[name1, 1]', elem2 = '[name2, 2]', elem3 = '[name3, 3]'".equals( result) ||
+ "elem1 = '[name1, 1]', elem3 = '[name3, 3]', elem2 = '[name2, 2]'".equals( result) ||
+ "elem2 = '[name2, 2]', elem1 = '[name1, 1]', elem3 = '[name3, 3]'".equals( result) ||
+ "elem2 = '[name2, 2]', elem3 = '[name3, 3]', elem1 = '[name1, 1]'".equals( result) ||
+ "elem3 = '[name3, 3]', elem1 = '[name1, 1]', elem2 = '[name2, 2]'".equals( result) ||
+ "elem3 = '[name3, 3]', elem3 = '[name2, 2]', elem1 = '[name1, 1]'".equals( result);
+
+ Assert.assertTrue( res );
+ }
}