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

br.com.m4rc310.gql.mappers.custom.MCaseMapper Maven / Gradle / Ivy

The newest version!
package br.com.m4rc310.gql.mappers.custom;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.AnnotatedType;

import br.com.m4rc310.gql.mappers.MGraphQLScalarType;
import br.com.m4rc310.gql.mappers.annotations.MCase;
import br.com.m4rc310.gql.mappers.annotations.MMapper;
import graphql.schema.Coercing;
import graphql.schema.GraphQLScalarType;

/**
 * The Class MCaseMapper.
 *
 * @author marcelo
 * @version $Id: $Id
 */
@MMapper
public class MCaseMapper extends MGraphQLScalarType {

	/**
	 * {@inheritDoc}
	 *
	 * Inits the.
	 */
	@Override
	public GraphQLScalarType init(AnnotatedElement element, AnnotatedType type, MCase annotation) {
		
		String message = getString("Change case of the text (Case -> {0})", annotation.value().toString());

		Coercing coercing = getCoercing(String.class, from -> changeCase(from, annotation),
				to -> changeCase(to, annotation));

		return get(String.format("Case%s",  annotation.value()), message, coercing);
	}

	/**
	 * Change case.
	 *
	 * @param value      the value
	 * @param annotation the annotation
	 * @return the string
	 */
	private String changeCase(String value, MCase annotation) {
		
		switch (annotation.value()) {
			case UPPER:
				return value.toUpperCase();
			case LOWER:
				return value.toLowerCase();
			default:
				return value;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy