|
| 1 | +/* |
| 2 | + * Copyright 2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.r2dbc.postgresql.codec; |
| 18 | + |
| 19 | +import io.netty.buffer.ByteBuf; |
| 20 | +import io.netty.buffer.ByteBufAllocator; |
| 21 | +import io.r2dbc.postgresql.client.EncodedParameter; |
| 22 | +import io.r2dbc.postgresql.message.Format; |
| 23 | +import io.r2dbc.postgresql.util.Assert; |
| 24 | + |
| 25 | +import java.util.function.Function; |
| 26 | + |
| 27 | +class IntegerCodecDelegate<T> extends AbstractCodec<T> { |
| 28 | + |
| 29 | + private final IntegerCodec delegate; |
| 30 | + private final Function<T, Integer> toIntegerConverter; |
| 31 | + private final Function<Integer, T> fromIntegerConverter; |
| 32 | + |
| 33 | + IntegerCodecDelegate(Class<T> type, ByteBufAllocator byteBufAllocator, Function<T,Integer> toIntegerConverter, Function<Integer, T> fromIntegerConverter) { |
| 34 | + super(type); |
| 35 | + this.delegate = new IntegerCodec(byteBufAllocator); |
| 36 | + this.toIntegerConverter = toIntegerConverter; |
| 37 | + this.fromIntegerConverter = fromIntegerConverter; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + boolean doCanDecode(PostgresqlObjectId type, Format format) { |
| 42 | + return delegate.doCanDecode(type, format); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + T doDecode(ByteBuf buffer, PostgresTypeIdentifier dataType, Format format, Class<? extends T> type) { |
| 47 | + final Integer number = delegate.doDecode(buffer, dataType, format, Integer.TYPE); |
| 48 | + return fromIntegerConverter.apply(number); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + EncodedParameter doEncode(T value) { |
| 53 | + Assert.requireNonNull(value, "value must not be null"); |
| 54 | + return delegate.doEncode(toIntegerConverter.apply(value)); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + EncodedParameter doEncode(T value, PostgresTypeIdentifier dataType) { |
| 59 | + Assert.requireNonNull(value, "value must not be null"); |
| 60 | + Assert.requireNonNull(dataType, "dataType must not be null"); |
| 61 | + return delegate.doEncode(toIntegerConverter.apply(value), dataType); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Iterable<? extends PostgresTypeIdentifier> getDataTypes() { |
| 66 | + return delegate.getDataTypes(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public EncodedParameter encodeNull() { |
| 71 | + return delegate.encodeNull(); |
| 72 | + } |
| 73 | +} |
0 commit comments