danielcweeks commented on code in PR #12930: URL: https://github.com/apache/iceberg/pull/12930#discussion_r2073905002
########## core/src/main/java/org/apache/iceberg/io/ImmutableStorageCredential.java: ########## @@ -0,0 +1,359 @@ +/* + * 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.io; + +import com.google.errorprone.annotations.CanIgnoreReturnValue; +import com.google.errorprone.annotations.Var; +import java.io.ObjectStreamException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; +import javax.annotation.concurrent.Immutable; +import javax.annotation.concurrent.NotThreadSafe; +import org.apache.iceberg.util.SerializableMap; +import org.immutables.value.Generated; + +/** + * This was copied from the generated Immutable implementation of {@link StorageCredential} with the + * one difference that the underlying Map is not unmodifiable but rather a {@link SerializableMap} + * so that Ser/De properly works with Kryo. + * + * <p>Use the builder to create immutable instances: {@code ImmutableStorageCredential.builder()}. + */ +@Generated(from = "StorageCredential", generator = "Immutables") +@SuppressWarnings({"all"}) +@ParametersAreNonnullByDefault +@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor") +@Immutable +@CheckReturnValue +public final class ImmutableStorageCredential implements StorageCredential { + private final String prefix; + private final Map<String, String> config; + + private ImmutableStorageCredential(String prefix, Map<String, String> config) { + this.prefix = prefix; + this.config = config; + } + + /** + * @return The value of the {@code prefix} attribute + */ + @Override + public String prefix() { + return prefix; + } + + /** + * @return The value of the {@code config} attribute + */ + @Override + public Map<String, String> config() { + return config; + } + + /** + * Copy the current immutable object by setting a value for the {@link StorageCredential#prefix() + * prefix} attribute. An equals check used to prevent copying of the same value by returning + * {@code this}. + * + * @param value A new value for prefix + * @return A modified copy of the {@code this} object + */ + public final ImmutableStorageCredential withPrefix(String value) { + String newValue = Objects.requireNonNull(value, "prefix"); + if (this.prefix.equals(newValue)) return this; + return validate(new ImmutableStorageCredential(newValue, this.config)); + } + + /** + * Copy the current immutable object by replacing the {@link StorageCredential#config() config} + * map with the specified map. Nulls are not permitted as keys or values. A shallow reference + * equality check is used to prevent copying of the same value by returning {@code this}. + * + * @param entries The entries to be added to the config map + * @return A modified copy of {@code this} object + */ + public final ImmutableStorageCredential withConfig(Map<String, ? extends String> entries) { + if (this.config == entries) return this; + Map<String, String> newValue = createSerializableMap(true, false, entries); + return validate(new ImmutableStorageCredential(this.prefix, newValue)); + } + + /** + * This instance is equal to all instances of {@code ImmutableStorageCredential} that have equal + * attribute values. + * + * @return {@code true} if {@code this} is equal to {@code another} instance + */ + @Override + public boolean equals(@Nullable Object another) { + if (this == another) return true; + return another instanceof ImmutableStorageCredential + && equalTo(0, (ImmutableStorageCredential) another); + } + + private boolean equalTo(int synthetic, ImmutableStorageCredential another) { + return prefix.equals(another.prefix) && config.equals(another.config); + } + + /** + * Computes a hash code from attributes: {@code prefix}, {@code config}. + * + * @return hashCode value + */ + @Override + public int hashCode() { + @Var int h = 5381; + h += (h << 5) + prefix.hashCode(); + h += (h << 5) + config.hashCode(); + return h; + } + + /** + * Prints the immutable value {@code StorageCredential} with attribute values. + * + * @return A string representation of the value + */ + @Override + public String toString() { + return "StorageCredential{" + "prefix=" + prefix + ", config=" + config + "}"; + } + + private static ImmutableStorageCredential validate(ImmutableStorageCredential instance) { + instance.validate(); + return instance; + } + + /** + * Creates an immutable copy of a {@link StorageCredential} value. Uses accessors to get values to + * initialize the new immutable instance. If an instance is already immutable, it is returned as + * is. + * + * @param instance The instance to copy + * @return A copied immutable StorageCredential instance + */ + public static ImmutableStorageCredential copyOf(StorageCredential instance) { + if (instance instanceof ImmutableStorageCredential) { + return (ImmutableStorageCredential) instance; + } + return ImmutableStorageCredential.builder().from(instance).build(); + } + + private Object readResolve() throws ObjectStreamException { + return validate(this); + } + + /** + * Creates a builder for {@link ImmutableStorageCredential ImmutableStorageCredential}. + * + * <pre> + * ImmutableStorageCredential.builder() + * .prefix(String) // required {@link StorageCredential#prefix() prefix} + * .putConfig|putAllConfig(String => String) // {@link StorageCredential#config() config} mappings + * .build(); + * </pre> + * + * @return A new ImmutableStorageCredential builder + */ + public static ImmutableStorageCredential.Builder builder() { + return new ImmutableStorageCredential.Builder(); + } + + /** + * Builds instances of type {@link ImmutableStorageCredential ImmutableStorageCredential}. + * Initialize attributes and then invoke the {@link #build()} method to create an immutable + * instance. + * + * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or + * collection, but instead used immediately to create instances.</em> + */ + @Generated(from = "StorageCredential", generator = "Immutables") + @NotThreadSafe + public static final class Builder { + private static final long INIT_BIT_PREFIX = 0x1L; + private long initBits = 0x1L; Review Comment: I feel like we could swap out the bit checks for a precondition check of the prefix. There's only one value, so this is a lot of complexity to carry forward. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org