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

org.onebusaway.gtfs.impl.GenericDaoImpl Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
package org.onebusaway.gtfs.impl;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.onebusaway.gtfs.model.IdentityBean;
import org.onebusaway.gtfs.services.GenericMutableDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GenericDaoImpl implements GenericMutableDao {

  private final Logger _log = LoggerFactory.getLogger(GenericDaoImpl.class);

  private Map, Map> _entitiesByClassAndId = new HashMap, Map>();

  private Map, EntityHandler> _handlers = new HashMap, EntityHandler>();

  private boolean _generateIds = true;

  public void setGenerateIds(boolean generateIds) {
    _generateIds = generateIds;
  }

  public Set> getEntityClasses() {
    return _entitiesByClassAndId.keySet();
  }

  @SuppressWarnings("unchecked")
  public  Map getEntitiesByIdForEntityType(Class keyType,
      Class entityType) {
    return (Map) _entitiesByClassAndId.get(entityType);
  }

  /****
   * {@link GenericMutableDao} Interface
   ****/

  @SuppressWarnings("unchecked")
  @Override
  public  Collection getAllEntitiesForType(Class type) {
    Map entitiesById = _entitiesByClassAndId.get(type);
    if (entitiesById == null)
      return new ArrayList();
    return (Collection) entitiesById.values();
  }

  @SuppressWarnings("unchecked")
  @Override
  public  T getEntityForId(Class type, Serializable id) {
    Map byId = _entitiesByClassAndId.get(type);

    if (byId == null) {
      _log.warn("no stored entities type " + type);
      return null;
    }

    return (T) byId.get(id);
  }

  @SuppressWarnings("unchecked")
  @Override
  public void saveEntity(Object entity) {

    Class c = entity.getClass();

    EntityHandler handler = _handlers.get(c);
    if (handler == null) {
      handler = (EntityHandler) createEntityHandler(c);
      _handlers.put(c, handler);
    }

    IdentityBean bean = ((IdentityBean) entity);
    handler.handle(bean);

    Map byId = _entitiesByClassAndId.get(c);
    if (byId == null) {
      byId = new HashMap();
      _entitiesByClassAndId.put(c, byId);
    }
    Object id = bean.getId();
    Object prev = byId.put(id, entity);
    if (prev != null)
      _log.warn("entity with id already exists: class=" + c + " id=" + id
          + " prev=" + prev + " new=" + entity);
  }

  @Override
  public  void clearAllEntitiesForType(Class type) {
    _entitiesByClassAndId.remove(type);
  }

  @Override
  public > void removeEntity(
      T entity) {

    Class type = entity.getClass();
    K id = entity.getId();

    Map byId = _entitiesByClassAndId.get(type);

    if (byId == null) {
      _log.warn("no stored entities type " + type);
      return;
    }

    Object found = byId.remove(id);

    if (found == null)
      _log.warn("no stored entity with type " + type + " and id " + id);
  }

  @Override
  public void open() {

  }

  @Override
  public void flush() {

  }

  @Override
  public void close() {

  }

  /****
   * Private Methods
   ****/

  private EntityHandler createEntityHandler(Class entityType) {

    if (_generateIds) {
      try {
        Field field = entityType.getDeclaredField("id");
        if (field != null) {
          Class type = field.getType();
          if (type.equals(Integer.class) || type.equals(Integer.TYPE))
            return new GeneratedIdHandler();
        }
      } catch (Exception ex) {

      }
    }

    return new EntityHandler() {
      public void handle(IdentityBean entity) {
      }
    };
  }

  private interface EntityHandler {
    public void handle(IdentityBean entity);
  }

  private static class GeneratedIdHandler implements EntityHandler {

    private int _maxId = 0;

    public void handle(IdentityBean entity) {
      Integer value = (Integer) entity.getId();
      if (value == null || value.intValue() == 0) {
        value = _maxId + 1;
        entity.setId(value);
      }
      _maxId = Math.max(_maxId, value.intValue());
    }
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy