| 1 | package org.jtoolkit.essence.data.impl; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jetbrains.annotations.Nullable; |
| 5 | import org.jtoolkit.essence.app.net.NetObject; |
| 6 | import org.jtoolkit.essence.app.pojo.impl.DataValueClass; |
| 7 | import org.jtoolkit.essence.concurrency.ThreadSafe; |
| 8 | import org.jtoolkit.essence.concurrency.Concurrency; |
| 9 | import org.jtoolkit.essence.data.Store; |
| 10 | import org.jtoolkit.essence.data.Visitor; |
| 11 | import org.jtoolkit.essence.data.VisitorException; |
| 12 | import org.jtoolkit.essence.utils.IOUtils; |
| 13 | import org.jtoolkit.essence.utils.Pair; |
| 14 | |
| 15 | import java.util.Map; |
| 16 | import java.util.Set; |
| 17 | import java.util.Collection; |
| 18 | /* |
| 19 | Copyright 2006 Peter Lawrey |
| 20 | |
| 21 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 22 | you may not use this file except in compliance with the License. |
| 23 | You may obtain a copy of the License at |
| 24 | |
| 25 | http://www.apache.org/licenses/LICENSE-2.0 |
| 26 | |
| 27 | Unless required by applicable law or agreed to in writing, software |
| 28 | distributed under the License is distributed on an "AS IS" BASIS, |
| 29 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 30 | See the License for the specific language governing permissions and |
| 31 | limitations under the License. |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * @author Peter Lawrey |
| 36 | * @noinspection RedundantTypeArguments |
| 37 | */ |
| 38 | @ThreadSafe(Concurrency.CONCURRENT_READ_WRITE) |
| 39 | public class NetStore<K, V> extends AbstractStore<K, V> { |
| 40 | private final NetObject<Store<K, V>> netObject; |
| 41 | |
| 42 | public static <K, V> NetStore<K, V> newNetStore(@NotNull CollectionType collectionType, @NotNull NetObject<Store<K, V>> netObject) { |
| 43 | Pair<Class<K>, Class<V>> classes = netObject.call("getClasses"); |
| 44 | assert classes != null; |
| 45 | DataValueClass<K> keyClass = DataValueClass.acquire(classes.first); |
| 46 | DataValueClass<V> valueClass = DataValueClass.acquire(classes.second); |
| 47 | return new NetStore<K, V>(collectionType, keyClass, valueClass, netObject); |
| 48 | } |
| 49 | |
| 50 | private NetStore(@NotNull CollectionType collectionType, @NotNull DataValueClass<K> keyClass, @NotNull DataValueClass<V> valueClass, @NotNull NetObject<Store<K, V>> netObject) { |
| 51 | super(netObject.getName(), collectionType, keyClass, valueClass, netObject.getListenerSet(), ReadMode.MINIMAL, PersistMode.SYNC); |
| 52 | this.netObject = netObject; |
| 53 | } |
| 54 | |
| 55 | protected boolean add(@NotNull V value) { |
| 56 | return netObject.<Boolean>call("add", value); |
| 57 | } |
| 58 | |
| 59 | protected boolean containsKey(@NotNull Object key) { |
| 60 | return netObject.<Boolean>call("containsKey", key); |
| 61 | } |
| 62 | |
| 63 | protected boolean containsValue(@NotNull Object value) { |
| 64 | return netObject.<Boolean>call("containsValue", value); |
| 65 | } |
| 66 | |
| 67 | protected void clear() { |
| 68 | netObject.call("clear"); |
| 69 | } |
| 70 | |
| 71 | @NotNull public Map<K, V> asMap() { |
| 72 | Map<K, V> map = netObject.call("asMap"); |
| 73 | assert map != null; |
| 74 | return map; |
| 75 | } |
| 76 | |
| 77 | @Nullable protected V first() { |
| 78 | return netObject.<V>call("first"); |
| 79 | } |
| 80 | |
| 81 | @Nullable protected K firstKey() { |
| 82 | return netObject.<K>call("firstKey"); |
| 83 | } |
| 84 | |
| 85 | @Nullable protected V get(@NotNull Object key) { |
| 86 | return netObject.<V>call("get", key); |
| 87 | } |
| 88 | |
| 89 | @NotNull protected Set<K> keySet() { |
| 90 | Set<K> keySet = netObject.call("keySet"); |
| 91 | assert keySet != null; |
| 92 | return keySet; |
| 93 | } |
| 94 | |
| 95 | @Nullable protected V doPut(@NotNull K key, @NotNull V value, boolean ifAbsent, boolean ifPresent) { |
| 96 | return netObject.<V>call("doPut", key, value, ifAbsent, ifPresent); |
| 97 | } |
| 98 | |
| 99 | protected V doRemove(@NotNull K key, boolean byValue, V value) { |
| 100 | return netObject.<V>call("doRemove", key, byValue, value); |
| 101 | } |
| 102 | |
| 103 | protected void put0(K key, V value) { |
| 104 | throw new UnsupportedOperationException(); |
| 105 | } |
| 106 | |
| 107 | @Nullable protected V remove(@NotNull Object key) { |
| 108 | return netObject.<V>call("remove", key); |
| 109 | } |
| 110 | |
| 111 | @Nullable protected V removeFirst() { |
| 112 | return netObject.<V>call("removeFirst"); |
| 113 | } |
| 114 | |
| 115 | protected int getSize() { |
| 116 | return netObject.<Integer>call("getSize"); |
| 117 | } |
| 118 | |
| 119 | @NotNull protected Collection<V> values() { |
| 120 | Collection<V> vs = netObject.call("values"); |
| 121 | assert vs != null; |
| 122 | return vs; |
| 123 | } |
| 124 | |
| 125 | protected Collection<V> orderedValues() { |
| 126 | Collection<V> vs = netObject.call("orderedValues"); |
| 127 | assert vs != null; |
| 128 | return vs; |
| 129 | } |
| 130 | |
| 131 | public void close() { |
| 132 | IOUtils.close(netObject); |
| 133 | } |
| 134 | |
| 135 | public <R> R visit(@NotNull Visitor<Store<K, V>, R> visitor) throws VisitorException { |
| 136 | return netObject.visit(visitor); |
| 137 | } |
| 138 | } |