de.vandermeer.asciitable.commons.ArrayTransformations Maven / Gradle / Ivy
/* Copyright 2014 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.asciitable.commons;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.StrBuilder;
import org.apache.commons.lang3.text.StrTokenizer;
import org.apache.commons.lang3.text.WordUtils;
/**
* Collections array transformation methods.
*
* @author Sven van der Meer <[email protected]>
* @version v1.0.0 build 160319 (19-Mar-16) for Java 1.7
* @since v0.0.1
*/
public abstract class ArrayTransformations {
/**
* Takes a 2 dimensional array and returns a string representation in table form.
* @param type of the input array
* @param ar the array to be transformed
* @return a string representation of the array
*/
public static final StrBuilder ARRAY_TO_STRING(T[][] ar){
StrBuilder ret = new StrBuilder(50);
for(int row=0; row
* row 1: a1, b1, c1
* row 2: a2, b2, c2
* row 3: a3, b3, c3
*
*
* The transformed table will be
* * row 1: a1, a2, a3 * row 2: b1, b2, b3 * row 3: c1, c2, c3 ** * @param ar input array which will be flipped * @return flipped array, null if input was null */ public static final String[][] FLIP_ARRAY(String[][] ar){ if(ar==null){ return null; } String[][] ret=new String[ar[0].length][ar.length]; for(int i=0; i
{@code String: "paragraph 1\n\nparagraph 2" Array: {paragraph 1,,paragraph 2} * }* * @param content the content to process * @return null if content was null, empty array if content was an empty string, string array with lines otherwise */ public static final String[] PROCESS_CONTENT(Object content){ if(content==null || content.toString()==null){ return null; } if("".equals(content)){ return new String[]{""}; } String lfRep = StringUtils.replacePattern(content.toString(), "\\r\\n|\\r|\\n", "
"); lfRep = StringUtils.replace(lfRep, "
", "
"); StrTokenizer tok = new StrTokenizer(lfRep, "
").setIgnoreEmptyTokens(false); return tok.getTokenArray(); } /** * Takes an content array (of strings) and returns a string array with wrapped lines of max length using {@link #WRAP_LINES(int, Object)}. * The wrapping is done using StringUtils and WordUtils so that words are not broken into characters. * @param length max length of a string in the returned array * @param ar array with one line per array entry, null and empty objects are allowed * @return string array with wrapped strings: null of input object was null or its toString() returned null, empty array if empty string, array with lines of wrappings otherwise */ public static final String[] WRAP_LINES(final int length, String[] ar){ if(ar==null){ return null; } String[] ret = new String[0]; for(int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy