src/org/sonews/util/StringTemplate.java
author Christian Lins <christian@lins.me>
Wed Sep 14 23:25:00 2011 +0200 (2011-09-14)
changeset 62 be4e87479855
parent 35 ed84c8bdd87b
permissions -rwxr-xr-x
Reformatting XDaemonCommand
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
chris@1
    19
package org.sonews.util;
chris@1
    20
chris@1
    21
import java.util.HashMap;
chris@1
    22
import java.util.Map;
chris@1
    23
chris@1
    24
/**
chris@1
    25
 * Class that allows simple String template handling.
chris@1
    26
 * @author Christian Lins
chris@1
    27
 * @since sonews/0.5.0
chris@1
    28
 */
cli@37
    29
public class StringTemplate
chris@1
    30
{
chris@1
    31
cli@37
    32
	private String str = null;
cli@37
    33
	private String templateDelimiter = "%";
cli@37
    34
	private Map<String, String> templateValues = new HashMap<String, String>();
chris@1
    35
cli@37
    36
	public StringTemplate(String str, final String templateDelimiter)
cli@37
    37
	{
cli@37
    38
		if (str == null || templateDelimiter == null) {
cli@37
    39
			throw new IllegalArgumentException("null arguments not allowed");
cli@37
    40
		}
chris@1
    41
cli@37
    42
		this.str = str;
cli@37
    43
		this.templateDelimiter = templateDelimiter;
cli@37
    44
	}
chris@1
    45
cli@37
    46
	public StringTemplate(String str)
cli@37
    47
	{
cli@37
    48
		this(str, "%");
cli@37
    49
	}
chris@1
    50
cli@37
    51
	public StringTemplate set(String template, String value)
cli@37
    52
	{
cli@37
    53
		if (template == null || value == null) {
cli@37
    54
			throw new IllegalArgumentException("null arguments not allowed");
cli@37
    55
		}
cli@37
    56
cli@37
    57
		this.templateValues.put(template, value);
cli@37
    58
		return this;
cli@37
    59
	}
cli@37
    60
cli@37
    61
	public StringTemplate set(String template, long value)
cli@37
    62
	{
cli@37
    63
		return set(template, Long.toString(value));
cli@37
    64
	}
cli@37
    65
cli@37
    66
	public StringTemplate set(String template, double value)
cli@37
    67
	{
cli@37
    68
		return set(template, Double.toString(value));
cli@37
    69
	}
cli@37
    70
cli@37
    71
	public StringTemplate set(String template, Object obj)
cli@37
    72
	{
cli@37
    73
		if (template == null || obj == null) {
cli@37
    74
			throw new IllegalArgumentException("null arguments not allowed");
cli@37
    75
		}
cli@37
    76
cli@37
    77
		return set(template, obj.toString());
cli@37
    78
	}
cli@37
    79
cli@37
    80
	@Override
cli@37
    81
	public String toString()
cli@37
    82
	{
cli@37
    83
		String ret = str;
cli@37
    84
cli@37
    85
		for (String key : this.templateValues.keySet()) {
cli@37
    86
			String value = this.templateValues.get(key);
cli@37
    87
			ret = ret.replace(templateDelimiter + key, value);
cli@37
    88
		}
cli@37
    89
cli@37
    90
		return ret;
cli@37
    91
	}
chris@1
    92
}