Java Array to String toStringArray(Dictionary props, String key, String[] defaultArray)

Here you can find the source of toStringArray(Dictionary props, String key, String[] defaultArray)

Description

to String Array

License

Apache License

Declaration

public static String[] toStringArray(Dictionary props, String key, String[] defaultArray) 

Method Source Code


//package com.java2s;
/*/*from  w w  w . j a v a2 s  . c o m*/
 * 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.
 */

import java.util.*;

public class Main {
    public static String[] toStringArray(Dictionary props, String key, String[] defaultArray) {
        if (props == null || key == null) {
            return defaultArray;
        }
        final Object propValue = props.get(key);
        if (propValue == null) {
            // no value at all
            return defaultArray;

        } else if (propValue instanceof String) {
            // single string
            return new String[] { (String) propValue };

        } else if (propValue instanceof String[]) {
            // String[]
            return (String[]) propValue;

        } else if (propValue.getClass().isArray()) {
            // other array
            Object[] valueArray = (Object[]) propValue;
            List values = new ArrayList(valueArray.length);
            for (int i = 0; i < valueArray.length; i++) {
                final Object value = valueArray[i];
                if (value != null) {
                    values.add(value.toString());
                }
            }
            return (String[]) values.toArray(new String[values.size()]);

        } else if (propValue instanceof Collection) {
            // collection
            Collection valueCollection = (Collection) propValue;
            List valueList = new ArrayList(valueCollection.size());
            final Iterator i = valueCollection.iterator();
            while (i.hasNext()) {
                final Object value = i.next();
                if (value != null) {
                    valueList.add(value.toString());
                }
            }
            return (String[]) valueList.toArray(new String[valueList.size()]);
        }

        return defaultArray;
    }

    public static String toString(Dictionary props, String key, String defaultValue) {
        Object propValue = toObject(props, key);
        return (propValue != null) ? propValue.toString() : defaultValue;
    }

    public static Object toObject(Dictionary props, String key) {
        if (props == null || key == null) {
            return null;
        }
        final Object propValue = props.get(key);
        if (propValue == null) {
            return null;
        } else if (propValue.getClass().isArray()) {
            Object[] prop = (Object[]) propValue;
            return prop.length > 0 ? prop[0] : null;
        } else if (propValue instanceof Collection) {
            Collection prop = (Collection) propValue;
            return prop.isEmpty() ? null : prop.iterator().next();
        } else {
            return propValue;
        }
    }
}

Related

  1. toString(String[] paramNames, Object[] params)
  2. toString(T[] array)
  3. toString(T[] array)
  4. toString(T[] array, String prefix, String sep, String suffix)
  5. toStringArray(Collection strings)
  6. toStringArray(final E[] array)
  7. toStringArray(int[] nums)
  8. toStringArray(Object value)
  9. toStringKeyValuePairs(Object[] keys, Object[] values, int size, StringBuilder dstOptional)