001package ca.uhn.fhir.interceptor.model; 002 003/*- 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2020 University Health Network 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import org.apache.commons.lang3.builder.EqualsBuilder; 024import org.apache.commons.lang3.builder.HashCodeBuilder; 025 026import javax.annotation.Nonnull; 027import javax.annotation.Nullable; 028import java.time.LocalDate; 029 030/** 031 * @since 5.0.0 032 */ 033public class RequestPartitionId { 034 035 private static final RequestPartitionId ALL_PARTITIONS = new RequestPartitionId(); 036 private final LocalDate myPartitionDate; 037 private final boolean myAllPartitions; 038 private final Integer myPartitionId; 039 private final String myPartitionName; 040 041 /** 042 * Constructor for a single partition 043 */ 044 private RequestPartitionId(@Nullable String thePartitionName, @Nullable Integer thePartitionId, @Nullable LocalDate thePartitionDate) { 045 myPartitionId = thePartitionId; 046 myPartitionName = thePartitionName; 047 myPartitionDate = thePartitionDate; 048 myAllPartitions = false; 049 } 050 051 /** 052 * Constructor for all partitions 053 */ 054 private RequestPartitionId() { 055 super(); 056 myPartitionDate = null; 057 myPartitionName = null; 058 myPartitionId = null; 059 myAllPartitions = true; 060 } 061 062 public boolean isAllPartitions() { 063 return myAllPartitions; 064 } 065 066 @Nullable 067 public LocalDate getPartitionDate() { 068 return myPartitionDate; 069 } 070 071 @Nullable 072 public String getPartitionName() { 073 return myPartitionName; 074 } 075 076 @Nullable 077 public Integer getPartitionId() { 078 return myPartitionId; 079 } 080 081 @Override 082 public String toString() { 083 return "RequestPartitionId[id=" + getPartitionId() + ", name=" + getPartitionName() + "]"; 084 } 085 086 /** 087 * Returns the partition ID (numeric) as a string, or the string "null" 088 */ 089 public String getPartitionIdStringOrNullString() { 090 if (myPartitionId == null) { 091 return "null"; 092 } 093 return myPartitionId.toString(); 094 } 095 096 @Override 097 public boolean equals(Object theO) { 098 if (this == theO) { 099 return true; 100 } 101 102 if (theO == null || getClass() != theO.getClass()) { 103 return false; 104 } 105 106 RequestPartitionId that = (RequestPartitionId) theO; 107 108 return new EqualsBuilder() 109 .append(myAllPartitions, that.myAllPartitions) 110 .append(myPartitionDate, that.myPartitionDate) 111 .append(myPartitionId, that.myPartitionId) 112 .append(myPartitionName, that.myPartitionName) 113 .isEquals(); 114 } 115 116 @Override 117 public int hashCode() { 118 return new HashCodeBuilder(17, 37) 119 .append(myPartitionDate) 120 .append(myAllPartitions) 121 .append(myPartitionId) 122 .append(myPartitionName) 123 .toHashCode(); 124 } 125 126 @Nonnull 127 public static RequestPartitionId allPartitions() { 128 return ALL_PARTITIONS; 129 } 130 131 @Nonnull 132 public static RequestPartitionId defaultPartition() { 133 return fromPartitionId(null); 134 } 135 136 @Nonnull 137 public static RequestPartitionId fromPartitionId(@Nullable Integer thePartitionId) { 138 return fromPartitionId(thePartitionId, null); 139 } 140 141 @Nonnull 142 public static RequestPartitionId fromPartitionId(@Nullable Integer thePartitionId, @Nullable LocalDate thePartitionDate) { 143 return new RequestPartitionId(null, thePartitionId, thePartitionDate); 144 } 145 146 @Nonnull 147 public static RequestPartitionId fromPartitionName(@Nullable String thePartitionName) { 148 return fromPartitionName(thePartitionName, null); 149 } 150 151 @Nonnull 152 public static RequestPartitionId fromPartitionName(@Nullable String thePartitionName, @Nullable LocalDate thePartitionDate) { 153 return new RequestPartitionId(thePartitionName, null, thePartitionDate); 154 } 155 156 @Nonnull 157 public static RequestPartitionId fromPartitionIdAndName(@Nullable Integer thePartitionId, @Nullable String thePartitionName) { 158 return new RequestPartitionId(thePartitionName, thePartitionId, null); 159 } 160 161 @Nonnull 162 public static RequestPartitionId forPartitionIdAndName(@Nullable Integer thePartitionId, @Nullable String thePartitionName, @Nullable LocalDate thePartitionDate) { 163 return new RequestPartitionId(thePartitionName, thePartitionId, thePartitionDate); 164 } 165 166 /** 167 * Create a string representation suitable for use as a cache key. Null aware. 168 */ 169 public static String stringifyForKey(RequestPartitionId theRequestPartitionId) { 170 String retVal = "(null)"; 171 if (theRequestPartitionId != null) { 172 retVal = theRequestPartitionId.getPartitionIdStringOrNullString(); 173 } 174 return retVal; 175 } 176}