Subversion Repositories DevTools

Rev

Rev 6914 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6914 dpurdie 1
package com.erggroup.buildtool.utilities;
7333 dpurdie 2
 
3
import java.util.Iterator;
4
 
6914 dpurdie 5
/**
6
 * 
7
 * Class to simplify the appending of text to a string
8
 * Will append text with a configurable 'joiner'
9
 * 
10
 * The 'joiner' will only be used between strings. Not before the first or after the last
11
 * The 'joiner' may be changed on the fly
12
 * 
13
 * The append and join methods may be chained
14
 * 
15
 *
16
 */
17
 
18
public class StringAppender {
19
    String joiner;
20
    StringBuilder text = new StringBuilder();
21
    boolean skip = true;
22
 
23
    public StringAppender(String joiner)
24
    {
25
        setJoin(joiner);
26
    }
27
 
28
    public StringAppender setJoin(String joiner)
29
    {
30
        this.joiner = joiner;
31
        return this;
32
    }
33
 
34
    public StringAppender append(String text)
35
    {
36
        if ( !skip ) {
37
            this.text.append(joiner);
38
        }
39
        skip = false;
40
        this.text.append(text);
41
        return this;
42
    }
43
 
7333 dpurdie 44
    /**
45
     * Append a Set/List of strings (Sort of like a join)
46
     * @param itr - to iterate over the collection of strings
47
     * @return
48
     */
49
    public StringAppender append(Iterator<String> itr)
50
    {
51
        while(itr.hasNext()){
52
            append(itr.next());
53
          }
54
        return this;
55
    }
56
 
6914 dpurdie 57
    public String toString()
58
    {
59
        return text.toString();
60
    }
61
 
62
    public StringBuilder getStringBuilder()
63
    {
64
        return text;
65
    }
66
 
67
    public int length() {
68
        return text.length();
69
    }
70
}