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

com.bluetrainsoftware.common.config.MapCoercion Maven / Gradle / Ivy

Go to download

Extends the functionality of Spring Boot and Sticky Code and provides extra support for Kubernetes style mounted volumes.

There is a newer version: 2.2
Show newest version
package com.bluetrainsoftware.common.config;

import net.stickycode.coercion.*;

import javax.inject.Inject;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * The Sticky one doesn't trim the names
 * 

* author: Richard Vowles - http://gplus.to/RichardVowles */ public class MapCoercion extends AbstractNoDefaultCoercion> { @Inject CoercionFinder finder; @Override public Map coerce(CoercionTarget type, String value) { if (value.length() == 0) return Collections.emptyMap(); CoercionTarget[] typeArguments = type.getComponentCoercionTypes(); assert typeArguments.length == 2 : "Maps should have two type arguments"; Coercion keyCoercion = findComponentCoercion(typeArguments[0]); Coercion valueCoercion = findComponentCoercion(typeArguments[1]); Map map = new HashMap(); for (String string : new StringSpliterable(value)) { String[] s = string.split("="); map.put( keyCoercion.coerce(typeArguments[0], s[0].trim()), valueCoercion.coerce(typeArguments[1], s[1].trim())); } return Collections.unmodifiableMap(map); } private Coercion findComponentCoercion(CoercionTarget target) { return finder.find(target); } @Override public boolean isApplicableTo(CoercionTarget target) { if (!Map.class.isAssignableFrom(target.getType())) return false; return true; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy