-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Core, API, Spark: Add FileContent.fromId #15953
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
Changes from all commits
30e4569
7e525cf
4cabd8d
6622e95
956c870
85e0e71
6a8a72e
b8921ba
8dbe11a
58e7505
a0f33b3
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 |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ public enum FileContent { | |
| DATA_MANIFEST(3), | ||
| DELETE_MANIFEST(4); | ||
|
|
||
| private static final FileContent[] VALUES = FileContent.values(); | ||
|
|
||
| private final int id; | ||
|
|
||
| FileContent(int id) { | ||
|
|
@@ -35,4 +37,8 @@ public enum FileContent { | |
| public int id() { | ||
| return id; | ||
| } | ||
|
|
||
| public static FileContent fromId(int id) { | ||
| return VALUES[id]; | ||
|
Contributor
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. i wonder if we should throw an IllegalArg here, instead of letting it have an ArrayOutOfBound, thoughts ?
Contributor
Author
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. Good idea. Done.
Contributor
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. I don't think that I agree with this. The reason to use a values array is to have a quick lookup, since this is resolved in a tight loop. Having an extra check basically to throw the exception as a different class doesn't seem worth it.
Contributor
Author
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. That makes sense. Reverted. |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.iceberg; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import java.util.stream.IntStream; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.EnumSource; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
|
|
||
| class TestFileContent { | ||
|
|
||
| @ParameterizedTest | ||
| @EnumSource(FileContent.class) | ||
| void fromId(FileContent content) { | ||
| assertThat(FileContent.fromId(content.id())).isEqualTo(content); | ||
| } | ||
|
|
||
| static IntStream invalidContentTypeIds() { | ||
| return IntStream.of(-1, FileContent.values().length); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource("invalidContentTypeIds") | ||
| void fromIdInvalid(int id) { | ||
| assertThatThrownBy(() -> FileContent.fromId(id)) | ||
| .isInstanceOf(ArrayIndexOutOfBoundsException.class) | ||
| .hasMessageContaining(String.valueOf(id)); | ||
| } | ||
| } |
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.
that's a nice trick