Version.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.util.Objects;

  18. /**
  19.  * The version of the bagit specification used to create the bag.
  20.  */
  21. public final class Version implements Comparable<Version>{
  22.   public final int major;
  23.   public final int minor;
  24.  
  25.   private transient final String cachedToString;
  26.  
  27.   public Version(final int major, final int minor){
  28.     this.major = major;
  29.     this.minor = minor;
  30.     this.cachedToString = major + "." + minor;
  31.   }
  32.  
  33.   public static Version LATEST_BAGIT_VERSION() {
  34.     return new Version(1, 0);
  35.   }

  36.   @Override
  37.   public String toString() {
  38.     return cachedToString;
  39.   }

  40.   @Override
  41.   public int compareTo(final Version o) {
  42.     //a negative integer - this is less than specified object
  43.     //zero - equal to specified object
  44.     //positive - greater than the specified object
  45.     if(major > o.major || major == o.major && minor > o.minor){
  46.       return 1;
  47.     }
  48.     if(major == o.major && minor == o.minor){
  49.       return 0;
  50.     }
  51.    
  52.     return -1;
  53.   }

  54.   @Override
  55.   public int hashCode() {
  56.     return Objects.hash(major) + Objects.hash(minor);
  57.   }

  58.   @Override
  59.   public boolean equals(final Object obj) {
  60.     if (this == obj){
  61.       return true;
  62.     }
  63.     if (obj == null){
  64.       return false;
  65.     }
  66.     if (!(obj instanceof Version)){
  67.       return false;
  68.     }
  69.    
  70.     final Version other = (Version) obj;
  71.    
  72.     return Objects.equals(major, other.major) && Objects.equals(minor, other.minor);
  73.   }
  74.  
  75.   public boolean isNewer(final Version version){
  76.     return this.compareTo(version) > 0;
  77.   }
  78.  
  79.   public boolean isSameOrNewer(final Version version){
  80.     return this.compareTo(version) >= 0;
  81.   }
  82.  
  83.   public boolean isOlder(final Version version){
  84.     return this.compareTo(version) < 0;
  85.   }
  86.  
  87.   public boolean isSameOrOlder(final Version version){
  88.     return this.compareTo(version) <= 0;
  89.   }

  90.   public int getMajor() {
  91.     return major;
  92.   }

  93.   public int getMinor() {
  94.     return minor;
  95.   }
  96. }