001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.commons.lang;
021
022import java.lang.reflect.Array;
023import java.lang.reflect.Constructor;
024import java.lang.reflect.InvocationTargetException;
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.Collection;
028import java.util.List;
029
030import org.apache.isis.core.commons.exceptions.IsisException;
031
032public final class ArrayExtensions {
033
034    private ArrayExtensions() {
035    }
036
037    static Object[] convertPrimitiveToObjectArray(final Object extendee, final Class<?> arrayType) {
038        Object[] convertedArray;
039        try {
040            final Class<?> wrapperClass = ClassExtensions.asWrapped(arrayType);
041            final Constructor<?> constructor = wrapperClass.getConstructor(new Class[] { String.class });
042            final int len = Array.getLength(extendee);
043            convertedArray = (Object[]) Array.newInstance(wrapperClass, len);
044            for (int i = 0; i < len; i++) {
045                convertedArray[i] = constructor.newInstance(new Object[] { Array.get(extendee, i).toString() });
046            }
047        } catch (final NoSuchMethodException e) {
048            throw new IsisException(e);
049        } catch (final ArrayIndexOutOfBoundsException e) {
050            throw new IsisException(e);
051        } catch (final IllegalArgumentException e) {
052            throw new IsisException(e);
053        } catch (final InstantiationException e) {
054            throw new IsisException(e);
055        } catch (final IllegalAccessException e) {
056            throw new IsisException(e);
057        } catch (final InvocationTargetException e) {
058            throw new IsisException(e);
059        }
060        return convertedArray;
061    }
062
063    public static Object[] asCharToCharacterArray(final Object extendee) {
064        final char[] original = (char[]) extendee;
065        final int len = original.length;
066        final Character[] converted = new Character[len];
067        for (int i = 0; i < converted.length; i++) {
068            converted[i] = Character.valueOf(original[i]);
069        }
070        return converted;
071    }
072
073    public static <T> T[] combine(final T[]... arrays) {
074        final List<T> combinedList = new ArrayList<T>();
075        for (final T[] array : arrays) {
076            for (final T t : array) {
077                combinedList.add(t);
078            }
079        }
080        return combinedList.toArray(arrays[0]); // using 1st element of arrays
081                                                // to specify the type
082    }
083
084    public static String[] append(final String[] extendee, final String... moreArgs) {
085        final ArrayList<String> argList = new ArrayList<String>();
086        argList.addAll(Arrays.asList(extendee));
087        argList.addAll(Arrays.asList(moreArgs));
088        return argList.toArray(new String[] {});
089    }
090
091    public static List<String> mergeToList(final String[] extendee, final String[] array2) {
092        final List<String> prefixes = new ArrayList<String>();
093        ArrayExtensions.addNoDuplicates(extendee, prefixes);
094        ArrayExtensions.addNoDuplicates(array2, prefixes);
095        return prefixes;
096    }
097
098    static void addNoDuplicates(final String[] array, final List<String> list) {
099        for (int i = 0; i < array.length; i++) {
100            if (!list.contains(array[i])) {
101                list.add(array[i]);
102            }
103        }
104    }
105
106    public static List<Object> asListFlattened(final Object[] objectArray) {
107        final List<Object> list = new ArrayList<Object>();
108        for (final Object element : objectArray) {
109            if (Collection.class.isAssignableFrom(element.getClass())) {
110                @SuppressWarnings("rawtypes")
111                final Collection collection = (Collection) element;
112                list.addAll(asListFlattened(collection.toArray()));
113            } else {
114                list.add(element);
115            }
116        }
117        return list;
118    }
119
120    public static <T> T coalesce(final T... strings) {
121        for (final T str : strings) {
122            if (str != null) {
123                return str;
124            }
125        }
126        return null;
127    }
128
129    public static String commaSeparatedClassNames(final List<Object> objects) {
130        final StringBuilder buf = new StringBuilder();
131        int i = 0;
132        for (final Object object : objects) {
133            if (i++ > 0) {
134                buf.append(',');
135            }
136            buf.append(object.getClass().getName());
137        }
138        return buf.toString();
139    }
140
141    public static String asSemicolonDelimitedStr(final List<String> list) {
142        final StringBuffer buf = new StringBuffer();
143        for (final String message : list) {
144            if (list.size() > 1) {
145                buf.append("; ");
146            }
147            buf.append(message);
148        }
149        return buf.toString();
150    }
151
152    public static List<Class<?>> toClasses(final List<Object> objectList) {
153        final List<Class<?>> classList = new ArrayList<Class<?>>();
154        for (final Object service : objectList) {
155            classList.add(service.getClass());
156        }
157        return classList;
158    }
159
160
161}