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