FetchItem.java

  1. /*
  2.  * Copyright (C) 2023 DANS - Data Archiving and Networked Services (info@dans.knaw.nl)
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package nl.knaw.dans.bagit.domain;

  17. import java.net.URL;
  18. import java.nio.file.Path;
  19. import java.util.Objects;

  20. /**
  21.  * An individual item to fetch as specified by
  22.  * <a href="https://tools.ietf.org/html/draft-kunze-bagit-13#section-2.2.3">https://tools.ietf.org/html/draft-kunze-bagit-13#section-2.2.3</a>
  23.  */
  24. public final class FetchItem {
  25.   /**
  26.    * The url from which the item can be downloaded
  27.    */
  28.   public final URL url;
  29.  
  30.   /**
  31.    * The length of the file in octets
  32.    */
  33.   public final Long length;
  34.  
  35.   /**
  36.    * The path where the fetched item should be put
  37.    */
  38.   public final Path path;
  39.  
  40.   private transient String cachedString;
  41.  
  42.   /**
  43.    *
  44.    * @param url the {@link URL} of the file
  45.    * @param length the file length in bytes, -1 or null to not specify the length
  46.    * @param path the path in the bag where the file belongs
  47.    */
  48.   public FetchItem(final URL url, final Long length, final Path path){
  49.     this.url = url;
  50.     this.length = length;
  51.     this.path = path;
  52.   }
  53.  
  54.   private String internalToString() {
  55.     final StringBuilder sb = new StringBuilder();
  56.     sb.append(url).append(' ');
  57.    
  58.     if(length == null || length < 0){
  59.       sb.append("- ");
  60.     }
  61.     else{
  62.       sb.append(length).append(' ');
  63.     }
  64.    
  65.     sb.append(path);
  66.      
  67.     return sb.toString();
  68.   }

  69.   @Override
  70.   public String toString() {
  71.     if(cachedString == null){
  72.       cachedString = internalToString();
  73.     }
  74.    
  75.     return cachedString;
  76.   }

  77.   public URL getUrl() {
  78.     return url;
  79.   }

  80.   public Long getLength() {
  81.     return length;
  82.   }

  83.   public Path getPath() {
  84.     return path;
  85.   }
  86.  
  87.   @Override
  88.   public int hashCode() {
  89.     return Objects.hash(url) + Objects.hash(length) + Objects.hash(path);
  90.   }

  91.   @Override
  92.   public boolean equals(final Object obj) {
  93.     if (this == obj){
  94.       return true;
  95.     }
  96.     if (obj == null){
  97.       return false;
  98.     }
  99.     if (!(obj instanceof FetchItem)){
  100.       return false;
  101.     }
  102.    
  103.     final FetchItem other = (FetchItem) obj;
  104.    
  105.     return Objects.equals(url, other.getUrl()) && Objects.equals(length, other.getLength()) && Objects.equals(path, other.getPath());
  106.   }
  107. }