Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0e64cb9fe1116c6fc4e2915724c76a502e995f28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
 * Copyright (c) 2008, 2009 Mia-Software.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Gabriel Barbier (Mia-Software) - initial API and implementation
 *    Nicolas Bros (Mia-Software)
 *    Gregoire Dupe (Mia-Software) - Bug 366804 - [Restructuring] Table widget upgrade
 *    Gregoire Dupe (Mia-Software) - Bug 373078 - API Cleaning
 *******************************************************************************/

package org.eclipse.emf.facet.util.core.internal.exported;


import org.eclipse.emf.facet.util.core.internal.Messages;

import com.ibm.icu.lang.UCharacter;

/**
 * @author Gabriel Barbier
 * @since 0.2
 */
// Copied from org.eclipse.emf.facet.infra.common.core.internal.utils.StringUtils
public final class StringUtils {
	public static final String ELLIPSIS = Messages.StringUtils_ellipsis;
	private static final int TRUNCATE_AFTER = 150;

	private StringUtils() {
		// prevent instantiation
	}

	public static String firstLetterToLowerCase(final String source) {
		String result;
		if (source.length() == 0) {
			result = source;
		} else if (source.length() == 1) {
			result = source.toLowerCase();
		} else {
			result = source.substring(0, 1).toLowerCase() + source.substring(1);
		}
		return result;
	}

	public static String firstLetterToUpperCase(final String source) {
		String result;
		if (source.length() == 0) {
			result = source;
		} else if (source.length() == 1) {
			result = source.toUpperCase();
		} else {
			result = source.substring(0, 1).toUpperCase() + source.substring(1);
		}
		return result;
	}

	/**
	 * Truncate the given String before the first newline or a maximum number of
	 * characters, whichever comes first. Adds an ellipsis ("...") if it was
	 * effectively truncated.
	 * 
	 * @param str
	 *            the string to truncate
	 * @return the part of the given string before the first newline
	 */
	public static String truncateBeforeNewline(final String str) {
		int endIndex = str.indexOf('\r');
		if (endIndex == -1) {
			endIndex = str.indexOf('\n');
		}
		if (endIndex != -1 && endIndex <= StringUtils.TRUNCATE_AFTER) {
			return str.substring(0, endIndex) + StringUtils.ELLIPSIS;
		}
		if (endIndex == -1) {
			endIndex = str.length();
		}

		if (endIndex > StringUtils.TRUNCATE_AFTER) {
			return str.substring(0, StringUtils.TRUNCATE_AFTER / 2) + StringUtils.ELLIPSIS
					+ str.substring(str.length() - StringUtils.TRUNCATE_AFTER / 2, endIndex);
		}
		return str;
	}

	/**
	 * Get a name suitable for a Java class from the given name. Capitalizes the
	 * first letter and each letter after a space, and removes spaces.
	 */
	public static String inferJavaClassName(final String name) {
		String upperName = StringUtils.firstLetterToUpperCase(name.trim());
		StringBuilder javaName = new StringBuilder();
		boolean space = false;
		for (int i = 0; i < upperName.length(); i++) {
			char c = upperName.charAt(i);
			if (c == ' ') {
				space = true;
			} else if (space) {
				javaName.append(UCharacter.toUpperCase(c));
				space = false;
			} else {
				javaName.append(c);
			}
		}
		return javaName.toString();
	}
}

Back to the top