Sync with 5.4.0
[deliverable/titan.core.git] / titan_executor_api / TITAN_Executor_API / src / org / eclipse / titan / executorapi / util / StringUtil.java
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;
9
10 import java.lang.reflect.Array;
11 import java.util.List;
12 import java.util.Map;
13
14 /**
15 * String related utility functions, which are used for logging and in toString() functions.
16 * <p>
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()
23 */
24 public class StringUtil {
25
26 /**
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
31 */
32 public static void replaceString( final StringBuilder aSb, final String aFrom, final String aTo ) {
33 int index = aSb.indexOf( aFrom );
34 while (index != -1) {
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 );
38 }
39 }
40
41 /**
42 * Appends any Object to the given StringBuilder.
43 * <p>
44 * It works with the following data types:
45 * <ul>
46 * <li> null
47 * <li> String
48 * <li> array of Object
49 * <li> array of any primitive type, like int[], boolean[], ...
50 * <li> list of Object
51 * </ul>
52 * @param aSb [in/out] the StringBuilder to extend
53 * @param aObject [in] object to add
54 */
55 public static void appendObject( final StringBuilder aSb, final Object aObject ) {
56 final Object o = aObject;
57 if ( o == null ) {
58 aSb.append("null");
59 }
60 else if ( o instanceof String ) {
61 aSb.append("\"");
62 aSb.append(o.toString());
63 aSb.append("\"");
64 }
65 else if ( o instanceof Object[] ) {
66 final Object[] oArray = (Object[])o;
67 aSb.append("[");
68 if ( oArray.length > 0 ) {
69 aSb.append(" ");
70 for ( int i = 0; i < oArray.length; i++ ) {
71 if ( i > 0 ) {
72 aSb.append(", ");
73 }
74 appendObject(aSb, oArray[i]);
75 }
76 aSb.append(" ");
77 }
78 aSb.append("]");
79 }
80 else if ( isPrimitiveArray( o ) ) {
81 appendObject( aSb, toObjectArray( o ) );
82 }
83 else if ( o instanceof List ) {
84 final List<?> oList = (List<?>)o;
85 aSb.append("[");
86 if ( oList.size() > 0 ) {
87 aSb.append(" ");
88 for ( int i = 0; i < oList.size(); i++ ) {
89 if ( i > 0 ) {
90 aSb.append(", ");
91 }
92 appendObject(aSb, oList.get(i));
93 }
94 aSb.append(" ");
95 }
96 aSb.append("]");
97 }
98 else if ( o instanceof Map ) {
99 final Map<?,?> oMap = (Map<?,?>)o;
100 aSb.append("[");
101 if ( oMap.size() > 0 ) {
102 aSb.append(" ");
103 int i = 0;
104 for (Object key : oMap.keySet()) {
105 if ( i++ > 0 ) {
106 aSb.append(", ");
107 }
108 appendObject(aSb, key);
109 aSb.append(": ");
110 appendObject(aSb, oMap.get( key ) );
111 }
112 aSb.append(" ");
113 }
114 aSb.append("]");
115 }
116 else {
117 aSb.append(o.toString());
118 }
119 }
120
121 /**
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
125 */
126 public static void indent( final StringBuilder aSb, final String aIndentString ) {
127 replaceString( aSb, "\n", "\n" + aIndentString );
128 }
129
130 /**
131 * @param aPrimitiveArrayCandidate the object to check
132 * @return true, if aPrimitiveArrayCandidate is a primitive array, like int[], boolean[], ...
133 * false otherwise
134 */
135 private static boolean isPrimitiveArray( final Object aPrimitiveArrayCandidate ) {
136 return aPrimitiveArrayCandidate != null &&
137 aPrimitiveArrayCandidate.getClass().isArray() &&
138 aPrimitiveArrayCandidate.getClass().getComponentType().isPrimitive();
139 }
140
141 /**
142 * Converts primitive array to Object array.
143 * <p>
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)
149 */
150 private static Object[] toObjectArray( final Object aPrimitiveArray ) {
151 if ( aPrimitiveArray instanceof Object[] ) {
152 return ( Object[] )aPrimitiveArray;
153 }
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 );
158 }
159 return outputArray;
160 }
161
162 }
This page took 0.051892 seconds and 5 git commands to generate.