| 1 |
/* |
| 2 |
* Copyright (C) 2013 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.utils; |
| 18 |
|
| 19 |
import com.android.annotations.NonNull; |
| 20 |
import com.android.annotations.Nullable; |
| 21 |
import com.google.common.base.Splitter; |
| 22 |
import com.google.common.collect.ImmutableList; |
| 23 |
import com.google.common.collect.Lists; |
| 24 |
|
| 25 |
import java.util.Collection; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Locale; |
| 28 |
|
| 29 |
/** |
| 30 |
*/ |
| 31 |
public class StringHelper { |
| 32 |
|
| 33 |
@NonNull |
| 34 |
public static String capitalize(@NonNull String string) { |
| 35 |
StringBuilder sb = new StringBuilder(); |
| 36 |
sb.append(string.substring(0, 1).toUpperCase(Locale.US)).append(string.substring(1)); |
| 37 |
|
| 38 |
return sb.toString(); |
| 39 |
} |
| 40 |
|
| 41 |
@NonNull |
| 42 |
public static String combineAsCamelCase(@NonNull Iterable<String> stringList) { |
| 43 |
StringBuilder sb = new StringBuilder(); |
| 44 |
boolean first = true; |
| 45 |
for (String str : stringList) { |
| 46 |
if (first) { |
| 47 |
sb.append(str); |
| 48 |
first = false; |
| 49 |
} else { |
| 50 |
sb.append(StringHelper.capitalize(str)); |
| 51 |
} |
| 52 |
} |
| 53 |
return sb.toString(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns a list of Strings containing the objects passed in argument. |
| 58 |
* |
| 59 |
* If the objects are strings, they are directly added to the list. |
| 60 |
* If the objects are collections of strings, the strings are added. |
| 61 |
* For other objects, the result of their toString() is added. |
| 62 |
* @param objects the objects to add |
| 63 |
* @return the list of objects. |
| 64 |
*/ |
| 65 |
@NonNull |
| 66 |
public static List<String> toStrings(@NonNull Object... objects) { |
| 67 |
ImmutableList.Builder<String> builder = ImmutableList.builder(); |
| 68 |
for (Object path : objects) { |
| 69 |
if (path instanceof String) { |
| 70 |
builder.add((String) path); |
| 71 |
} else if (path instanceof Collection) { |
| 72 |
Collection pathCollection = (Collection) path; |
| 73 |
for (Object item : pathCollection) { |
| 74 |
if (item instanceof String) { |
| 75 |
builder.add((String) item); |
| 76 |
} else { |
| 77 |
builder.add(path.toString()); |
| 78 |
} |
| 79 |
} |
| 80 |
} else { |
| 81 |
builder.add(path.toString()); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return builder.build(); |
| 86 |
} |
| 87 |
|
| 88 |
public static void appendCamelCase(@NonNull StringBuilder sb, @Nullable String word) { |
| 89 |
if (word != null) { |
| 90 |
if (sb.length() == 0) { |
| 91 |
sb.append(word); |
| 92 |
} else { |
| 93 |
sb.append(StringHelper.capitalize(word)); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Split a single command line into individual commands with platform specific rules. |
| 100 |
* |
| 101 |
* @param commandLine the command line to be split |
| 102 |
* @return the list of individual commands |
| 103 |
*/ |
| 104 |
@NonNull |
| 105 |
public static List<String> splitCommandLine(@NonNull String commandLine) { |
| 106 |
|
| 107 |
if (System.getProperty("os.name").startsWith("Windows")) |
| 108 |
return StringHelperWindows.splitCommandLine(commandLine); |
| 109 |
else |
| 110 |
return StringHelperPOSIX.splitCommandLine(commandLine); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Quote and join a list of tokens with platform specific rules. |
| 115 |
* |
| 116 |
* @param tokens the token to be quoted and joined |
| 117 |
* @return the string |
| 118 |
*/ |
| 119 |
@NonNull |
| 120 |
public static String quoteAndJoinTokens(@NonNull List<String> tokens) { |
| 121 |
if (System.getProperty("os.name").startsWith("Windows")) |
| 122 |
return StringHelperWindows.quoteAndJoinTokens(tokens); |
| 123 |
else |
| 124 |
return StringHelperPOSIX.quoteAndJoinTokens(tokens); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Tokenize a string with platform specific rules. |
| 129 |
* |
| 130 |
* @param string the string to be tokenized |
| 131 |
* @return the list of tokens |
| 132 |
*/ |
| 133 |
@NonNull |
| 134 |
public static List<String> tokenizeString(@NonNull String string) { |
| 135 |
if (System.getProperty("os.name").startsWith("Windows")) |
| 136 |
return StringHelperWindows.tokenizeString(string); |
| 137 |
else |
| 138 |
return StringHelperPOSIX.tokenizeString(string); |
| 139 |
} |
| 140 |
} |