|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2016 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * #L% |
| 30 | + */ |
| 31 | +package org.scijava.convert; |
| 32 | + |
| 33 | +import org.scijava.Priority; |
| 34 | +import org.scijava.plugin.Plugin; |
| 35 | +import org.scijava.util.ClassUtils; |
| 36 | +import org.scijava.util.ConversionUtils; |
| 37 | +import org.scijava.util.GenericUtils; |
| 38 | + |
| 39 | +/** |
| 40 | + * Minimal {@link Converter} implementation to do direct casting. |
| 41 | + * |
| 42 | + * @author Mark Hiner hinerm at gmail.com |
| 43 | + */ |
| 44 | +@Plugin(type = Converter.class, priority = Priority.FIRST_PRIORITY - 1) |
| 45 | +public class CastingConverter extends AbstractConverter<Object, Object> { |
| 46 | + |
| 47 | + @SuppressWarnings("deprecation") |
| 48 | + @Override |
| 49 | + public boolean canConvert(final Object src, final Class<?> dest) { |
| 50 | + return ClassUtils.canCast(src, dest); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public boolean canConvert(final Class<?> src, final Class<?> dest) { |
| 55 | + // OK if the existing object can be casted |
| 56 | + if (ConversionUtils.canCast(src, dest)) |
| 57 | + return true; |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + @Override |
| 64 | + public <T> T convert(final Object src, final Class<T> dest) { |
| 65 | + // NB: Regardless of whether the destination type is an array or |
| 66 | + // collection, |
| 67 | + // we still want to cast directly if doing so is possible. But note that |
| 68 | + // in |
| 69 | + // general, this check does not detect cases of incompatible generic |
| 70 | + // parameter types. If this limitation becomes a problem in the future |
| 71 | + // we |
| 72 | + // can extend the logic here to provide additional signatures of canCast |
| 73 | + // which operate on Types in general rather than only Classes. However, |
| 74 | + // the |
| 75 | + // logic could become complex very quickly in various subclassing cases, |
| 76 | + // generic parameters resolved vs. propagated, etc. |
| 77 | + final Class<?> c = GenericUtils.getClass(dest); |
| 78 | + return (T) ConversionUtils.cast(src, c); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Class<Object> getOutputType() { |
| 83 | + return Object.class; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Class<Object> getInputType() { |
| 88 | + return Object.class; |
| 89 | + } |
| 90 | +} |
0 commit comments