All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cloudfoundry.multiapps.controller.persistence.model.ImmutableFileInfo Maven / Gradle / Ivy

package org.cloudfoundry.multiapps.controller.persistence.model;

import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Immutable implementation of {@link FileInfo}.
 * 

* Use the builder to create immutable instances: * {@code ImmutableFileInfo.builder()}. */ @SuppressWarnings({"all"}) public final class ImmutableFileInfo extends FileInfo { private final BigInteger size; private final String digest; private final String digestAlgorithm; private final File file; private ImmutableFileInfo( BigInteger size, String digest, String digestAlgorithm, File file) { this.size = size; this.digest = digest; this.digestAlgorithm = digestAlgorithm; this.file = file; } /** * @return The value of the {@code size} attribute */ @Override public BigInteger getSize() { return size; } /** * @return The value of the {@code digest} attribute */ @Override public String getDigest() { return digest; } /** * @return The value of the {@code digestAlgorithm} attribute */ @Override public String getDigestAlgorithm() { return digestAlgorithm; } /** * @return The value of the {@code file} attribute */ @Override public File getFile() { return file; } /** * Copy the current immutable object by setting a value for the {@link FileInfo#getSize() size} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for size * @return A modified copy of the {@code this} object */ public final ImmutableFileInfo withSize(BigInteger value) { BigInteger newValue = Objects.requireNonNull(value, "size"); if (this.size.equals(newValue)) return this; return new ImmutableFileInfo(newValue, this.digest, this.digestAlgorithm, this.file); } /** * Copy the current immutable object by setting a value for the {@link FileInfo#getDigest() digest} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for digest * @return A modified copy of the {@code this} object */ public final ImmutableFileInfo withDigest(String value) { String newValue = Objects.requireNonNull(value, "digest"); if (this.digest.equals(newValue)) return this; return new ImmutableFileInfo(this.size, newValue, this.digestAlgorithm, this.file); } /** * Copy the current immutable object by setting a value for the {@link FileInfo#getDigestAlgorithm() digestAlgorithm} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for digestAlgorithm * @return A modified copy of the {@code this} object */ public final ImmutableFileInfo withDigestAlgorithm(String value) { String newValue = Objects.requireNonNull(value, "digestAlgorithm"); if (this.digestAlgorithm.equals(newValue)) return this; return new ImmutableFileInfo(this.size, this.digest, newValue, this.file); } /** * Copy the current immutable object by setting a value for the {@link FileInfo#getFile() file} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for file * @return A modified copy of the {@code this} object */ public final ImmutableFileInfo withFile(File value) { if (this.file == value) return this; File newValue = Objects.requireNonNull(value, "file"); return new ImmutableFileInfo(this.size, this.digest, this.digestAlgorithm, newValue); } /** * This instance is equal to all instances of {@code ImmutableFileInfo} that have equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(Object another) { if (this == another) return true; return another instanceof ImmutableFileInfo && equalTo(0, (ImmutableFileInfo) another); } private boolean equalTo(int synthetic, ImmutableFileInfo another) { return size.equals(another.size) && digest.equals(another.digest) && digestAlgorithm.equals(another.digestAlgorithm) && file.equals(another.file); } /** * Computes a hash code from attributes: {@code size}, {@code digest}, {@code digestAlgorithm}, {@code file}. * @return hashCode value */ @Override public int hashCode() { int h = 5381; h += (h << 5) + size.hashCode(); h += (h << 5) + digest.hashCode(); h += (h << 5) + digestAlgorithm.hashCode(); h += (h << 5) + file.hashCode(); return h; } /** * Prints the immutable value {@code FileInfo} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "FileInfo{" + "size=" + size + ", digest=" + digest + ", digestAlgorithm=" + digestAlgorithm + ", file=" + file + "}"; } /** * Creates an immutable copy of a {@link FileInfo} 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 FileInfo instance */ public static ImmutableFileInfo copyOf(FileInfo instance) { if (instance instanceof ImmutableFileInfo) { return (ImmutableFileInfo) instance; } return ImmutableFileInfo.builder() .from(instance) .build(); } /** * Creates a builder for {@link ImmutableFileInfo ImmutableFileInfo}. *

   * ImmutableFileInfo.builder()
   *    .size(java.math.BigInteger) // required {@link FileInfo#getSize() size}
   *    .digest(String) // required {@link FileInfo#getDigest() digest}
   *    .digestAlgorithm(String) // required {@link FileInfo#getDigestAlgorithm() digestAlgorithm}
   *    .file(java.io.File) // required {@link FileInfo#getFile() file}
   *    .build();
   * 
* @return A new ImmutableFileInfo builder */ public static ImmutableFileInfo.Builder builder() { return new ImmutableFileInfo.Builder(); } /** * Builds instances of type {@link ImmutableFileInfo ImmutableFileInfo}. * Initialize attributes and then invoke the {@link #build()} method to create an * immutable instance. *

{@code Builder} is not thread-safe and generally should not be stored in a field or collection, * but instead used immediately to create instances. */ public static final class Builder { private static final long INIT_BIT_SIZE = 0x1L; private static final long INIT_BIT_DIGEST = 0x2L; private static final long INIT_BIT_DIGEST_ALGORITHM = 0x4L; private static final long INIT_BIT_FILE = 0x8L; private long initBits = 0xfL; private BigInteger size; private String digest; private String digestAlgorithm; private File file; private Builder() { } /** * Fill a builder with attribute values from the provided {@code FileInfo} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(FileInfo instance) { Objects.requireNonNull(instance, "instance"); this.size(instance.getSize()); this.digest(instance.getDigest()); this.digestAlgorithm(instance.getDigestAlgorithm()); this.file(instance.getFile()); return this; } /** * Initializes the value for the {@link FileInfo#getSize() size} attribute. * @param size The value for size * @return {@code this} builder for use in a chained invocation */ public final Builder size(BigInteger size) { this.size = Objects.requireNonNull(size, "size"); initBits &= ~INIT_BIT_SIZE; return this; } /** * Initializes the value for the {@link FileInfo#getDigest() digest} attribute. * @param digest The value for digest * @return {@code this} builder for use in a chained invocation */ public final Builder digest(String digest) { this.digest = Objects.requireNonNull(digest, "digest"); initBits &= ~INIT_BIT_DIGEST; return this; } /** * Initializes the value for the {@link FileInfo#getDigestAlgorithm() digestAlgorithm} attribute. * @param digestAlgorithm The value for digestAlgorithm * @return {@code this} builder for use in a chained invocation */ public final Builder digestAlgorithm(String digestAlgorithm) { this.digestAlgorithm = Objects.requireNonNull(digestAlgorithm, "digestAlgorithm"); initBits &= ~INIT_BIT_DIGEST_ALGORITHM; return this; } /** * Initializes the value for the {@link FileInfo#getFile() file} attribute. * @param file The value for file * @return {@code this} builder for use in a chained invocation */ public final Builder file(File file) { this.file = Objects.requireNonNull(file, "file"); initBits &= ~INIT_BIT_FILE; return this; } /** * Builds a new {@link ImmutableFileInfo ImmutableFileInfo}. * @return An immutable instance of FileInfo * @throws java.lang.IllegalStateException if any required attributes are missing */ public ImmutableFileInfo build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ImmutableFileInfo(size, digest, digestAlgorithm, file); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_SIZE) != 0) attributes.add("size"); if ((initBits & INIT_BIT_DIGEST) != 0) attributes.add("digest"); if ((initBits & INIT_BIT_DIGEST_ALGORITHM) != 0) attributes.add("digestAlgorithm"); if ((initBits & INIT_BIT_FILE) != 0) attributes.add("file"); return "Cannot build FileInfo, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy