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

de.japkit.el.juel.MapRootResolver Maven / Gradle / Ivy

package de.japkit.el.juel;

import com.google.common.base.Objects;
import java.beans.FeatureDescriptor;
import java.util.Iterator;
import java.util.Map;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.PropertyNotFoundException;

/**
 * Resolves root properties (base == null) from a map.
 */
@SuppressWarnings("all")
public class MapRootResolver extends ELResolver {
  private Map map;
  
  private boolean isResolvable(final Object base) {
    return Objects.equal(base, null);
  }
  
  private boolean resolve(final ELContext context, final Object base, final Object property) {
    context.setPropertyResolved((this.isResolvable(base) && (property instanceof String)));
    return context.isPropertyResolved();
  }
  
  @Override
  public Class getCommonPropertyType(final ELContext context, final Object base) {
    Class _xifexpression = null;
    boolean _isResolvable = this.isResolvable(context);
    if (_isResolvable) {
      _xifexpression = String.class;
    } else {
      _xifexpression = null;
    }
    return _xifexpression;
  }
  
  @Override
  public Iterator getFeatureDescriptors(final ELContext context, final Object base) {
    return null;
  }
  
  @Override
  public Class getType(final ELContext context, final Object base, final Object property) {
    Class _xifexpression = null;
    boolean _resolve = this.resolve(context, base, property);
    if (_resolve) {
      _xifexpression = Object.class;
    } else {
      _xifexpression = null;
    }
    return _xifexpression;
  }
  
  @Override
  public Object getValue(final ELContext context, final Object base, final Object property) {
    boolean _resolve = this.resolve(context, base, property);
    if (_resolve) {
      boolean _containsKey = this.map.containsKey(property);
      boolean _not = (!_containsKey);
      if (_not) {
        throw new PropertyNotFoundException(("Cannot find property " + property));
      }
      return this.map.get(property);
    }
    return null;
  }
  
  @Override
  public boolean isReadOnly(final ELContext context, final Object base, final Object property) {
    return true;
  }
  
  @Override
  public void setValue(final ELContext context, final Object base, final Object property, final Object value) {
    throw new UnsupportedOperationException("Not allowed to set root properties.");
  }
  
  public MapRootResolver(final Map map) {
    this.map = map;
  }
}