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
/**
 * Copyright (C) 2011 Brian Ferris 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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 updateEntity(Object entity) {
    // No-op required, since entity already resides in memory
  }

  @Override
  public void saveOrUpdateEntity(Object entity) {

    @SuppressWarnings("unchecked")
    IdentityBean bean = ((IdentityBean) entity);

    Object existing = getEntityForId(entity.getClass(), bean.getId());

    // If the entity has already been inserted into storage, then we don't need
    // to save it
    if (existing == entity)
      return;
    saveEntity(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