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 Object myId;
037        private Long myVersion;
038        private IIdType myAssociatedResourceId;
039
040        public ResourcePersistentId(Object theId) {
041                this(theId, null);
042        }
043
044        /**
045         * @param theVersion This should only be populated if a specific version is needed. If you want the current version,
046         *                   leave this as <code>null</code>
047         */
048        public ResourcePersistentId(Object theId, Long theVersion) {
049                assert !(theId instanceof Optional);
050                myId = theId;
051                myVersion = theVersion;
052        }
053
054        public IIdType getAssociatedResourceId() {
055                return myAssociatedResourceId;
056        }
057
058        public ResourcePersistentId setAssociatedResourceId(IIdType theAssociatedResourceId) {
059                myAssociatedResourceId = theAssociatedResourceId;
060                return this;
061        }
062
063        @Override
064        public boolean equals(Object theO) {
065                if (!(theO instanceof ResourcePersistentId)) {
066                        return false;
067                }
068                ResourcePersistentId that = (ResourcePersistentId) theO;
069
070                boolean retVal = ObjectUtil.equals(myId, that.myId);
071                retVal &= ObjectUtil.equals(myVersion, that.myVersion);
072                return retVal;
073        }
074
075        @Override
076        public int hashCode() {
077                int retVal = myId.hashCode();
078                if (myVersion != null) {
079                        retVal += myVersion.hashCode();
080                }
081                return retVal;
082        }
083
084        public Object getId() {
085                return myId;
086        }
087
088        public void setId(Object theId) {
089                myId = theId;
090        }
091
092        public Long getIdAsLong() {
093                if (myId instanceof String) {
094                        return Long.parseLong((String) myId);
095                }
096                return (Long) myId;
097        }
098
099        @Override
100        public String toString() {
101                return myId.toString();
102        }
103
104        public Long getVersion() {
105                return myVersion;
106        }
107
108        /**
109         * @param theVersion This should only be populated if a specific version is needed. If you want the current version,
110         *                   leave this as <code>null</code>
111         */
112        public void setVersion(Long theVersion) {
113                myVersion = theVersion;
114        }
115
116        public static List<Long> toLongList(Collection<ResourcePersistentId> thePids) {
117                List<Long> retVal = new ArrayList<>(thePids.size());
118                for (ResourcePersistentId next : thePids) {
119                        retVal.add(next.getIdAsLong());
120                }
121                return retVal;
122        }
123
124        public static List<ResourcePersistentId> fromLongList(List<Long> theResultList) {
125                List<ResourcePersistentId> retVal = new ArrayList<>(theResultList.size());
126                for (Long next : theResultList) {
127                        retVal.add(new ResourcePersistentId(next));
128                }
129                return retVal;
130        }
131}