]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/framework/src/com/ibm/icu/dev/util/VariableReplacer.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / framework / src / com / ibm / icu / dev / util / VariableReplacer.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2002-2012, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.util;
8
9 import java.util.Collections;
10 import java.util.Iterator;
11 import java.util.Map;
12 import java.util.TreeMap;
13
14 public class VariableReplacer {
15     // simple implementation for now
16     private Map m = new TreeMap(Collections.reverseOrder());
17
18     // TODO - fix to do streams also, clean up implementation
19
20     public VariableReplacer add(String variable, String value) {
21         m.put(variable, value);
22         return this;
23     }
24     public String replace(String source) {
25         String oldSource;
26         do {
27             oldSource = source;
28             for (Iterator it = m.keySet().iterator(); it.hasNext();) {
29                 String variable = (String) it.next();
30                 String value = (String) m.get(variable);
31                 source = replaceAll(source, variable, value);
32             }
33         } while (!source.equals(oldSource));
34         return source;
35     }
36     public String replaceAll(String source, String key, String value) {
37         while (true) {
38             int pos = source.indexOf(key);
39             if (pos < 0) return source;
40             source = source.substring(0,pos) + value + source.substring(pos+key.length());
41         }
42     }
43 }
44