📄 memoryhelper.java
字号:
/* * $Id: MemoryHelper.java 5462 2005-08-05 18:35:48Z jonesde $ * * Copyright (c) 2001 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. */package org.ofbiz.entity.datasource;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.ofbiz.base.util.Debug;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericNotImplementedException;import org.ofbiz.entity.GenericPK;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.condition.EntityCondition;import org.ofbiz.entity.jdbc.SqlJdbcUtil;import org.ofbiz.entity.model.ModelEntity;import org.ofbiz.entity.model.ModelField;import org.ofbiz.entity.model.ModelFieldTypeReader;import org.ofbiz.entity.model.ModelRelation;import org.ofbiz.entity.util.EntityFindOptions;import org.ofbiz.entity.util.EntityListIterator;/** * Partial GenericHelper implementation that is entirely memory-based, * to be used for simple unit testing (can't do anything beyond searches * for primary keys, findByOr and findByAnd). * * @author <a href="mailto:plightbo@.com">Pat Lightbody</a> */public class MemoryHelper implements GenericHelper { public static final String module = MemoryHelper.class.getName(); private static Map cache = new HashMap(); public static void clearCache() { cache = new HashMap(); } private String helperName; private boolean addToCache(GenericValue value) { if (value == null) { return false; } if (!veryifyValue(value)) { return false; } value = (GenericValue) value.clone(); HashMap entityCache = (HashMap) cache.get(value.getEntityName()); if (entityCache == null) { entityCache = new HashMap(); cache.put(value.getEntityName(), entityCache); } entityCache.put(value.getPrimaryKey(), value); return true; } private GenericValue findFromCache(GenericPK pk) { if (pk == null) { return null; } HashMap entityCache = (HashMap) cache.get(pk.getEntityName()); if (entityCache == null) { return null; } GenericValue value = (GenericValue) entityCache.get(pk); if (value == null) { return null; } else { return (GenericValue) value.clone(); } } private int removeFromCache(GenericPK pk) { if (pk == null) { return 0; } HashMap entityCache = (HashMap) cache.get(pk.getEntityName()); if (entityCache == null) { return 0; } Object o = entityCache.remove(pk); if (o == null) { return 0; } else { return 1; } } private int removeFromCache(String entityName, EntityCondition condition) { if (entityName == null || condition == null) { return 0; } HashMap entityCache = (HashMap) cache.get(entityName); if (entityCache == null) { return 0; } Iterator it = entityCache.values().iterator(); int count = 0; while (it.hasNext()) { GenericValue value = (GenericValue) it.next(); if (condition.entityMatches(value)) { it.remove(); count++; } } return count; } private boolean isAndMatch(Map values, Map fields) { for (Iterator iterator = fields.entrySet().iterator(); iterator.hasNext();) { Map.Entry mapEntry = (Map.Entry) iterator.next(); if (mapEntry.getValue() == null) { if (values.get(mapEntry.getKey()) != null) { return false; } } else { try { if (!mapEntry.getValue().equals(values.get(mapEntry.getKey()))) { return false; } } catch (Exception e) { return false; } } } return true; } private boolean isOrMatch(Map values, Map fields) { for (Iterator iterator = fields.entrySet().iterator(); iterator.hasNext();) { Map.Entry mapEntry = (Map.Entry) iterator.next(); if (mapEntry.getValue() == null) { if (values.get(mapEntry.getKey()) == null) { return true; } } else { try { if (mapEntry.getValue().equals(values.get(mapEntry.getKey()))) { return true; } } catch (Exception e) { Debug.logError(e, module); } } } return false; } private boolean veryifyValue(GenericValue value) { ModelEntity me = value.getModelEntity(); // make sure the PKs exist for (Iterator iterator = me.getPksIterator(); iterator.hasNext();) { ModelField field = (ModelField) iterator.next(); if (!value.containsKey(field.getName())) { return false; } } // make sure the value doesn't have any extra (unknown) fields for (Iterator iterator = value.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); if (me.getField((String) entry.getKey()) == null) { return false; } } // make sure all fields that are in the value are of the right type for (Iterator iterator = me.getFieldsIterator(); iterator.hasNext();) { ModelField field = (ModelField) iterator.next(); Object o = value.get(field.getName()); int typeValue = 0; try { typeValue = SqlJdbcUtil.getType(modelFieldTypeReader.getModelFieldType(field.getType()).getJavaType()); } catch (GenericNotImplementedException e) { return false; } if (o != null) { switch (typeValue) { case 1: if (!(o instanceof String)) { return false; } break; case 2: if (!(o instanceof java.sql.Timestamp)) { return false; } break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -