attempting to fix generics issues with MapWireHandler

This commit is contained in:
Rob Austin
2015-05-02 10:30:00 +01:00
parent 7b87ae21c7
commit 406a17c5f1
2 changed files with 67 additions and 26 deletions
@@ -0,0 +1,41 @@
/*
* Copyright 2014 Higher Frequency Trading
*
* http://www.higherfrequencytrading.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.wire.ValueIn;
import net.openhft.chronicle.wire.ValueOut;
import net.openhft.chronicle.wire.Wire;
import java.io.StreamCorruptedException;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
public interface MapWireHandler<K, V> {
void process(Wire in,
Wire out,
Map<K, V> map,
CharSequence csp,
BiConsumer<ValueOut, V> vToWire,
Function<ValueIn, K> kFromWire,
Function<ValueIn, V> vFromWire) throws StreamCorruptedException;
}
@@ -49,21 +49,22 @@ import static net.openhft.chronicle.wire.Wires.acquireStringBuilder;
/**
* @author Rob Austin.
*/
public class MapWireHandlerProcessor implements
MapWireHandler, Consumer<WireHandlers> {
public class MapWireHandlerProcessor<K, V> implements
MapWireHandler<K, V>,
Consumer<WireHandlers> {
private CharSequence csp;
private BiConsumer<ValueOut, Object> vToWire;
private Function<ValueIn, Object> wireToK;
private Function<ValueIn, Object> wireToV;
private BiConsumer<ValueOut, V> vToWire;
private Function<ValueIn, K> wireToK;
private Function<ValueIn, V> wireToV;
@Override
public <K, V> void process(Wire in, Wire out, Map<K, V> map, CharSequence csp, BiConsumer<ValueOut, V> vToWire, Function<ValueIn, K> kFromWire, Function<ValueIn, V> vFromWire) throws StreamCorruptedException {
this.vToWire = (BiConsumer<ValueOut, Object>) vToWire;
this.wireToK = (Function<ValueIn, Object>) kFromWire;
this.wireToV = (Function<ValueIn, Object>) vFromWire;
public void process(Wire in, Wire out, Map<K, V> map, CharSequence csp, BiConsumer<ValueOut, V> vToWire, Function<ValueIn, K> kFromWire, Function<ValueIn, V> vFromWire) throws StreamCorruptedException {
this.vToWire = vToWire;
this.wireToK = kFromWire;
this.wireToV = vFromWire;
try {
this.inWire = in;
@@ -74,7 +75,6 @@ public class MapWireHandlerProcessor implements
} catch (Exception e) {
LOG.error("", e);
}
}
@@ -161,7 +161,7 @@ public class MapWireHandlerProcessor implements
}
};
private Map map;
private Map<K, V> map;
public MapWireHandlerProcessor(@NotNull final Map<Long, CharSequence> cidToCsp) throws IOException {
this.cidToCsp = cidToCsp;
@@ -233,9 +233,9 @@ public class MapWireHandlerProcessor implements
valueIn.marshallable(wire -> {
final Params[] params = putIfAbsent.params();
final Object key = wireToK.apply(wire.read(params[0]));
final Object value = wireToV.apply(wire.read(params[1]));
final Object v = map.putIfAbsent(key, value);
final K key = wireToK.apply(wire.read(params[0]));
final V value = wireToV.apply(wire.read(params[1]));
final V v = map.putIfAbsent(key, value);
vToWire.accept(outWire.write(reply), v);
});
@@ -324,7 +324,7 @@ public class MapWireHandlerProcessor implements
}
if (get.contentEquals(eventName)) {
final Object key = wireToK.apply(valueIn);
final K key = wireToK.apply(valueIn);
vToWire.accept(outWire.write(reply),
map.get(key));
return;
@@ -335,8 +335,8 @@ public class MapWireHandlerProcessor implements
valueIn.marshallable(wire -> {
final Params[] params = getAndPut.params();
final Object key = wireToK.apply(wire.read(params[0]));
final Object value = wireToV.apply(wire.read(params[1]));
final K key = wireToK.apply(wire.read(params[0]));
final V value = wireToV.apply(wire.read(params[1]));
vToWire.accept(outWire.write(reply),
map.put(key, value));
@@ -348,7 +348,7 @@ public class MapWireHandlerProcessor implements
if (remove.contentEquals(eventName)) {
outWire.write(reply);
final Object key = wireToK.apply(valueIn);
final K key = wireToK.apply(valueIn);
vToWire.accept(outWire.write(reply), map.remove(key));
return;
}
@@ -357,8 +357,8 @@ public class MapWireHandlerProcessor implements
if (replace.contentEquals(eventName)) {
valueIn.marshallable(wire -> {
final Params[] params = replace.params();
final Object key = wireToK.apply(wire.read(params[0]));
final Object value = wireToV.apply(wire.read(params[1]));
final K key = wireToK.apply(wire.read(params[0]));
final V value = wireToV.apply(wire.read(params[1]));
vToWire.accept(outWire.write(reply),
map.replace(key, value));
@@ -372,9 +372,9 @@ public class MapWireHandlerProcessor implements
if (replaceWithOldAndNewValue.contentEquals(eventName)) {
valueIn.marshallable(wire -> {
final Params[] params = replaceWithOldAndNewValue.params();
final Object key = wireToK.apply(wire.read(params[0]));
final Object oldValue = wireToV.apply(wire.read(params[1]));
final Object newValue = wireToV.apply(wire.read(params[2]));
final K key = wireToK.apply(wire.read(params[0]));
final V oldValue = wireToV.apply(wire.read(params[1]));
final V newValue = wireToV.apply(wire.read(params[2]));
outWire.write(reply).bool(map.replace(key, oldValue, newValue));
});
@@ -384,8 +384,8 @@ public class MapWireHandlerProcessor implements
if (putIfAbsent.contentEquals(eventName)) {
valueIn.marshallable(wire -> {
final Params[] params = putIfAbsent.params();
final Object key = wireToK.apply(wire.read(params[0]));
final Object value = wireToV.apply(wire.read(params[1]));
final K key = wireToK.apply(wire.read(params[0]));
final V value = wireToV.apply(wire.read(params[1]));
vToWire.accept(outWire.write(reply),
map.putIfAbsent(key, value));