2 * This file is part of storage-units. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of storage-units,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
7 package de
.xn__ho_hia
.storage_unit
.mongodb
;
9 import java
.math
.BigInteger
;
11 import org
.bson
.BsonReader
;
12 import org
.bson
.BsonWriter
;
13 import org
.bson
.codecs
.Codec
;
14 import org
.bson
.codecs
.DecoderContext
;
15 import org
.bson
.codecs
.EncoderContext
;
16 import org
.eclipse
.jdt
.annotation
.NonNull
;
18 import de
.xn__ho_hia
.quality
.suppression
.CompilerWarnings
;
19 import de
.xn__ho_hia
.storage_unit
.StorageUnit
;
22 * Abstract implementation of a MongoDB {@link Codec} for {@link StorageUnit StorageUnits}.
24 * @see <a href="http://mongodb.github.io/mongo-java-driver/3.3/bson/codecs/">MongoDB Codec documentation</a>
26 abstract class AbstractStorageUnitCodec
implements Codec
<StorageUnit
<?
>> {
29 public final void encode(final BsonWriter writer
, final StorageUnit
<?
> value
, final EncoderContext encoderContext
) {
30 writer
.writeString(value
.inByte().toString());
34 @SuppressWarnings(CompilerWarnings
.UNCHECKED
)
35 public final Class
<StorageUnit
<?
>> getEncoderClass() {
36 return (Class
<StorageUnit
<?
>>) (Object
) StorageUnit
.class;
40 public final StorageUnit
<?
> decode(final BsonReader reader
, final DecoderContext decoderContext
) {
41 final String bsonValue
= reader
.readString();
42 final BigInteger value
= new BigInteger(bsonValue
);
43 return convertToStorageUnit(value
);
46 protected abstract StorageUnit
<?
> convertToStorageUnit(@NonNull BigInteger value
);