~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.system.util;
 
2
 
 
3
import java.util.Collection;
 
4
 
 
5
/*
 
6
 * Copyright (c) 2004-2007, University of Oslo
 
7
 * All rights reserved.
 
8
 *
 
9
 * Redistribution and use in source and binary forms, with or without
 
10
 * modification, are permitted provided that the following conditions are met:
 
11
 * * Redistributions of source code must retain the above copyright notice, this
 
12
 *   list of conditions and the following disclaimer.
 
13
 * * Redistributions in binary form must reproduce the above copyright notice,
 
14
 *   this list of conditions and the following disclaimer in the documentation
 
15
 *   and/or other materials provided with the distribution.
 
16
 * * Neither the name of the HISP project nor the names of its contributors may
 
17
 *   be used to endorse or promote products derived from this software without
 
18
 *   specific prior written permission.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
22
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
23
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
24
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
25
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
26
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
27
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
28
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
30
 */
 
31
 
 
32
/**
 
33
 * @author Lars Helge Overland
 
34
 * @version $Id$
 
35
 */
 
36
public class TextUtils
 
37
{
 
38
    /**
 
39
     * Gets the sub string of the given string. If the beginIndex is larger than
 
40
     * the length of the string, the empty string is returned. If the beginIndex +
 
41
     * the length is larger than the length of the string, the part of the string
 
42
     * following the beginIndex is returned.
 
43
     * 
 
44
     * @param string the string.
 
45
     * @param beginIndex the zero-based begin index.
 
46
     * @param length the length of the sub string starting at the begin index.
 
47
     * @return the sub string of the given string.
 
48
     */
 
49
    public static String subString( String string, int beginIndex, int length )
 
50
    {
 
51
        int endIndex = beginIndex + length;
 
52
        
 
53
        if ( beginIndex >= string.length()  )
 
54
        {
 
55
            return "";
 
56
        }
 
57
        
 
58
        if ( endIndex > string.length() )
 
59
        {
 
60
            return string.substring( beginIndex, string.length() );
 
61
        }
 
62
        
 
63
        return string.substring( beginIndex, endIndex );
 
64
    }
 
65
 
 
66
    /**
 
67
     * Transforms a collection of Integers into a comma delimited String.
 
68
     * 
 
69
     * @param elements the collection of Integers
 
70
     * @return a comma delimited String.
 
71
     */
 
72
    public static String getCommaDelimitedString( Collection<Integer> elements )
 
73
    {
 
74
        if ( elements != null && elements.size() > 0 )
 
75
        {
 
76
            StringBuffer buffer = new StringBuffer();        
 
77
        
 
78
            for ( Integer element : elements )
 
79
            {
 
80
                buffer.append( element.toString() ).append( ", " );
 
81
            }
 
82
            
 
83
            return buffer.substring( 0, buffer.length() - ", ".length() );
 
84
        }
 
85
        
 
86
        return null;
 
87
    }
 
88
    
 
89
    /**
 
90
     * Returns null if the given string is not null and contains no charachters,
 
91
     * the string itselft otherwise.
 
92
     * 
 
93
     * @param string the string.
 
94
     * @return null if the given string is not null and contains no charachters,
 
95
     *         the string itself otherwise.
 
96
     */
 
97
    public static String nullIfEmpty( String string )
 
98
    {
 
99
        return string != null && string.trim().length() == 0 ? null : string;
 
100
    }
 
101
}