| 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.pojo.DataValue; |
| 6 | import org.jtoolkit.essence.app.pojo.Datable; |
| 7 | import static org.jtoolkit.essence.app.pojo.DatableUtils.readObject; |
| 8 | import static org.jtoolkit.essence.app.pojo.DatableUtils.writeObject; |
| 9 | import org.jtoolkit.essence.data.Event; |
| 10 | import org.jtoolkit.essence.data.Store; |
| 11 | |
| 12 | import java.io.DataInput; |
| 13 | import java.io.DataOutput; |
| 14 | import java.io.IOException; |
| 15 | |
| 16 | /* |
| 17 | Copyright 2006 Peter Lawrey |
| 18 | |
| 19 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 | you may not use this file except in compliance with the License. |
| 21 | You may obtain a copy of the License at |
| 22 | |
| 23 | http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | |
| 25 | Unless required by applicable law or agreed to in writing, software |
| 26 | distributed under the License is distributed on an "AS IS" BASIS, |
| 27 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | See the License for the specific language governing permissions and |
| 29 | limitations under the License. |
| 30 | */ |
| 31 | |
| 32 | /** |
| 33 | * @author Peter Lawrey |
| 34 | * @noinspection unchecked |
| 35 | */ |
| 36 | public class StoreEvent<K, V> extends DataValue implements Event, Datable { |
| 37 | private final boolean updated; |
| 38 | private final K key; |
| 39 | private final V value; |
| 40 | |
| 41 | public StoreEvent(boolean updated, @NotNull K key, @Nullable V value) { |
| 42 | this.updated = updated; |
| 43 | this.key = key; |
| 44 | this.value = value; |
| 45 | } |
| 46 | |
| 47 | public void notifyListener(@NotNull Listener l) { |
| 48 | if (l instanceof Store.StoreListener) { |
| 49 | if (updated) |
| 50 | ((Store.StoreListener) l).onUpdate(key, value); |
| 51 | else |
| 52 | ((Store.StoreListener) l).onRemoved(key, value); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // used to deserialize. |
| 57 | @SuppressWarnings({"UnusedDeclaration"}) StoreEvent(@NotNull DataInput in) throws IOException { |
| 58 | updated = in.readBoolean(); |
| 59 | key = (K) readObject(in); |
| 60 | value = (V) readObject(in); |
| 61 | } |
| 62 | |
| 63 | public void writeData(@NotNull DataOutput out) throws IOException { |
| 64 | out.writeBoolean(updated); |
| 65 | writeObject(out, key); |
| 66 | writeObject(out, value); |
| 67 | } |
| 68 | } |