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

xapi.collect.impl.AbstractMultiInitMap Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
package xapi.collect.impl;

import xapi.util.X_Runtime;
import xapi.util.api.ConvertsValue;
import xapi.util.api.MergesValues;
import xapi.util.api.Pair;
import xapi.util.impl.PairBuilder;

public class AbstractMultiInitMap 
extends InitMapDefault,Value>
implements MergesValues
{

  private transient volatile Params params;

  private boolean clearState;


  protected static  ConvertsValue, String> adapt(final ConvertsValue keyConverter) {
    return new ConvertsValue,String>() {
      @Override
      public String convert(Pair from) {
        return keyConverter.convert(from.get0());
      }
    };
  }

  @SuppressWarnings("unchecked") //ALWAYS_NULL is erased to object; anything is safe
  public AbstractMultiInitMap(ConvertsValue keyConverter) {
    super(AbstractMultiInitMap.adapt(keyConverter), ALWAYS_NULL);
    clearState = true;
  }
  public AbstractMultiInitMap(ConvertsValue keyConverter, ConvertsValue,Value> valueConverter) {
    super(AbstractMultiInitMap.adapt(keyConverter), valueConverter);
    clearState = true;
  }

  public static  AbstractMultiInitMap
    stringMultiInitMap(final ConvertsValue converter) {
    return new AbstractMultiInitMap(PASS_THRU,
      new ConvertsValue, Value>() {
      @Override
      public Value convert(Pair from) {
        return converter.convert(from.get1());
      }
    });
  }

  public Value get(Key key, Params params) {
    if (X_Runtime.isMultithreaded()) {
      synchronized (getLock(key)) {
        return doGet(key, params);
      }
    }else {
      return doGet(key, params);
    }
  }

  private Value doGet(Key key, Params params) {
    this.params = params;
    try {
      return super.get(PairBuilder.pairOf(key, params));
    }finally {
      if (isClearState())
        this.params = null;
    }
  }

  protected boolean isClearState() {
    return clearState;
  }

  public void setClearState(boolean clearImmediately) {
    this.clearState = clearImmediately;
  }

  @Override
  public final Value initialize(Pair params) {
    Value value = null;
    try {
      value = valueProvider.convert(params);
    } catch (Throwable e) {
      logInitError(params, e);
    }
    if (value == null)
      value = initialize(params.get0(), params.get1() == null ? defaultParams() : params.get1());
    return value;
  }

  protected void logInitError(Pair params, Throwable e) {
    // Can't use X_Log, as this class is used for run-once semantics in our injectors.
    System.err.println("Init error in "+getClass().getName()+" for "+params);
    if (X_Runtime.isDebug()) {
      while (e != null) {
        e.printStackTrace();
        e = e.getCause();
      }
    }
  }

  protected Params defaultParams() {
    return params;
  }

  protected Value initialize(Key key, Params params) {
    return null;
  }

  @Override
  public Value merge(Key key1, Params key2) {
    return get(key1, key2);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy