/**************************************************************************************
* Copyright (C) 2008 EsperTech, Inc. All rights reserved. *
* http://esper.codehaus.org *
* http://www.espertech.com *
* ---------------------------------------------------------------------------------- *
* The software in this package is published under the terms of the GPL license *
* a copy of which has been included with this distribution in the license.txt file. *
**************************************************************************************/
package com.espertech.esper.event.bean;
import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.EventPropertyGetter;
import com.espertech.esper.client.PropertyAccessException;
import com.espertech.esper.event.EventAdapterService;
import java.util.List;
/**
* Getter for one or more levels deep nested properties.
*/
public class NestedPropertyGetter extends BaseNativePropertyGetter implements BeanEventPropertyGetter
{
private final BeanEventPropertyGetter[] getterChain;
/**
* Ctor.
* @param getterChain is the chain of getters to retrieve each nested property
* @param eventAdapterService is the cache and factory for event bean types and event wrappers
* @param finalPropertyType type of the entry returned
* @param finalGenericType generic type parameter of the entry returned, if any
*/
public NestedPropertyGetter(List<EventPropertyGetter> getterChain, EventAdapterService eventAdapterService, Class finalPropertyType, Class finalGenericType)
{
super(eventAdapterService, finalPropertyType, finalGenericType);
this.getterChain = new BeanEventPropertyGetter[getterChain.size()];
for (int i = 0; i < getterChain.size(); i++)
{
this.getterChain[i] = (BeanEventPropertyGetter) getterChain.get(i);
}
}
public Object getBeanProp(Object value) throws PropertyAccessException {
if (value == null)
{
return value;
}
for (int i = 0; i < getterChain.length; i++)
{
value = getterChain[i].getBeanProp(value);
if (value == null)
{
return null;
}
}
return value;
}
public boolean isBeanExistsProperty(Object value) {
if (value == null)
{
return false;
}
int lastElementIndex = getterChain.length - 1;
// walk the getter chain up to the previous-to-last element, returning its object value.
// any null values in between mean the property does not exists
for (int i = 0; i < getterChain.length - 1; i++)
{
value = getterChain[i].getBeanProp(value);
if (value == null)
{
return false;
}
}
return getterChain[lastElementIndex].isBeanExistsProperty(value);
}
public Object get(EventBean eventBean) throws PropertyAccessException
{
return getBeanProp(eventBean.getUnderlying());
}
public boolean isExistsProperty(EventBean eventBean)
{
return isBeanExistsProperty(eventBean.getUnderlying());
}
}