001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016// Generated File -- DO NOT EDIT BY HAND
017// --------------------------------------------------------------------------------
018
019/**
020 * EntityInstanceValue.java
021 */
022
023package com.echothree.model.data.core.server.value;
024
025import com.echothree.model.data.core.common.pk.EntityInstancePK;
026
027import com.echothree.model.data.core.server.factory.EntityInstanceFactory;
028
029import com.echothree.model.data.core.common.pk.EntityTypePK;
030
031import com.echothree.util.common.exception.PersistenceCloneException;
032import com.echothree.util.common.exception.PersistenceNotNullException;
033
034import com.echothree.util.server.persistence.BaseValue;
035
036import java.io.Serializable;
037
038public class EntityInstanceValue
039        extends BaseValue<EntityInstancePK>
040        implements Cloneable, Serializable {
041    
042    private EntityTypePK entityTypePK;
043    private boolean entityTypePKHasBeenModified = false;
044    private Long entityUniqueId;
045    private boolean entityUniqueIdHasBeenModified = false;
046    private String uuid;
047    private boolean uuidHasBeenModified = false;
048    
049    private transient Integer _hashCode = null;
050    private transient String _stringValue = null;
051    
052    private void constructFields(EntityTypePK entityTypePK, Long entityUniqueId, String uuid)
053            throws PersistenceNotNullException {
054        checkForNull(entityTypePK);
055        this.entityTypePK = entityTypePK;
056        checkForNull(entityUniqueId);
057        this.entityUniqueId = entityUniqueId;
058        this.uuid = uuid;
059    }
060    
061    /** Creates a new instance of EntityInstanceValue */
062    public EntityInstanceValue(EntityInstancePK entityInstancePK, EntityTypePK entityTypePK, Long entityUniqueId, String uuid)
063            throws PersistenceNotNullException {
064        super(entityInstancePK);
065        constructFields(entityTypePK, entityUniqueId, uuid);
066    }
067    
068    /** Creates a new instance of EntityInstanceValue */
069    public EntityInstanceValue(EntityTypePK entityTypePK, Long entityUniqueId, String uuid)
070            throws PersistenceNotNullException {
071        super();
072        constructFields(entityTypePK, entityUniqueId, uuid);
073    }
074    
075   @Override
076   public EntityInstanceFactory getBaseFactoryInstance() {
077        return EntityInstanceFactory.getInstance();
078    }
079    
080    @Override
081    public EntityInstanceValue clone() {
082        Object result;
083        
084        try {
085            result = super.clone();
086        } catch (CloneNotSupportedException cnse) {
087            // This shouldn't happen, fail when it does.
088            throw new PersistenceCloneException(cnse);
089        }
090        
091        return (EntityInstanceValue)result;
092    }
093    
094   @Override
095    public EntityInstancePK getPrimaryKey() {
096        if(_primaryKey == null) {
097            _primaryKey = new EntityInstancePK(entityId);
098        }
099        
100        return _primaryKey;
101    }
102    
103    private void clearHashAndString() {
104        _hashCode = null;
105        _stringValue = null;
106    }
107    
108    @Override
109    public int hashCode() {
110        if(_hashCode == null) {
111            int hashCode = 17;
112            
113            hashCode = 37 * hashCode + ((entityId != null) ? entityId.hashCode() : 0);
114            
115            hashCode = 37 * hashCode + ((entityTypePK != null) ? entityTypePK.hashCode() : 0);
116            hashCode = 37 * hashCode + ((entityUniqueId != null) ? entityUniqueId.hashCode() : 0);
117            hashCode = 37 * hashCode + ((uuid != null) ? uuid.hashCode() : 0);
118            
119            _hashCode = hashCode;
120        }
121        
122        return _hashCode;
123    }
124    
125    @Override
126    public String toString() {
127        if(_stringValue == null) {
128            _stringValue = "{" + 
129                    "entityId=" + getEntityId() +
130                    ", entityTypePK=" + getEntityTypePK() +
131                    ", entityUniqueId=" + getEntityUniqueId() +
132                    ", uuid=" + getUuid() +
133                    "}";
134        }
135        return _stringValue;
136    }
137    
138    @Override
139    public boolean equals(Object other) {
140        if(this == other)
141            return true;
142        
143        if(!hasIdentity())
144            return false;
145        
146        if(other instanceof  EntityInstanceValue that) {
147            if(!that.hasIdentity())
148                return false;
149            
150            Long thisEntityId = getEntityId();
151            Long thatEntityId = that.getEntityId();
152            
153            boolean objectsEqual = thisEntityId.equals(thatEntityId);
154            if(objectsEqual)
155                objectsEqual = isIdentical(that);
156            
157            return objectsEqual;
158        } else {
159            return false;
160        }
161    }
162    
163    public boolean isIdentical(Object other) {
164        if(other instanceof EntityInstanceValue that) {
165            boolean objectsEqual = true;
166            
167            
168            if(objectsEqual) {
169                EntityTypePK thisEntityTypePK = getEntityTypePK();
170                EntityTypePK thatEntityTypePK = that.getEntityTypePK();
171                
172                if(thisEntityTypePK == null) {
173                    objectsEqual = objectsEqual && (thatEntityTypePK == null);
174                } else {
175                    objectsEqual = objectsEqual && thisEntityTypePK.equals(thatEntityTypePK);
176                }
177            }
178            
179            if(objectsEqual) {
180                Long thisEntityUniqueId = getEntityUniqueId();
181                Long thatEntityUniqueId = that.getEntityUniqueId();
182                
183                if(thisEntityUniqueId == null) {
184                    objectsEqual = objectsEqual && (thatEntityUniqueId == null);
185                } else {
186                    objectsEqual = objectsEqual && thisEntityUniqueId.equals(thatEntityUniqueId);
187                }
188            }
189            
190            if(objectsEqual) {
191                String thisUuid = getUuid();
192                String thatUuid = that.getUuid();
193                
194                if(thisUuid == null) {
195                    objectsEqual = objectsEqual && (thatUuid == null);
196                } else {
197                    objectsEqual = objectsEqual && thisUuid.equals(thatUuid);
198                }
199            }
200            
201            return objectsEqual;
202        } else {
203            return false;
204        }
205    }
206    
207    @Override
208    public boolean hasBeenModified() {
209        return entityTypePKHasBeenModified || entityUniqueIdHasBeenModified || uuidHasBeenModified;
210    }
211    
212    @Override
213    public void clearHasBeenModified() {
214        entityTypePKHasBeenModified = false;
215        entityUniqueIdHasBeenModified = false;
216        uuidHasBeenModified = false;
217    }
218    
219    public EntityTypePK getEntityTypePK() {
220        return entityTypePK;
221    }
222    
223    public void setEntityTypePK(EntityTypePK entityTypePK)
224            throws PersistenceNotNullException {
225        checkForNull(entityTypePK);
226        
227        boolean update = true;
228        
229        if(this.entityTypePK != null) {
230            if(this.entityTypePK.equals(entityTypePK)) {
231                update = false;
232            }
233        } else if(entityTypePK == null) {
234            update = false;
235        }
236        
237        if(update) {
238            this.entityTypePK = entityTypePK;
239            entityTypePKHasBeenModified = true;
240            clearHashAndString();
241        }
242    }
243    
244    public boolean getEntityTypePKHasBeenModified() {
245        return entityTypePKHasBeenModified;
246    }
247    
248    public Long getEntityUniqueId() {
249        return entityUniqueId;
250    }
251    
252    public void setEntityUniqueId(Long entityUniqueId)
253            throws PersistenceNotNullException {
254        checkForNull(entityUniqueId);
255        
256        boolean update = true;
257        
258        if(this.entityUniqueId != null) {
259            if(this.entityUniqueId.equals(entityUniqueId)) {
260                update = false;
261            }
262        } else if(entityUniqueId == null) {
263            update = false;
264        }
265        
266        if(update) {
267            this.entityUniqueId = entityUniqueId;
268            entityUniqueIdHasBeenModified = true;
269            clearHashAndString();
270        }
271    }
272    
273    public boolean getEntityUniqueIdHasBeenModified() {
274        return entityUniqueIdHasBeenModified;
275    }
276    
277    public String getUuid() {
278        return uuid;
279    }
280    
281    public void setUuid(String uuid) {
282        boolean update = true;
283        
284        if(this.uuid != null) {
285            if(this.uuid.equals(uuid)) {
286                update = false;
287            }
288        } else if(uuid == null) {
289            update = false;
290        }
291        
292        if(update) {
293            this.uuid = uuid;
294            uuidHasBeenModified = true;
295            clearHashAndString();
296        }
297    }
298    
299    public boolean getUuidHasBeenModified() {
300        return uuidHasBeenModified;
301    }
302    
303}