|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * Converting a value to the type something else wants. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2026 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.convert3; |
| 31 | + |
| 32 | +import java.lang.reflect.Array; |
| 33 | +import java.lang.reflect.Type; |
| 34 | +import java.util.Collection; |
| 35 | + |
| 36 | +import org.scijava.common3.Types; |
| 37 | +import org.scijava.priority.Priority; |
| 38 | + |
| 39 | +/** |
| 40 | + * Converts to an array, converting each element on the way. |
| 41 | + * <p> |
| 42 | + * A collection becomes an array, an array becomes an array of another |
| 43 | + * component type, and a lone value becomes an array of one - which is what |
| 44 | + * makes {@code "3"} usable where {@code int[]} is wanted, a thing scripts and |
| 45 | + * command lines do constantly. |
| 46 | + * </p> |
| 47 | + * |
| 48 | + * @author Curtis Rueden |
| 49 | + */ |
| 50 | +public class ArrayConverter implements Converter<Object, Object> { |
| 51 | + |
| 52 | + @Override |
| 53 | + public Type sourceType() { |
| 54 | + return Object.class; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Type destType() { |
| 59 | + return Object[].class; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean supports(final Object source, final Type dest) { |
| 64 | + return source != null && Types.component(dest) != null; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Object convert(final Object source, final Type dest) { |
| 69 | + final Type componentType = Types.component(dest); |
| 70 | + final Class<?> component = Types.raw(componentType); |
| 71 | + if (component == null) return null; |
| 72 | + |
| 73 | + final Object[] elements = elements(source); |
| 74 | + final Object array = Array.newInstance(component, elements.length); |
| 75 | + final Converters converters = Converters.get(); |
| 76 | + for (int i = 0; i < elements.length; i++) { |
| 77 | + // NB: each element is converted in turn, so a List<String> reaches an |
| 78 | + // int[] without anything knowing about that pair specifically. |
| 79 | + final Object element = converters.tryConvert(elements[i], componentType) |
| 80 | + .orElse(null); |
| 81 | + if (element == null && elements[i] != null) return null; |
| 82 | + Array.set(array, i, element); |
| 83 | + } |
| 84 | + return array; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public double priority() { |
| 89 | + return Priority.HIGH; |
| 90 | + } |
| 91 | + |
| 92 | + // -- Helper methods -- |
| 93 | + |
| 94 | + private static Object[] elements(final Object source) { |
| 95 | + if (source instanceof Collection) { |
| 96 | + return ((Collection<?>) source).toArray(); |
| 97 | + } |
| 98 | + if (source.getClass().isArray()) { |
| 99 | + final int length = Array.getLength(source); |
| 100 | + final Object[] elements = new Object[length]; |
| 101 | + for (int i = 0; i < length; i++) |
| 102 | + elements[i] = Array.get(source, i); |
| 103 | + return elements; |
| 104 | + } |
| 105 | + return new Object[] { source }; |
| 106 | + } |
| 107 | +} |
0 commit comments