001/*-
002 * #%L
003 * HAPI FHIR JPA Model
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.model.dao;
021
022import ca.uhn.fhir.rest.api.server.storage.BaseResourcePersistentId;
023
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.HashSet;
027import java.util.List;
028import java.util.Objects;
029import java.util.Set;
030
031/**
032 * JPA implementation of IResourcePersistentId.  JPA uses a Long as the primary key.  This class should be used in any
033 * context where the pid is known to be a Long.
034 */
035public class JpaPid extends BaseResourcePersistentId<Long> {
036        private final Long myId;
037
038        private JpaPid(Long theId) {
039                super(null);
040                myId = theId;
041        }
042
043        private JpaPid(Long theId, Long theVersion) {
044                super(theVersion, null);
045                myId = theId;
046        }
047
048        private JpaPid(Long theId, String theResourceType) {
049                super(theResourceType);
050                myId = theId;
051        }
052
053        private JpaPid(Long theId, Long theVersion, String theResourceType) {
054                super(theVersion, theResourceType);
055                myId = theId;
056        }
057
058        public static List<Long> toLongList(Collection<JpaPid> thePids) {
059                List<Long> retVal = new ArrayList<>(thePids.size());
060                for (JpaPid next : thePids) {
061                        retVal.add(next.getId());
062                }
063                return retVal;
064        }
065
066        public static Set<Long> toLongSet(Collection<JpaPid> thePids) {
067                Set<Long> retVal = new HashSet<>(thePids.size());
068                for (JpaPid next : thePids) {
069                        retVal.add(next.getId());
070                }
071                return retVal;
072        }
073
074        public static List<JpaPid> fromLongList(Collection<Long> theResultList) {
075                List<JpaPid> retVal = new ArrayList<>(theResultList.size());
076                for (Long next : theResultList) {
077                        retVal.add(fromId(next));
078                }
079                return retVal;
080        }
081
082        public static JpaPid fromId(Long theId) {
083                return new JpaPid(theId);
084        }
085
086        public static JpaPid fromIdAndVersion(Long theId, Long theVersion) {
087                return new JpaPid(theId, theVersion);
088        }
089
090        public static JpaPid fromIdAndResourceType(Long theId, String theResourceType) {
091                return new JpaPid(theId, theResourceType);
092        }
093
094        public static JpaPid fromIdAndVersionAndResourceType(Long theId, Long theVersion, String theResourceType) {
095                return new JpaPid(theId, theVersion, theResourceType);
096        }
097
098        @Override
099        public boolean equals(Object theO) {
100                if (this == theO) return true;
101                if (theO == null || getClass() != theO.getClass()) return false;
102                if (!super.equals(theO)) return false;
103                JpaPid jpaPid = (JpaPid) theO;
104                return myId.equals(jpaPid.myId);
105        }
106
107        @Override
108        public int hashCode() {
109                return Objects.hash(super.hashCode(), myId);
110        }
111
112        @Override
113        public Long getId() {
114                return myId;
115        }
116
117        @Override
118        public String toString() {
119                return myId.toString();
120        }
121}