1 /******************************************************************************
2 * Copyright (c) 2000-2015 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 ******************************************************************************/
8 package org
.eclipse
.titan
.executorapi
.util
;
10 import java
.lang
.reflect
.Array
;
11 import java
.util
.List
;
15 * String related utility functions, which are used for logging and in toString() functions.
17 * Dependencies (these functions are needed by):
18 * appendObject(): Log.fi(), Log.fo(), ComponentStruct.toString(), HostStruct.toString()
19 * toObjectArray(): appendObject()
20 * isPrimitiveArray: appendObject()
21 * indent(): ComponentStruct.toString()
22 * replaceString(): indent(), Log.f()
24 public class StringUtil
{
27 * Replaces all the occurrences of a given string
28 * @param aSb [in/out] the string to modify
29 * @param aFrom [in] the string to replace
30 * @param aTo [in] the replacement string
32 public static void replaceString( final StringBuilder aSb
, final String aFrom
, final String aTo
) {
33 int index
= aSb
.indexOf( aFrom
);
35 aSb
.replace( index
, index
+ aFrom
.length(), aTo
);
36 index
+= aTo
.length(); // Move to the end of the replacement
37 index
= aSb
.indexOf( aFrom
, index
);
42 * Appends any Object to the given StringBuilder.
44 * It works with the following data types:
48 * <li> array of Object
49 * <li> array of any primitive type, like int[], boolean[], ...
52 * @param aSb [in/out] the StringBuilder to extend
53 * @param aObject [in] object to add
55 public static void appendObject( final StringBuilder aSb
, final Object aObject
) {
56 final Object o
= aObject
;
60 else if ( o
instanceof String
) {
62 aSb
.append(o
.toString());
65 else if ( o
instanceof Object
[] ) {
66 final Object
[] oArray
= (Object
[])o
;
68 if ( oArray
.length
> 0 ) {
70 for ( int i
= 0; i
< oArray
.length
; i
++ ) {
74 appendObject(aSb
, oArray
[i
]);
80 else if ( isPrimitiveArray( o
) ) {
81 appendObject( aSb
, toObjectArray( o
) );
83 else if ( o
instanceof List
) {
84 final List
<?
> oList
= (List
<?
>)o
;
86 if ( oList
.size() > 0 ) {
88 for ( int i
= 0; i
< oList
.size(); i
++ ) {
92 appendObject(aSb
, oList
.get(i
));
98 else if ( o
instanceof Map
) {
99 final Map
<?
,?
> oMap
= (Map
<?
,?
>)o
;
101 if ( oMap
.size() > 0 ) {
104 for (Object key
: oMap
.keySet()) {
108 appendObject(aSb
, key
);
110 appendObject(aSb
, oMap
.get( key
) );
117 aSb
.append(o
.toString());
122 * Changes the indentation of the string, a requested string is appended after every EOFs in the string.
123 * @param aSb [in/out] the string to modify
124 * @param aIndentString [in] the requested string to append
126 public static void indent( final StringBuilder aSb
, final String aIndentString
) {
127 replaceString( aSb
, "\n", "\n" + aIndentString
);
131 * @param aPrimitiveArrayCandidate the object to check
132 * @return true, if aPrimitiveArrayCandidate is a primitive array, like int[], boolean[], ...
135 private static boolean isPrimitiveArray( final Object aPrimitiveArrayCandidate
) {
136 return aPrimitiveArrayCandidate
!= null &&
137 aPrimitiveArrayCandidate
.getClass().isArray() &&
138 aPrimitiveArrayCandidate
.getClass().getComponentType().isPrimitive();
142 * Converts primitive array to Object array.
144 * Prerequisite: aPrimitiveArray must be primitive array or Object array
145 * @param aPrimitiveArray a primitive array, like int[], boolean[], ...
146 * @return result Object[] representation of aPrimitiveArray
147 * @see <a href="http://stackoverflow.com/questions/5606338/cast-primitive-type-array-into-object-array-in-java">stackoverflow.com</a>
148 * @see #isPrimitiveArray(Object)
150 private static Object
[] toObjectArray( final Object aPrimitiveArray
) {
151 if ( aPrimitiveArray
instanceof Object
[] ) {
152 return ( Object
[] )aPrimitiveArray
;
154 int arrlength
= Array
.getLength( aPrimitiveArray
);
155 Object
[] outputArray
= new Object
[ arrlength
];
156 for(int i
= 0; i
< arrlength
; ++i
) {
157 outputArray
[i
] = Array
.get( aPrimitiveArray
, i
);