1 /* 2 * Copyright 2022 Google LLC 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 com.google.android.libraries.mobiledatadownload.file.common; 17 18 // TODO: investigate using org.joda.time.instant instead of Date 19 import java.util.Date; 20 21 /** Specification of GC behavior for a given file. */ 22 public final class GcParam { 23 24 private static enum Code { 25 NONE, 26 EXPIRES_AT 27 } 28 29 private static final GcParam NONE_PARAM = new GcParam(Code.NONE, null); 30 31 private final Code code; 32 private final Date date; 33 GcParam(Code code, Date date)34 private GcParam(Code code, Date date) { 35 this.code = code; 36 this.date = date; 37 } 38 39 /** 40 * @return A GcParam for expiration at the given date. 41 */ expiresAt(Date date)42 public static GcParam expiresAt(Date date) { 43 return new GcParam(Code.EXPIRES_AT, date); 44 } 45 46 /** 47 * @return the "none" GcParam. 48 */ none()49 public static GcParam none() { 50 return NONE_PARAM; 51 } 52 53 /** 54 * @return the expiration associated with {@code this}. 55 * @throws IllegalStateException if the GcParam is not an expiration 56 */ getExpiration()57 public Date getExpiration() { 58 if (!isExpiresAt()) { 59 throw new IllegalStateException("GcParam is not an expiration"); 60 } 61 return date; 62 } 63 64 /** 65 * @return whether {@code this} is an expiration GcParam 66 */ isExpiresAt()67 public boolean isExpiresAt() { 68 return code == Code.EXPIRES_AT; 69 } 70 71 /** 72 * @return whether {@code this} is the none GcParam 73 */ isNone()74 public boolean isNone() { 75 return code == Code.NONE; 76 } 77 78 @Override equals(Object obj)79 public boolean equals(Object obj) { 80 if (!(obj instanceof GcParam)) { 81 return false; 82 } 83 GcParam that = (GcParam) obj; 84 return (this.code == that.code) 85 && (this.isNone() || this.date.getTime() == that.date.getTime()); 86 } 87 88 @Override hashCode()89 public int hashCode() { 90 return isNone() ? 0 : date.hashCode(); 91 } 92 } 93