001package com.pi4j.util;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  StringUtil.java  
009 * 
010 * This file is part of the Pi4J project. More information about 
011 * this project can be found here:  http://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2015 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 * 
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 * 
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033public class StringUtil {
034    
035    public static final String EMPTY = "";
036    public static final char DEFAULT_PAD_CHAR = ' ';
037
038    public static boolean isNullOrEmpty(String data, boolean trim){
039        if(data == null)
040            return true;
041        
042        // trim if requested
043        String test = data;
044        if(trim)
045            test = data.trim();
046            
047        return (test.length() <= 0);        
048    }
049    
050    public static boolean isNullOrEmpty(String data){
051        return isNullOrEmpty(data, false);        
052    }
053
054    public static boolean isNotNullOrEmpty(String data){
055        return isNotNullOrEmpty(data, false);        
056    }
057
058    public static boolean isNotNullOrEmpty(String data, boolean trim){
059        return !(isNullOrEmpty(data, trim));        
060    }
061    
062    public static boolean contains(String source, String target)  {
063        return (null != source && null != target && source.contains(target));
064    }
065
066    public static boolean contains(String source, String[] targets)  {
067        if (null != source && null != targets) {
068            for(String target : targets) {
069                if (source.contains(target)) {
070                    return true;
071                }
072            }
073        }
074        return false;
075    }     
076
077    public static boolean contains(String[] sources, String target)  {
078        if (null != sources && null != target) { 
079            for (String source : sources) {
080                if(contains(source, target))
081                    return true;
082            }
083        }
084        return false;
085    }     
086
087    public static boolean contains(String[] sources, String[] targets)  {
088        if (null != sources && null != targets) { 
089            for (String source : sources) {
090                if(contains(source, targets))
091                    return true;
092            }
093        }
094        return false;
095    }     
096    
097    public static String create(int length)  {
098        return create(DEFAULT_PAD_CHAR, length);
099    }
100    
101    public static String create(char c, int length)  {
102        StringBuilder sb = new StringBuilder(length);
103        for(int index = 0; index < length; index++)
104            sb.append(c);
105        return sb.toString();
106    }     
107    
108    public static String create(String s, int length)  {
109        StringBuilder sb = new StringBuilder(length * s.length());
110        for(int index = 0; index < length; index++)
111            sb.append(s);
112        return sb.toString();
113    }     
114
115    public static String padLeft(String data, int length)  {
116        return padLeft(data, DEFAULT_PAD_CHAR, length);
117    }
118    
119    public static String padLeft(String data, char pad, int length)  {
120        StringBuilder sb = new StringBuilder(data.length() + length);
121        for(int index = 0; index < length; index++)
122            sb.append(pad);
123        sb.append(data);
124        return sb.toString();
125    }     
126
127    public static String padLeft(String data, String pad, int length)  {
128        StringBuilder sb = new StringBuilder(data.length() + (length * pad.length()));
129        for(int index = 0; index < length; index++)
130            sb.append(pad);
131        sb.append(data);
132        return sb.toString();
133    }     
134
135    public static String padRight(String data, int length)  {
136        return padRight(data, DEFAULT_PAD_CHAR, length);
137    }
138    
139    public static String padRight(String data, char pad, int length)  {
140        StringBuilder sb = new StringBuilder(data.length() + length);
141        sb.append(data);
142        for(int index = 0; index < length; index++)
143            sb.append(pad);        
144        return sb.toString();
145    }     
146
147    public static String padRight(String data, String pad, int length)  {
148        StringBuilder sb = new StringBuilder(data.length() + (length * pad.length()));
149        sb.append(data);
150        for(int index = 0; index < length; index++)
151            sb.append(pad);
152        return sb.toString();
153    }     
154
155    public static String pad(String data, int length)  {
156        return pad(data, DEFAULT_PAD_CHAR, length);
157    }
158    
159    public static String pad(String data, char pad, int length)  {
160        return create(pad, length) + data + create(pad, length);
161    }
162
163    public static String pad(String data, String pad, int length)  {
164        return create(pad, length) + data + create(pad, length);
165    }
166
167    public static String padCenter(String data, int length) {
168        return padCenter(data, DEFAULT_PAD_CHAR, length);
169    }
170    
171    public static String padCenter(String data, char pad, int length) {
172        if(data.length() < length) {
173            int needed = length - data.length();
174            int padNeeded = needed / 2;
175            StringBuilder result = new StringBuilder();
176            result.append(create(pad, padNeeded));
177            result.append(data);
178            result.append(create(pad, padNeeded));
179            int remaining = length - result.length();
180            result.append(create(pad, remaining));
181            return result.toString();
182        }
183        return data;
184    }
185
186    public static String trimLeft(String data)  {
187        return trimLeft(data, DEFAULT_PAD_CHAR);
188    }
189    
190    public static String trimLeft(String data, char trim)  {
191        for(int index = 0; index < data.length(); index++)
192            if(!(data.charAt(index) == trim))
193                return data.substring(index);
194        return EMPTY;
195    }     
196
197    public static String trimRight(String data)  {
198        return trimRight(data, DEFAULT_PAD_CHAR);
199    }
200    
201    public static String trimRight(String data, char trim)  {
202        int count = 0;
203        for(int index = data.length(); index > 0; index--)
204            if(data.charAt(index-1) == trim)
205                count++;
206            else
207                return data.substring(0, data.length() - count);
208        return EMPTY;
209    }     
210
211    public static String trim(String data)  {
212        return trim(data, DEFAULT_PAD_CHAR);
213    }
214    
215    public static String trim(String data, char trim)  {
216        String result = trimLeft(data, trim);
217        return trimRight(result, trim);
218    }
219    
220}