All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xapi.source.write.MappedTemplate Maven / Gradle / Ivy

Go to download

This module exists solely to package all other gwt modules into a single uber jar. This makes deploying to non-mavenized targets much easier. Of course, you would be wise to inherit your dependencies individually; the uber jar is intended for projects like collide, which have complex configuration, and adding many jars would be a pain.

The newest version!
package xapi.source.write;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class MappedTemplate extends Template {

  private Map positions;

  public MappedTemplate(String template, Iterable items) {
    this(template, toArray(items));
  }

  public MappedTemplate(String template, String ... replaceables) {
    super(template, replaceables);
    positions = new HashMap();
    for (int i = 0, m = replaceables.length; i < m; i++) {
      positions.put(replaceables[i], i);
    }
  }

  public String applyMap(Iterable> map) {
    Object[] values = new Object[positions.size()];
    for (Entry entry : map) {
      String key = entry.getKey();
      Integer pos = positions.get(key);
      if (pos == null) {
        warnMissing(key);
      } else {
        values[pos] = retrieve(key, entry.getValue());
      }
    }
    return apply(values);
  }

  /**
   * Allow subclasses to perform additional data retrieval logic.
   */
  protected Object retrieve(String key, Object object) {
    return object;
  }

  protected void warnMissing(String key) {
    // Default does nothing; this is here so you can implement logging as you see fit.
  }

  public boolean hasKey(String key) {
    return positions.containsKey(key);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy