General-purpose StringConverter class that utilizes generics in Java8

what's this

Operating environment

Contents of the class

StringConverter.java


	private static class StringConverter<T> {

		private static final List<StringConverter<?>> converters = Arrays.asList(
				new StringConverter<>(String.class, String::toString),
				new StringConverter<>(BigDecimal.class, BigDecimal::toPlainString),
				new StringConverter<>(Date.class, Date::toString)
		);

		private final Class<T> clazz;
		private final Function<T, String> converter;

		static String convert(final Object object) {
			if(object == null){
				return null;
			}
			return converters.stream().filter(c -> c.isApplicable(object)).map(c -> c.doConvert(object)).findFirst()
					.orElseThrow(() -> new IllegalArgumentException("Unexpected class:" + object.getClass().getName()));
		}

		private StringConverter(final Class<T> clazz, final Function<T, String> converter) {
			this.clazz = clazz;
			this.converter = converter;
		}

		private boolean isApplicable(final Object object) {
			return clazz.isInstance(object);
		}

		private String doConvert(final Object object) {
			return converter.apply(clazz.cast(object));
		}

	}

How to use

I wrote it because I needed it, but I forgot what kind of situation it was because there was too much space.

String hoge = StringConverter<The class you want to convert(Please define it in converters)>.convert(obj);

After that, I didn't have a chance to use the generic type, so I wanted to use it.

I'm sorry if I made a lot of mistakes.

Recommended Posts

General-purpose StringConverter class that utilizes generics in Java8
Write a class that can be ordered in Java
[MQTT / Java] Implemented a class that does MQTT Pub / Sub in Java
Java version notation that changes in Java 10
StringBuffer and StringBuilder Class in Java
What is a class in Java language (3 /?)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Creating a matrix class in Java Part 1
[Java] Generics
[Java] Implement a function that uses a class implemented in the Builder pattern
[Java] Where is the implementation class of annotation that exists in Bean Validation?
Java that outputs vmg format file in eml format
GetInstance () from a @Singleton class in Groovy from Java
Java method call from RPG (method call in own class)
How to get Class from Element in Java
A bat file that uses Java in windows
Java class methods
[Java] Class inheritance
Java Generics Summary
java Scanner class
Partization in Java
Java HashMap class
[Java] Generics sample
Java Generics (Notes)
java (abstract class)
Changes in Java 11
Class in Ruby
Rock-paper-scissors in Java
[Java] Nested class
About Java class
Java General-purpose method that retrieves only the differences between the properties of objects of the same class
[java] abstract class
Pi in Java
[Java] Object class
FizzBuzz in Java
A quick review of Java learned in class part4
Browse class objects in Kotlin (instead of Java class name.class)
Write a class in Kotlin and call it in Java
[.net, Java integration] Nger that executes .jar in C # .net
This and that for editing ini in Java. : inieditor-java
A quick review of Java learned in class part3
A quick review of Java learned in class part2
Use of Abstract Class and Interface properly in Java
Summarize the additional elements of the Optional class in Java 9
The story that .java is also built in Unity 2018
A library that realizes multi-line strings in Java multiline-string