001package ca.uhn.fhir.rest.api.server.storage; 002 003/*- 004 * #%L 005 * HAPI FHIR - Server Framework 006 * %% 007 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 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 ca.uhn.fhir.util.ObjectUtil; 024import org.hl7.fhir.instance.model.api.IIdType; 025 026import java.util.ArrayList; 027import java.util.Collection; 028import java.util.List; 029import java.util.Optional; 030 031/** 032 * This class is an abstraction for however primary keys are stored in the underlying storage engine. This might be 033 * a Long, a String, or something else. 034 */ 035public class ResourcePersistentId { 036 private static final String RESOURCE_PID = "RESOURCE_PID"; 037 private Object myId; 038 private Long myVersion; 039 private IIdType myAssociatedResourceId; 040 041 public ResourcePersistentId(Object theId) { 042 this(theId, null); 043 } 044 045 /** 046 * @param theVersion This should only be populated if a specific version is needed. If you want the current version, 047 * leave this as <code>null</code> 048 */ 049 public ResourcePersistentId(Object theId, Long theVersion) { 050 assert !(theId instanceof Optional); 051 myId = theId; 052 myVersion = theVersion; 053 } 054 055 public IIdType getAssociatedResourceId() { 056 return myAssociatedResourceId; 057 } 058 059 public ResourcePersistentId setAssociatedResourceId(IIdType theAssociatedResourceId) { 060 myAssociatedResourceId = theAssociatedResourceId; 061 return this; 062 } 063 064 @Override 065 public boolean equals(Object theO) { 066 if (!(theO instanceof ResourcePersistentId)) { 067 return false; 068 } 069 ResourcePersistentId that = (ResourcePersistentId) theO; 070 071 boolean retVal = ObjectUtil.equals(myId, that.myId); 072 retVal &= ObjectUtil.equals(myVersion, that.myVersion); 073 return retVal; 074 } 075 076 @Override 077 public int hashCode() { 078 int retVal = myId.hashCode(); 079 if (myVersion != null) { 080 retVal += myVersion.hashCode(); 081 } 082 return retVal; 083 } 084 085 public Object getId() { 086 return myId; 087 } 088 089 public void setId(Object theId) { 090 myId = theId; 091 } 092 093 public Long getIdAsLong() { 094 return (Long) myId; 095 } 096 097 @Override 098 public String toString() { 099 return myId.toString(); 100 } 101 102 public Long getVersion() { 103 return myVersion; 104 } 105 106 /** 107 * @param theVersion This should only be populated if a specific version is needed. If you want the current version, 108 * leave this as <code>null</code> 109 */ 110 public void setVersion(Long theVersion) { 111 myVersion = theVersion; 112 } 113 114 public static List<Long> toLongList(Collection<ResourcePersistentId> thePids) { 115 List<Long> retVal = new ArrayList<>(thePids.size()); 116 for (ResourcePersistentId next : thePids) { 117 retVal.add(next.getIdAsLong()); 118 } 119 return retVal; 120 } 121 122 public static List<ResourcePersistentId> fromLongList(List<Long> theResultList) { 123 List<ResourcePersistentId> retVal = new ArrayList<>(theResultList.size()); 124 for (Long next : theResultList) { 125 retVal.add(new ResourcePersistentId(next)); 126 } 127 return retVal; 128 } 129}