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

com.github.jscancella.domain.FetchItem Maven / Gradle / Ivy

Go to download

This is a software library intended to support the creation, manipulation, and validation of "bags" from the bagit specification. It currently supports version 0.93 through 1.0.

There is a newer version: 5.2
Show newest version
package com.github.jscancella.domain;

import java.net.URI;
import java.nio.file.Path;
import java.util.Objects;

import com.github.jscancella.domain.internal.EclipseGenerated;

/**
 * An individual item to fetch as specified by 
 * https://tools.ietf.org/html/draft-kunze-bagit-13#section-2.2.3
 */
public final class FetchItem {
  /**
   * The uri from which the item can be downloaded
   */
  private final URI uri;
  
  /**
   * The length of the file in octets
   */
  private final Long length; 
  
  /**
   * The path where the fetched item should be put
   */
  private final Path path;
  
  private transient String cachedString;
  
  /**
   * 
   * @param uri the {@link URI} of the file
   * @param length the file length in bytes, -1 or null to not specify the length
   * @param path the path in the bag where the file belongs
   */
  public FetchItem(final URI uri, final Long length, final Path path){
    this.uri = URI.create(uri.toString());
    this.length = length;
    this.path = path;
    this.cachedString = internalToString();
  }
  
  private String internalToString() {
    final StringBuilder builder = new StringBuilder();
    builder.append(uri).append(' ');
    
    if(length == null || length < 0){
      builder.append("- ");
    }
    else{
      builder.append(length).append(' ');
    }
    
    builder.append(path.toString().replace('\\', '/'));
      
    return builder.toString();
  }

  @EclipseGenerated
  @Override
  public String toString() {
    if(cachedString == null) {
      this.cachedString = internalToString();
    }
    return cachedString;
  }

  /**
   * @return the URI part of a fetch item
   */
  public URI getUri() {
    return uri;
  }

  /**
   * @return the length in bytes of the file
   */
  public Long getLength() {
    return length;
  }

  /**
   * @return the path in the bag it should be downloaded to
   */
  public Path getPath() {
    return path;
  }
  
  @EclipseGenerated
  @Override
  public int hashCode() {
    return Objects.hash(uri, length, path);
  }

  @EclipseGenerated
  @Override
  public boolean equals(final Object obj) {
    boolean isEqual = false;
    if (obj instanceof FetchItem){
      final FetchItem other = (FetchItem) obj;
      isEqual = Objects.equals(uri, other.getUri()) && Objects.equals(length, other.getLength()) && Objects.equals(path, other.getPath()); 
    }
    
    return isEqual;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy