CompositeStorage.java

biz/hammurapi/legacy/persistence/CompositeStorage.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Type is not documented 2 34:1
Java Inspector 089 Undocumented method 2 41:17
Java Inspector 089 Undocumented method 2 46:9
Java Inspector 089 Undocumented method 2 62:9
Java Inspector 089 Undocumented method 2 74:9
Java Inspector 089 Undocumented method 2 91:9
Java Inspector 089 Undocumented method 2 108:9
Java Inspector 089 Undocumented method 2 119:9
Java Inspector 092 Override both equals() and hashCode(). Overriding of only one of these methods may cause unpredictable behavior of HashXXX collections. 2 37:9
Java Inspector 024 Avoid hardwired character literals 3 75:37
Java Inspector 024 Avoid hardwired character literals 3 92:37
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 48:56
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 52:56
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 68:47
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 77:56
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 94:56
Java Inspector 090 Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). 3 76:17
Java Inspector 090 Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). 3 93:17

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.legacy.persistence;
24
25import java.util.ArrayList;
26import java.util.Iterator;
27import java.util.List;
28
29/**
30 *
31 * @author Pavel Vlasov
32 * @version $Revision: 1.1 $
33 */
34public class CompositeStorage implements Storage {
35 private List storages=new ArrayList();
36
37 private class StorageEntry {
38 String key;
39 Storage storage;
40
41 public boolean equals(Object obj) {
42 return obj instanceof StorageEntry && ((StorageEntry) obj).key.equals(key);
43 }
44 }
45
46 public void addStorage(String key, Storage storage) {
47 if (key==null) {
48 throw new NullPointerException("key==null");
49 }
50
51 if (storage==null) {
52 throw new NullPointerException("storage==null");
53 }
54
55 StorageEntry se=new StorageEntry();
56 se.key=key;
57 se.storage=storage;
58 storages.remove(se);
59 storages.add(se);
60 }
61
62 public String put(Object o) throws PersistenceException {
63 Iterator it=storages.iterator();
64 while (it.hasNext()) {
65 StorageEntry se=(StorageEntry) it.next();
66 String key=se.storage.put(o);
67 if (key!=null) {
68 return se.key+":"+key;
69 }
70 }
71 return null;
72 }
73
74 public Object get(String key) throws PersistenceException {
75 int idx=key.indexOf(':');
76 if (idx==-1) {
77 throw new PersistenceException("Invalid key format");
78 } else {
79 String skey=key.substring(0, idx);
80 Iterator it=storages.iterator();
81 while (it.hasNext()) {
82 StorageEntry se=(StorageEntry) it.next();
83 if (skey.equals(se.key)) {
84 return se.storage.get(key.substring(idx+1));
85 }
86 }
87 }
88 return null;
89 }
90
91 public void remove(String key) throws PersistenceException {
92 int idx=key.indexOf(':');
93 if (idx==-1) {
94 throw new PersistenceException("Invalid key format");
95 } else {
96 String skey=key.substring(0, idx);
97 Iterator it=storages.iterator();
98 while (it.hasNext()) {
99 StorageEntry se=(StorageEntry) it.next();
100 if (skey.equals(se.key)) {
101 se.storage.remove(key.substring(idx+1));
102 return;
103 }
104 }
105 }
106 }
107
108 public Storage getStorage(Class storageClass) {
109 Iterator it=storages.iterator();
110 while (it.hasNext()) {
111 StorageEntry se=(StorageEntry) it.next();
112 if (storageClass.isInstance(se.storage)) {
113 return se.storage;
114 }
115 }
116 return null;
117 }
118
119 public Storage getStorage(String storageKey) {
120 Iterator it=storages.iterator();
121 while (it.hasNext()) {
122 StorageEntry se=(StorageEntry) it.next();
123 if (storageKey.equals(se.key)) {
124 return se.storage;
125 }
126 }
127 return null;
128 }
129}
130