2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.database;
26 public class DBMap<C> extends TableHandler<C> implements Map<String, C> {
27 public DBMap(Class<C> cls, String tabname, String keyfield) throws Exception {
28 this(ConnectionFactory.getDefaultInstance(), cls, tabname, keyfield);
30 public DBMap(ConnectionFactory cf, Class<C> cls, String tabname, String keyfield) throws Exception {
31 super(cf, cls, tabname, keyfield);
34 throw new UnsupportedOperationException();
36 public boolean containsKey(Object key) {
37 return(get(key) != null);
39 public boolean containsValue(Object value) {
40 throw new UnsupportedOperationException();
42 public boolean isEmpty() {
45 public Set<Map.Entry<String, C>> entrySet() {
48 public Set<String> keySet() {
49 Set<String> ret = new HashSet<>();
50 for (Map.Entry<String, C> x: list()) {
55 public void putAll(Map<? extends String, ? extends C> m) {
56 throw new UnsupportedOperationException();
61 public Collection<C> values() {
62 Collection<C> ret = new ArrayList<>();
63 for (Map.Entry<String, C> x: list()) {
64 ret.add(x.getValue());
68 public C get(Object key) {
69 if (!(key instanceof String)) {
72 return((new ConnWrapper<C, String>() {
73 protected C run(String key) throws Exception {
74 ps = c.prepareStatement(getstmt);
75 ps.setString(1, (String)key);
76 rs = ps.executeQuery();
80 C ret = cls.newInstance();
81 for (DBFieldHandler f: fields) {
86 }).protect(cf, (String)key));
88 public Set<Map.Entry<String, C>> list() {
89 return((new ConnWrapper<Set<Map.Entry<String, C>>, Object>() {
90 protected Set<Map.Entry<String, C>> run(Object junk) throws Exception {
91 DBFieldHandler keyfield = fields[fields.length - 1];
92 ps = c.prepareStatement(liststmt);
93 rs = ps.executeQuery();
94 Set<Map.Entry<String, C>> ret = new HashSet<>();
96 C val = cls.newInstance();
97 for (DBFieldHandler f: fields) {
100 String key = keyfield.getKey(val);
101 ret.add(new AbstractMap.SimpleEntry<String, C>(key, val));
105 }).protect(cf, null));
107 public C put(String key, C val) {
109 fields[fields.length - 1].setKey(val, key);
110 } catch (Exception e) {
111 throw new DBException(e);
113 return((new ConnWrapper<C, C>() {
114 protected C run(C val) throws Exception {
115 ps = c.prepareStatement(insorreplstmt);
116 for (DBFieldHandler f: fields) {
122 }).protect(cf, val));
124 public C remove(Object key) {
125 if (!(key instanceof String)) {
128 return((new ConnWrapper<C, String>() {
129 protected C run(String key) throws Exception {
130 ps = c.prepareStatement(delstmt);
131 ps.setString(1, key);
135 }).protect(cf, (String)key));