VariableReplacer.java :  » Internationalization-Localization » icu4j » com » ibm » icu » dev » test » util » Java Open Source

Java Open Source » Internationalization Localization » icu4j 
icu4j » com » ibm » icu » dev » test » util » VariableReplacer.java
/*
 *******************************************************************************
 * Copyright (C) 2002-2005, International Business Machines Corporation and    *
 * others. All Rights Reserved.                                                *
 *******************************************************************************
 */
package com.ibm.icu.dev.test.util;import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class VariableReplacer {
  // simple implementation for now
  private Comparator c;
  private Map m = new TreeMap(Collections.reverseOrder());
  
  // TODO - fix to do streams also, clean up implementation
  
  public VariableReplacer add(String variable, String value) {
    m.put(variable, value);
    return this;
  }
  public String replace(String source) {
    String oldSource;
    do {
      oldSource = source;
      for (Iterator it = m.keySet().iterator(); it.hasNext();) {
        String variable = (String) it.next();
        String value = (String) m.get(variable);
        source = replaceAll(source, variable, value);
      }
    } while (!source.equals(oldSource));
    return source;
  }
  public String replaceAll(String source, String key, String value) {
    while (true) {
      int pos = source.indexOf(key);
      if (pos < 0) return source;
      source = source.substring(0,pos) + value + source.substring(pos+key.length());
    }
  }
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.