de.vandermeer.skb.interfaces.transformers.Object_To_StrBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of skb-interfaces Show documentation
Show all versions of skb-interfaces Show documentation
Set of interfaces used by other SKB projects.
/* Copyright 2016 Sven van der Meer
*
* 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.
*/
package de.vandermeer.skb.interfaces.transformers;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.text.StrBuilder;
import org.stringtemplate.v4.ST;
import de.vandermeer.skb.interfaces.render.DoesRender;
import de.vandermeer.skb.interfaces.render.DoesRenderToWidth;
import de.vandermeer.skb.interfaces.render.HasText;
import de.vandermeer.skb.interfaces.render.HasTextCluster;
import de.vandermeer.skb.interfaces.render.RendersToCluster;
import de.vandermeer.skb.interfaces.render.RendersToClusterWidth;
/**
* A null-safe transformer that takes an object and tries return a {@link StrBuilder} using various strategies.
*
* The object is processed as follows:
*
* - test object for being null, throw null pointer exception if that's the case
* - test if object implements {@link HasText}, take the text
* - test if object implements {@link HasTextCluster}, take the cluster and add each element to the text
* - test if object is an {@link ST}, render and take rendered text
* - test if object implements {@link DoesRender}, take the rendered text
* - test if object implements {@link DoesRenderToWidth}, take the rendered text
* - test if object implements {@link RendersToCluster}, take the rendered cluster and add as text
* - test if object implements {@link RendersToClusterWidth}, take the rendered cluster and add as text
* - test if object is an {@link Iterator}, iterate and call this method for each member
* - test if object is an {@link Iterable}, iterate and call this method for each member
* - test if object is an array of something, iterate the array and call this method for each member
* - use the object as is (which will take a string if it is a string, use the `toString()` method otherwise).
*
* Null objects in clusters are silently ignored.
* Blank strings are processed like any other string (they do not impact the text anyway).
*
* The method is recursive for iterators, iterables, and arrays.
* Care needs to be taken that the provided clusters do not lead to endless loops.
*
* @author Sven van der Meer <[email protected]>
* @version v0.0.1 build 170404 (04-Apr-17) for Java 1.8
* @since v0.0.1
*/
public interface Object_To_StrBuilder extends IsTransformer