-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
platform bytes class #204
platform bytes class #204
Changes from all commits
e08f6ab
597152f
6fd802f
6c4e49f
3638c93
96969a7
6d4f0ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2023 Toast, 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 protokt.v1 | ||
|
||
abstract class AbstractBytes internal constructor( | ||
internal val value: ByteArray | ||
) { | ||
val bytes | ||
get() = clone(value) | ||
|
||
fun isNotEmpty() = | ||
value.isNotEmpty() | ||
|
||
fun isEmpty() = | ||
value.isEmpty() | ||
|
||
fun toBytesSlice() = | ||
BytesSlice(value) | ||
|
||
final override fun equals(other: Any?) = | ||
other is Bytes && value.contentEquals(other.value) | ||
|
||
final override fun hashCode() = | ||
value.contentHashCode() | ||
|
||
final override fun toString() = | ||
value.contentToString() | ||
|
||
internal companion object { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used to hold common code in implementation companions rather than exposing a new public class. The same could be done with AbstractBytes but then the implementation would have to call into this object rather than inheriting the correct behavior. This pattern is already used in AbstractKtMessage and AbstractKtDeserializer, so it feels OK to be consistent with those. |
||
private val EMPTY = Bytes(ByteArray(0)) | ||
|
||
fun empty() = | ||
EMPTY | ||
|
||
fun from(bytes: ByteArray) = | ||
Bytes(clone(bytes)) | ||
|
||
fun from(message: KtMessage) = | ||
Bytes(message.serialize()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be the only way to share this code.