/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pluto.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Static class hosting a couple of utility methods around strings.
 */
public class StringUtils {

	// Private Constructor -----------------------------------------------------

	/**
	 * Private constructor that prevents external instantiation.
	 */
	private StringUtils() {
		// Do nothing.
	}


	// Static Utility Methods --------------------------------------------------

    /**
     * TODO: can't we just use String[].clone()?
     * @param source
     * @return
     */
    public static String[] copy(String[] source) {
        if (source == null) {
            return null;
        }
        int length = source.length;
        String[] result = new String[length];
        System.arraycopy(source, 0, result, 0, length);
        return result;
    }

    /**
     * Deep-clones a parameter map. The key is the parameter name as a String
     * instance, while the value is a String array (String[]) instance.
     * @param parameters  the parameter map to deep-clone.
     * @return the deep-cloned parameter map.
     */
    public static Map copyParameters(Map parameters) {
        Map result = new HashMap(parameters);
        for (Iterator it = result.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            if (!(entry.getKey() instanceof String)) {
                throw new IllegalArgumentException("Parameter map keys "
                		+ "must not be null and of type java.lang.String.");
            }
            try {
                entry.setValue(copy((String[]) entry.getValue()));
            } catch (ClassCastException ex) {
                throw new IllegalArgumentException("Parameter map values "
                		+ "must not be null and of type java.lang.String[].");
            }
        }
        return result;
    }

    /**
     * Strips the specified mime type by removing the character encoding
     * specified at the end of the mime type (all characters after the ';').
     * The stripped mime type is trimmed string which contains no white
     * spaces at the beginning and the end.
     * @param mimeType  the mime type to strip.
     * @return the stripped mime type.
     */
    public static String getMimeTypeWithoutEncoding(String mimeType) {
        int index = mimeType.indexOf(';');
        String strippedType = null;
        if (index == -1) {
            strippedType = mimeType;
        } else {
            strippedType = mimeType.substring(0, index);
        }
        return strippedType.trim();
    }
}

@@ -858,35 +858,8 @@
 ap;%0A
-import java.util.Iterator;%0A
 impo
@@ -876,16 +876,16 @@
 il.Map;%0A
+
 %0A/**%0A * 
@@ -1350,16 +1350,33 @@
  @return
+ The copied array
 %0A     */
@@ -1952,16 +1952,34 @@
 atic Map
+%3CString, String%5B%5D%3E
  copyPar
@@ -1989,16 +1989,34 @@
 ters(Map
+%3CString, String%5B%5D%3E
  paramet
@@ -2030,19 +2030,43 @@
         
-Map
+final Map%3CString, String%5B%5D%3E
  result 
@@ -2078,16 +2078,34 @@
  HashMap
+%3CString, String%5B%5D%3E
 (paramet
@@ -2125,359 +2125,73 @@
  for
- (Iterator it = result.entrySet().iterator(); it.hasNext();) %7B%0A            Map.Entry entry = (Map.Entry) it.next();%0A            if (!(entry.getKey() instanceof String)) %7B%0A                throw new IllegalArgumentException(%22Parameter map keys %22%0A                %09%09+ %22must not be null and of type java.lang.String.%22);%0A            %7D%0A            try %7B%0A    
+( final Map.Entry%3CString, String%5B%5D%3E entry : result.entrySet()) %7B%0A
     
@@ -2218,27 +2218,16 @@
 ue(copy(
-(String%5B%5D) 
 entry.ge
@@ -2242,216 +2242,8 @@
 ));%0A
-            %7D catch (ClassCastException ex) %7B%0A                throw new IllegalArgumentException(%22Parameter map values %22%0A                %09%09+ %22must not be null and of type java.lang.String%5B%5D.%22);%0A            %7D%0A
     

Use generics in string utils.