Skip to content

Commit

Permalink
Add test GtfsDaoImplTest - make sure the dao behave the same before a…
Browse files Browse the repository at this point in the history
…nd after refactoring.

Remove unused code, collapse DAO inheritance structure to get rid of unnecessary complexity.
(Refactor OTP to have new OTP classes to replace the OBA GTFS classes #2494)
  • Loading branch information
Thomas Gran committed Sep 12, 2017
1 parent 1cef26b commit 5155277
Show file tree
Hide file tree
Showing 42 changed files with 898 additions and 1,369 deletions.
198 changes: 68 additions & 130 deletions src/main/java/org/onebusaway2/gtfs/impl/GenericDaoImpl.java
@@ -1,12 +1,12 @@
/** /**
* Copyright (C) 2011 Brian Ferris <bdferris@onebusaway.org> * Copyright (C) 2011 Brian Ferris <bdferris@onebusaway.org>
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -15,164 +15,102 @@
*/ */
package org.onebusaway2.gtfs.impl; package org.onebusaway2.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.onebusaway2.gtfs.model.IdentityBean; import org.onebusaway2.gtfs.model.IdentityBean;
import org.onebusaway2.gtfs.services.GenericMutableDao;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


public class GenericDaoImpl implements GenericMutableDao { import java.io.Serializable;

import java.lang.reflect.Field;
private final Logger _log = LoggerFactory.getLogger(GenericDaoImpl.class); import java.util.*;

private Map<Class<?>, Map<Object, Object>> _entitiesByClassAndId = new HashMap<Class<?>, Map<Object, Object>>();


private Map<Class<?>, EntityHandler<Serializable>> _handlers = new HashMap<Class<?>, EntityHandler<Serializable>>(); public class GenericDaoImpl {


private boolean _generateIds = true; private final static Logger LOG = LoggerFactory.getLogger(GenericDaoImpl.class);


public void setGenerateIds(boolean generateIds) { private Map<Class<?>, Map<Object, Object>> entitiesByClassAndId = new HashMap<>();
_generateIds = generateIds;
}


public Set<Class<?>> getEntityClasses() { private Map<Class<?>, EntityHandler<Serializable>> handlers = new HashMap<Class<?>, EntityHandler<Serializable>>();
return _entitiesByClassAndId.keySet();
}


public void clear() { public void clear() {
_entitiesByClassAndId.clear(); entitiesByClassAndId.clear();
} }


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


/**** @SuppressWarnings("unchecked") <T> Collection<T> getAllEntitiesForType(Class<T> type) {
* {@link GenericMutableDao} Interface Map<Object, Object> entitiesById = entitiesByClassAndId.get(type);
****/ if (entitiesById == null)
return new ArrayList<T>();
return (Collection<T>) entitiesById.values();
}


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked") <T> T getEntityForId(Class<T> type, Serializable id) {
public <T> Collection<T> getAllEntitiesForType(Class<T> type) { Map<Object, Object> byId = entitiesByClassAndId.get(type);
Map<Object, Object> entitiesById = _entitiesByClassAndId.get(type);
if (entitiesById == null)
return new ArrayList<T>();
return (Collection<T>) entitiesById.values();
}


@SuppressWarnings("unchecked") if (byId == null) {
public <T> T getEntityForId(Class<T> type, Serializable id) { LOG.debug("no stored entities type {}", type);
Map<Object, Object> byId = _entitiesByClassAndId.get(type); return null;
}


if (byId == null) { return (T) byId.get(id);
_log.debug("no stored entities type {}", type);
return null;
} }


return (T) byId.get(id); @SuppressWarnings("unchecked") void saveEntity(Object entity) {
}


@SuppressWarnings("unchecked") Class<?> c = entity.getClass();
@Override
public void saveEntity(Object entity) {


Class<?> c = entity.getClass(); EntityHandler<Serializable> handler = handlers.get(c);
if (handler == null) {
handler = (EntityHandler<Serializable>) createEntityHandler(c);
handlers.put(c, handler);
}


EntityHandler<Serializable> handler = _handlers.get(c); IdentityBean<Serializable> bean = ((IdentityBean<Serializable>) entity);
if (handler == null) { handler.handle(bean);
handler = (EntityHandler<Serializable>) createEntityHandler(c);
_handlers.put(c, handler);
}


IdentityBean<Serializable> bean = ((IdentityBean<Serializable>) entity); Map<Object, Object> byId = entitiesByClassAndId.computeIfAbsent(c, k -> new HashMap<>());
handler.handle(bean);


Map<Object, Object> byId = _entitiesByClassAndId.get(c); Object id = bean.getId();
if (byId == null) { Object prev = byId.put(id, entity);
byId = new HashMap<Object, Object>(); if (prev != null)
_entitiesByClassAndId.put(c, byId); LOG.warn("entity with id already exists: class=" + c + " id=" + id + " prev=" + prev
+ " new=" + entity);
} }
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 <T> void clearAllEntitiesForType(Class<T> type) {
_entitiesByClassAndId.remove(type);
}

@Override
public <K extends Serializable, T extends IdentityBean<K>> void removeEntity(
T entity) {

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

Map<Object, Object> 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);
}


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


private EntityHandler<?> createEntityHandler(Class<?> entityType) { private EntityHandler<?> createEntityHandler(Class<?> entityType) {

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


} return (EntityHandler<Serializable>) entity -> {
};
} }


return new EntityHandler<Serializable>() { private interface EntityHandler<T extends Serializable> {
public void handle(IdentityBean<Serializable> entity) { void handle(IdentityBean<T> entity);
} }
};
}

private interface EntityHandler<T extends Serializable> {
public void handle(IdentityBean<T> entity);
}


private static class GeneratedIdHandler implements EntityHandler<Integer> { private static class GeneratedIdHandler implements EntityHandler<Integer> {


private int _maxId = 0; private int maxId = 0;


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


} }

0 comments on commit 5155277

Please sign in to comment.