<commit_before>package org.csstudio.ui.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;

import static org.csstudio.ui.util.ReflectUtil.*;

/**
 * Utilities for using adapters, particularly against selections.
 *
 *  @author <NAME>
 *  @author <NAME>
 */
public class AdapterUtil
{
    
    /**
     * Returns all class names that an object of that class can be
     * converted to.
     * 
     * @param clazz a class
     * @return all the class names with registered adapterFactories
     */
    public static String[] getAdaptableTypes(Class<?> clazz)
    {
    	if (Platform.isRunning()){
    		// Check for adapters in platform
    	    return Platform.getAdapterManager().computeAdapterTypes(clazz);
    	}
    	
    	// No types found
    	return new String[0];
    }
    
    /**
     * Returns the current selection converted to an array of the desired type,
     * or to an empty array if not possible.
     * 
     * @param <T> requested type
     * @param selection a selection
     * @param clazz the desired type
     * @return an array of the desired type
     */
    public static <T> T[] convert(ISelection selection, Class<T> clazz) {
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
			
			@SuppressWarnings("unchecked")
			T[] result = (T[]) convert(strucSelection.toArray(), ReflectUtil.toArrayClass(clazz.getName()));
			return result;
		} else {
			@SuppressWarnings("unchecked")
			T[] result = (T[]) Array.newInstance(clazz, 0);
			return result;
		}
    }
    
    /** 
     * Adapts an object to the desired type. This method, on top of
     * the standard adapter facility, adds support for arrays. If any of the
     * arguments represents an array, it will
     * use object to object adaptation to create it.
     * 
     * @param obj Object to adapt
     * @param targetClass Desired class name
     * @return Object that matches the <code>targetClass</code> or <code>null</code>
     */
    public static Object convert(Object obj, String targetClass) {

    	// If object is of the right class, do not adapt
    	if (isInstance(obj, targetClass)) {
    		return obj;
    	}
    	
    	// If object is of the right class, but the target is an array,
    	// return a single element array
    	if (isArray(targetClass) && isInstance(obj, getComponentType(targetClass))) {
    		Object[] result = (Object[]) Array.newInstance(obj.getClass(), 1);
    		result[0] = obj;<commit_msg>..ui.util: always return an empty array instead of null when converting the selection.<commit_after>package org.csstudio.ui.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;

import static org.csstudio.ui.util.ReflectUtil.*;

/**
 * Utilities for using adapters, particularly against selections.
 *
 *  @author <NAME>
 *  @author <NAME>
 */
public class AdapterUtil
{
    
    /**
     * Returns all class names that an object of that class can be
     * converted to.
     * 
     * @param clazz a class
     * @return all the class names with registered adapterFactories
     */
    public static String[] getAdaptableTypes(Class<?> clazz)
    {
    	if (Platform.isRunning()){
    		// Check for adapters in platform
    	    return Platform.getAdapterManager().computeAdapterTypes(clazz);
    	}
    	
    	// No types found
    	return new String[0];
    }
    
    /**
     * Returns the current selection converted to an array of the desired type,
     * or to an empty array if not possible.
     * 
     * @param <T> requested type
     * @param selection a selection
     * @param clazz the desired type
     * @return an array of the desired type
     */
    public static <T> T[] convert(ISelection selection, Class<T> clazz) {
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
			
			@SuppressWarnings("unchecked")
			T[] result = (T[]) convert(strucSelection.toArray(), ReflectUtil.toArrayClass(clazz.getName()));
			if (result != null)
				return result;
		}
		@SuppressWarnings("unchecked")
		T[] result = (T[]) Array.newInstance(clazz, 0);
		return result;
    }
    
    /** 
     * Adapts an object to the desired type. This method, on top of
     * the standard adapter facility, adds support for arrays. If any of the
     * arguments represents an array, it will
     * use object to object adaptation to create it.
     * 
     * @param obj Object to adapt
     * @param targetClass Desired class name
     * @return Object that matches the <code>targetClass</code> or <code>null</code>
     */
    public static Object convert(Object obj, String targetClass) {

    	// If object is of the right class, do not adapt
    	if (isInstance(obj, targetClass)) {
    		return obj;
    	}
    	
    	// If object is of the right class, but the target is an array,
    	// return a single element array
    	if (isArray(targetClass) && isInstance(obj, getComponentType(targetClass))) {
    		Object[] result = (Object[]) Array.newInstance(obj.getClass(), 1);
    		result[0] = obj;
{'fraction_non_alphanumeric': 0.10056579667822596, 'fraction_numerical': 0.0014601204599379448, 'mean_word_length': 2.9396117900790797, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7323281', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1592, 'n_words': 531}