forked from airlift/drift
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementing OptionalFloatThriftCodec and float coercion
- Loading branch information
1 parent
d6eed85
commit 690c432
Showing
5 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...dec/src/main/java/com/facebook/drift/codec/internal/builtin/OptionalFloatThriftCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2017 Facebook, Inc. | ||
* | ||
* 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 com.facebook.drift.codec.internal.builtin; | ||
|
||
import com.facebook.drift.codec.ThriftCodec; | ||
import com.facebook.drift.codec.metadata.ThriftType; | ||
import com.facebook.drift.protocol.TProtocolReader; | ||
import com.facebook.drift.protocol.TProtocolWriter; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
import java.util.Optional; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
@Immutable | ||
public class OptionalFloatThriftCodec | ||
implements ThriftCodec<Optional<Float>> | ||
{ | ||
@Override | ||
public ThriftType getType() | ||
{ | ||
return new ThriftType(ThriftType.FLOAT, new TypeToken<Optional<Float>>() {}.getType(), Optional.empty()); | ||
} | ||
|
||
@Override | ||
public Optional<Float> read(TProtocolReader protocol) | ||
throws Exception | ||
{ | ||
requireNonNull(protocol, "protocol is null"); | ||
return Optional.of(protocol.readFloat()); | ||
} | ||
|
||
@Override | ||
public void write(Optional<Float> value, TProtocolWriter protocol) | ||
throws Exception | ||
{ | ||
requireNonNull(value, "value is null"); | ||
requireNonNull(protocol, "protocol is null"); | ||
|
||
// write can not be called with a missing value, and instead the write should be skipped | ||
// after check the result from isNull | ||
protocol.writeFloat(value.orElseThrow((() -> new IllegalArgumentException("value is not present")))); | ||
} | ||
|
||
@Override | ||
public boolean isNull(Optional<Float> value) | ||
{ | ||
return value == null || !value.isPresent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters