001    /*
002     * Copyright (C) 2008-2009 Holger Arndt, A. Naegele and M. Bundschus
003     *
004     * This file is part of the Java Data Mining Package (JDMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * JDMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * JDMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with JDMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.jdmp.jetty.collections;
025    
026    import java.net.URL;
027    import java.util.Collection;
028    import java.util.Map;
029    import java.util.Set;
030    
031    import org.jdmp.jetty.JettyObjectClient;
032    import org.ujmp.core.exceptions.MatrixException;
033    
034    public class JettyMapClient<K, V> implements Map<K, V> {
035    
036            private JettyObjectClient client = null;
037    
038            public JettyMapClient(URL url) {
039                    client = new JettyObjectClient(url);
040            }
041    
042            @Override
043            public void clear() {
044                    try {
045                            client.execute("clear");
046                    } catch (Exception e) {
047                            throw new MatrixException(e);
048                    }
049            }
050    
051            @Override
052            public boolean containsKey(Object key) {
053                    try {
054                            return (Boolean) client.execute("containsKey", key);
055                    } catch (Exception e) {
056                            throw new MatrixException(e);
057                    }
058            }
059    
060            @Override
061            public boolean containsValue(Object value) {
062                    try {
063                            return (Boolean) client.execute("containsValue", value);
064                    } catch (Exception e) {
065                            throw new MatrixException(e);
066                    }
067            }
068    
069            @SuppressWarnings("unchecked")
070            @Override
071            public Set<java.util.Map.Entry<K, V>> entrySet() {
072                    try {
073                            return (Set<java.util.Map.Entry<K, V>>) client.execute("entrySet");
074                    } catch (Exception e) {
075                            throw new MatrixException(e);
076                    }
077            }
078    
079            @SuppressWarnings("unchecked")
080            @Override
081            public V get(Object key) {
082                    try {
083                            return (V) client.execute("get", key);
084                    } catch (Exception e) {
085                            throw new MatrixException(e);
086                    }
087            }
088    
089            @Override
090            public boolean isEmpty() {
091                    try {
092                            return (Boolean) client.execute("isEmpty");
093                    } catch (Exception e) {
094                            throw new MatrixException(e);
095                    }
096            }
097    
098            @SuppressWarnings("unchecked")
099            @Override
100            public Set<K> keySet() {
101                    try {
102                            return (Set<K>) client.execute("keySet");
103                    } catch (Exception e) {
104                            throw new MatrixException(e);
105                    }
106            }
107    
108            @SuppressWarnings("unchecked")
109            @Override
110            public V put(K key, V value) {
111                    try {
112                            return (V) client.execute("put", key, value);
113                    } catch (Exception e) {
114                            throw new MatrixException(e);
115                    }
116            }
117    
118            @Override
119            public void putAll(Map<? extends K, ? extends V> m) {
120                    try {
121                            client.execute("putAll", m);
122                    } catch (Exception e) {
123                            throw new MatrixException(e);
124                    }
125            }
126    
127            @SuppressWarnings("unchecked")
128            @Override
129            public V remove(Object key) {
130                    try {
131                            return (V) client.execute("remove", key);
132                    } catch (Exception e) {
133                            throw new MatrixException(e);
134                    }
135            }
136    
137            @Override
138            public int size() {
139                    try {
140                            return (Integer) client.execute("size");
141                    } catch (Exception e) {
142                            throw new MatrixException(e);
143                    }
144            }
145    
146            @SuppressWarnings("unchecked")
147            @Override
148            public Collection<V> values() {
149                    try {
150                            return (Collection<V>) client.execute("values");
151                    } catch (Exception e) {
152                            throw new MatrixException(e);
153                    }
154            }
155    
156    }