All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.projectmonkey.object.mapper.construction.converter.StringConverter Maven / Gradle / Ivy

Go to download

Object mapping implementation written as an alternative to modelmapper which is able to support inheritance, handles flattening / expanding in a precise way, and is extensible / configurable

The newest version!
package net.projectmonkey.object.mapper.construction.converter;

import net.projectmonkey.object.mapper.construction.PopulationContext;
import net.projectmonkey.object.mapper.util.Types;
import org.apache.commons.lang3.ArrayUtils;

/*
 *
 *  * Copyright 2012 the original author or authors.
 *  *
 *  * Licensed 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.
 *
 */

/**
 * Converter which takes the source object and converts it into a String.
 * char[], Character[], byte[] and Byte[] are handled as special cases.
 * For all other object
 * types the result is gained by calling toString on the source object.
 * 

* Adapted from ModelMapper (modelmapper.org) * * @author Jonathan Halterman * @author Andy Moody */ public final class StringConverter implements Converter { public static final StringConverter INSTANCE = new StringConverter(); private StringConverter() {} @Override public String convert(final PopulationContext context) { String toReturn = null; Object source = context.getSource(); if(source != null) { Class sourceType = source.getClass(); if(Types.isArrayOfType(sourceType, Character.class, Character.TYPE)) { char[] chars = Types.isArrayOfType(sourceType, Character.class) ? ArrayUtils.toPrimitive((Character[]) source): (char[]) source; toReturn = new String(chars); } else if(Types.isArrayOfType(sourceType, Byte.class, Byte.TYPE)) { byte[] bytes = Types.isArrayOfType(sourceType, Byte.class) ? ArrayUtils.toPrimitive((Byte[]) source): (byte[]) source; toReturn = new String(bytes); } else { toReturn = source.toString(); } } return toReturn; } @Override public boolean canConvert(final PopulationContext context) { return String.class.equals(context.getDestinationType()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy