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

com.sencha.gxt.data.shared.loader.AbstractAutoBeanReader Maven / Gradle / Ivy

There is a newer version: 3.1.1
Show newest version
/**
 * Sencha GXT 3.1.0 - Sencha for GWT
 * Copyright(c) 2007-2014, Sencha, Inc.
 * [email protected]
 *
 * http://www.sencha.com/products/gxt/license/
 */
package com.sencha.gxt.data.shared.loader;

import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;
import com.google.web.bindery.autobean.shared.Splittable;

/**
 * Abstract base class for Readers that turn the incoming data into
 * {@link AutoBean}s.
 * 
 * @param  Expected model type by the {@link Loader} that uses this
 *          {@link DataReader}
 * @param  Intermediate type before getting to M, allowing
 *          {@link #createReturnData(Object, Object)} to further modify the data
 * @param  Incoming data type from the {@link DataProxy}
 */
public abstract class AbstractAutoBeanReader implements DataReader {
  private final AutoBeanFactory factory;
  private final Class rootType;

  /**
   * Creates a new loader.
   * 
   * @param factory AutoBeanFactory instance capable of building all of the
   *          required classes
   * @param rootBeanType AutoBean based type to represent the base data in the
   *          Splittable
   */
  public AbstractAutoBeanReader(AutoBeanFactory factory, Class rootBeanType) {
    this.factory = factory;
    this.rootType = rootBeanType;
  }

  @Override
  public M read(Object loadConfig, D data) {
    Splittable s = readSplittable(loadConfig, data);

    AutoBean result = AutoBeanCodex.decode(factory, rootType, s);
    return createReturnData(loadConfig, result.as());
  }

  /**
   * Responsible for the object being returned by the reader. Default
   * implementation casts right to the return type, but can be overridden to
   * provide a more specialized implementation.
   * 
   * @param loadConfig the load config
   * @param records the list of models
   * @return the data to be returned by the reader
   */
  @SuppressWarnings("unchecked")
  protected M createReturnData(Object loadConfig, B records) {
    return (M) records;
  }

  /**
   * Implemented by subclasses to provide a {@link Splittable} based on the
   * incoming data.
   * 
   * @param loadConfig the load config
   * @param data the data to read
   * @return the splittable instance
   */
  protected abstract Splittable readSplittable(Object loadConfig, D data);
}