|
Glassfish example source code file (StringHelper.java)
The Glassfish StringHelper.java source code/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can * obtain a copy of the License at * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html * or packager/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at packager/legal/LICENSE.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * exception as provided by Oracle in the GPL Version 2 section of the License * file that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" * * Contributor(s): * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. */ /* * StringHelper.java * * Created on March 3, 2000 */ package com.sun.jdo.spi.persistence.utility; import java.util.*; /** * NOTE: These utilities have been moved from another (more specific * package's) utility class so that more classes can have access to them. * There can be some refactoring work to combine these with some of the * methods in the StringScanner class. * */ public class StringHelper { /** * constants for escape */ private static final char BACKSLASH = '\\'; private static final char QUOTE = '"'; /** Convert an array of objects into a separated string. This method * assumes there is no instance of the separator in the strings of list. * @param list The list of objects to be expanded. * @param beginIndex The index of the first element in the list to be used. * @param endIndex The index of the last element in the list to be used. * @param separator The separator to be used between strings. * @return a string representing the expanded list. */ public static String arrayToSeparatedList (List list, int beginIndex, int endIndex, String separator) { StringBuffer result = new StringBuffer(); if (list != null) { int i, count = (endIndex + 1); if ((count > beginIndex) && (list.size() >= count)) result.append(list.get(beginIndex)); for (i = beginIndex + 1; i < count; i++) result.append(separator + list.get(i)); } return result.toString(); } /** Convert an array of objects into a separated string using the default * separator. This method assumes there is no instance of the separator * in the strings of list. * @param list The list of objects to be expanded. * @param beginIndex The index of the first element in the list to be used. * @param endIndex The index of the last element in the list to be used. * @return a string representing the expanded list. */ public static String arrayToSeparatedList (List list, int beginIndex, int endIndex) { return arrayToSeparatedList(list, beginIndex, endIndex, ","); // NOI18N } /** Convert an array of objects into a separated string using the specified * separator and the entire array. This method assumes there is no * instance of the separator in the strings of list. * @param list The list of objects to be expanded. * @param separator The separator to be used between strings. * @return a string representing the expanded list. */ public static String arrayToSeparatedList (List list, String separator) { return arrayToSeparatedList(list, 0, ((list != null) ? (list.size() - 1) : 0), separator); } /** Convert an array of objects into a separated string using the default * separator and the entire array. This method assumes there is no * instance of the separator in the strings of list. * @param list The list of objects to be expanded. * @return a string representing the expanded list. */ public static String arrayToSeparatedList (List list) { return arrayToSeparatedList(list, 0, ((list != null) ? (list.size() - 1) : 0)); } /** Convert a separated string to an array of strings * @param list The string representing the list of objects. * @param separator The separator to be used to tokenize strings. * @return an array representing the tokenized list. */ public static List separatedListToArray (String list, String separator) { ArrayList result = new ArrayList(); if (list != null) { StringTokenizer st = new StringTokenizer(list, separator); int i, size = st.countTokens(); for (i = 0; i < size; i++) result.add(st.nextToken()); } return result; } /** Convert a separated string to an array of strings using the default * separator. * @param list The string representing the list of objects. * @return an array representing the tokenized list. */ public static List separatedListToArray (String list) { return separatedListToArray(list, ","); // NOI18N } /** Convert an array of int values into a separated string. * @param intArray The array of int values to be expanded. * @param separator The separator to be used between strings. * @return a string representing the expanded array. */ public static String intArrayToSeparatedList(int[] intArray, String separator) { return intArrayToSeparatedList(intArray, 0, ((intArray != null) ? (intArray.length - 1) : 0), separator); } /** Convert an array of int values into a separated string. * @param intArray The array of int values to be expanded. * @param beginIndex The index of the first element in the array to be used. * @param endIndex The index of the last element in the array to be used. * @param separator The separator to be used between strings. * @return a string representing the expanded array. */ public static String intArrayToSeparatedList(int[] intArray, int beginIndex, int endIndex, String separator) { StringBuffer result = new StringBuffer(); if (intArray != null) { int count = (endIndex + 1); if ((count > beginIndex) && (intArray.length >= count)) result.append(intArray[beginIndex]); for (int i = beginIndex + 1; i < count; i++) { result.append(separator); result.append(intArray[i]); } } return result.toString(); } /** Checks if a string is null or empty. * @return <code>true if the string is null or empty after trim, * <code>false otherwirse. */ public static boolean isEmpty (String aString) { return ((aString == null) || (aString.trim().length() == 0)); } /** Gets a version of the specified string with the first letter * capitalized. This can be used to convert a field name to get and set * method names. * @param aString the string to be capitalized * @return a capitalized for the specified string */ public static String getCapitalizedString (String aString) { if (isEmpty(aString)) return aString; return Character.toUpperCase(aString.charAt(0)) + aString.substring(1); } /** Replaces the first occurence of <code>oldString in Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish StringHelper.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.