ctubbsii commented on code in PR #3148: URL: https://github.com/apache/accumulo/pull/3148#discussion_r1063614739
########## core/src/main/java/org/apache/accumulo/core/file/FilePrefix.java: ########## @@ -0,0 +1,50 @@ +/* + * 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 + * + * https://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.accumulo.core.file; + +public enum FilePrefix { + + BULK_IMPORT("I"), MINOR_COMPACTION("F"), MAJOR_COMPACTION("C"), MAJOR_COMPACTION_ALL_FILES("A"); + + String prefix; + + FilePrefix(String prefix) { + this.prefix = prefix; + } + + public static FilePrefix fromPrefix(String prefix) { + switch (prefix) { + case "I": + return BULK_IMPORT; + case "F": + return MINOR_COMPACTION; + case "C": + return MAJOR_COMPACTION; + case "A": + return MAJOR_COMPACTION_ALL_FILES; + default: + throw new IllegalArgumentException("Unknown prefix type: " + prefix); + } + } Review Comment: A dynamic implementatoin that uses the `prefix` field from the enums would be better, so switch cases don't need to be manually maintained when new prefixes are added. It's also probably about the same performance, given there's approximately the same number of comparisons, and the number of enum values is quite small. ```java public static FilePrefix fromPrefix(String prefix) { for (FilePrefix p : FilePrefix.values()) { if (p.prefix.equals(prefix)) { return p; } } throw new IllegalArgumentException("Unknown prefix type: " + prefix); } ``` Here's a version with Streams: ```java public static FilePrefix fromPrefix(String prefix) { return Stream.of(FilePrefix.values()).filter(p -> p.prefix.equals(prefix)).findAny() .orElseThrow(() -> new IllegalArgumentException("Unknown prefix type: " + prefix)); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
