mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-201 implemented LongValue special case marshallers
This commit is contained in:
+23
-3
@@ -23,12 +23,24 @@ import net.openhft.chronicle.hash.serialization.BytesReader;
|
||||
import net.openhft.chronicle.hash.serialization.Hasher;
|
||||
import net.openhft.chronicle.hash.serialization.SizeMarshaller;
|
||||
import net.openhft.lang.io.Bytes;
|
||||
import net.openhft.lang.model.Byteable;
|
||||
import net.openhft.lang.model.DataValueClasses;
|
||||
import net.openhft.lang.values.LongValue;
|
||||
|
||||
public enum LongValueMarshaller
|
||||
public class LongValueKeyMarshaller
|
||||
implements BytesInterop<LongValue>, BytesReader<LongValue>, SizeMarshaller {
|
||||
INSTANCE;
|
||||
private static final long serialVersionUID = 0L;
|
||||
|
||||
public static final LongValueKeyMarshaller INSTANCE = new LongValueKeyMarshaller();
|
||||
|
||||
private static final Class<LongValue> heapLongValueClass =
|
||||
DataValueClasses.heapClassFor(LongValue.class);
|
||||
|
||||
LongValueKeyMarshaller() {}
|
||||
|
||||
private Object readResolve() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long size(LongValue e) {
|
||||
@@ -67,11 +79,19 @@ public enum LongValueMarshaller
|
||||
|
||||
@Override
|
||||
public LongValue read(Bytes bytes, long size) {
|
||||
return read(bytes, size, DataValueClasses.newInstance(LongValue.class));
|
||||
return read(bytes, size, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongValue read(Bytes bytes, long size, LongValue toReuse) {
|
||||
if (toReuse == null ||
|
||||
(toReuse instanceof Byteable && ((Byteable) toReuse).bytes() == null)) {
|
||||
try {
|
||||
toReuse = heapLongValueClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
toReuse.setValue(bytes.readLong());
|
||||
return toReuse;
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.hash.serialization.internal;
|
||||
|
||||
import net.openhft.lang.io.Bytes;
|
||||
import net.openhft.lang.model.Byteable;
|
||||
import net.openhft.lang.model.DataValueClasses;
|
||||
import net.openhft.lang.values.LongValue;
|
||||
|
||||
public final class LongValueValueMarshaller extends LongValueKeyMarshaller {
|
||||
private static final long serialVersionUID = 0L;
|
||||
|
||||
public static final LongValueValueMarshaller INSTANCE = new LongValueValueMarshaller();
|
||||
|
||||
private static final Class<LongValue> directLongValueClass =
|
||||
DataValueClasses.directClassFor(LongValue.class);
|
||||
|
||||
private LongValueValueMarshaller() {}
|
||||
|
||||
private Object readResolve() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongValue read(Bytes bytes, long size, LongValue toReuse) {
|
||||
if (!(toReuse instanceof Byteable)) {
|
||||
try {
|
||||
toReuse = directLongValueClass.newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
Byteable value = (Byteable) toReuse;
|
||||
ByteableMarshaller.setBytesAndOffset(value, bytes);
|
||||
bytes.skip(8L);
|
||||
return toReuse;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user