001: /*
002: * $Id: MemoryStorage.java 476710 2006-11-19 05:05:14Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.showcase.application;
022:
023: import java.io.Serializable;
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import org.apache.struts2.showcase.exception.CreateException;
030: import org.apache.struts2.showcase.exception.DuplicateKeyException;
031: import org.apache.struts2.showcase.exception.StorageException;
032: import org.apache.struts2.showcase.exception.UpdateException;
033: import org.apache.struts2.showcase.model.IdEntity;
034:
035: /**
036: * MemoryStorage.
037: * Very simple in-memory persistence emulation.
038: *
039: */
040:
041: public class MemoryStorage implements Storage {
042:
043: private static final long serialVersionUID = 8611213748834904125L;
044:
045: private Map memory = new HashMap();
046:
047: private Map getEntityMap(Class entityClass) {
048: if (entityClass != null) {
049: Map tryMap = (Map) memory.get(entityClass);
050: if (tryMap == null) {
051: synchronized (memory) {
052: tryMap = new HashMap();
053: memory.put(entityClass, tryMap);
054: }
055: }
056: return tryMap;
057: } else {
058: return null;
059: }
060: }
061:
062: private IdEntity intStore(Class entityClass, IdEntity object) {
063: getEntityMap(entityClass).put(object.getId(), object);
064: return object;
065: }
066:
067: public IdEntity get(Class entityClass, Serializable id) {
068: if (entityClass != null && id != null) {
069: return (IdEntity) getEntityMap(entityClass).get(id);
070: } else {
071: return null;
072: }
073: }
074:
075: public Serializable create(IdEntity object) throws CreateException {
076: if (object == null) {
077: throw new CreateException(
078: "Either given class or object was null");
079: }
080: if (object.getId() == null) {
081: throw new CreateException(
082: "Cannot store object with null id");
083: }
084: if (get(object.getClass(), object.getId()) != null) {
085: throw new DuplicateKeyException(
086: "Object with this id already exists.");
087: }
088: return intStore(object.getClass(), object).getId();
089: }
090:
091: public IdEntity update(IdEntity object) throws UpdateException {
092: if (object == null) {
093: throw new UpdateException("Cannot update null object.");
094: }
095: if (get(object.getClass(), object.getId()) == null) {
096: throw new UpdateException("Object to update not found.");
097: }
098: return intStore(object.getClass(), object);
099: }
100:
101: public Serializable merge(IdEntity object) throws StorageException {
102: if (object == null) {
103: throw new StorageException("Cannot merge null object");
104: }
105: if (object.getId() == null
106: || get(object.getClass(), object.getId()) == null) {
107: return create(object);
108: } else {
109: return update(object).getId();
110: }
111: }
112:
113: public int delete(Class entityClass, Serializable id)
114: throws CreateException {
115: try {
116: if (get(entityClass, id) != null) {
117: getEntityMap(entityClass).remove(id);
118: return 1;
119: } else {
120: return 0;
121: }
122: } catch (Exception e) {
123: throw new CreateException(e);
124: }
125: }
126:
127: public int delete(IdEntity object) throws CreateException {
128: if (object == null) {
129: throw new CreateException("Cannot delete null object");
130: }
131: return delete(object.getClass(), object.getId());
132: }
133:
134: public Collection findAll(Class entityClass) {
135: if (entityClass != null) {
136: return getEntityMap(entityClass).values();
137: } else {
138: return new ArrayList();
139: }
140: }
141:
142: public void reset() {
143: this .memory = new HashMap();
144: }
145:
146: }
|