001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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
038import javax.annotation.Nonnull;
039import javax.annotation.Nullable;
040
041public class EntityInstanceValue
042        extends BaseValue<EntityInstancePK>
043        implements Cloneable, Serializable {
044    
045    private EntityTypePK entityTypePK;
046    private boolean entityTypePKHasBeenModified = false;
047    private Long entityUniqueId;
048    private boolean entityUniqueIdHasBeenModified = false;
049    private String uuid;
050    private boolean uuidHasBeenModified = false;
051    
052    private transient Integer _hashCode = null;
053    private transient String _stringValue = null;
054    
055    private void constructFields(EntityTypePK entityTypePK, Long entityUniqueId, String uuid)
056            throws PersistenceNotNullException {
057        checkForNull(entityTypePK);
058        this.entityTypePK = entityTypePK;
059        checkForNull(entityUniqueId);
060        this.entityUniqueId = entityUniqueId;
061        this.uuid = uuid;
062    }
063    
064    /** Creates a new instance of EntityInstanceValue */
065    public EntityInstanceValue(EntityInstancePK entityInstancePK, EntityTypePK entityTypePK, Long entityUniqueId, String uuid)
066            throws PersistenceNotNullException {
067        super(entityInstancePK);
068        constructFields(entityTypePK, entityUniqueId, uuid);
069    }
070    
071    /** Creates a new instance of EntityInstanceValue */
072    public EntityInstanceValue(EntityTypePK entityTypePK, Long entityUniqueId, String uuid)
073            throws PersistenceNotNullException {
074        super();
075        constructFields(entityTypePK, entityUniqueId, uuid);
076    }
077    
078    @Override
079    @Nonnull
080    public EntityInstanceFactory getBaseFactoryInstance() {
081        return EntityInstanceFactory.getInstance();
082    }
083    
084    @Override
085    @Nonnull
086    public EntityInstanceValue clone() {
087        Object result;
088        
089        try {
090            result = super.clone();
091        } catch (CloneNotSupportedException cnse) {
092            // This shouldn't happen, fail when it does.
093            throw new PersistenceCloneException(cnse);
094        }
095        
096        return (EntityInstanceValue)result;
097    }
098    
099    @Override
100    @Nonnull
101    public EntityInstancePK getPrimaryKey() {
102        if(_primaryKey == null) {
103            _primaryKey = new EntityInstancePK(entityId);
104        }
105        
106        return _primaryKey;
107    }
108    
109    private void clearHashAndString() {
110        _hashCode = null;
111        _stringValue = null;
112    }
113    
114    @Override
115    public int hashCode() {
116        if(_hashCode == null) {
117            int hashCode = 17;
118            
119            hashCode = 37 * hashCode + ((entityId != null) ? entityId.hashCode() : 0);
120            
121            hashCode = 37 * hashCode + ((entityTypePK != null) ? entityTypePK.hashCode() : 0);
122            hashCode = 37 * hashCode + ((entityUniqueId != null) ? entityUniqueId.hashCode() : 0);
123            hashCode = 37 * hashCode + ((uuid != null) ? uuid.hashCode() : 0);
124            
125            _hashCode = hashCode;
126        }
127        
128        return _hashCode;
129    }
130    
131    @Override
132    @Nonnull
133    public String toString() {
134        if(_stringValue == null) {
135            _stringValue = "{" + 
136                    "entityId=" + getEntityId() +
137                    ", entityTypePK=" + getEntityTypePK() +
138                    ", entityUniqueId=" + getEntityUniqueId() +
139                    ", uuid=" + getUuid() +
140                    "}";
141        }
142        return _stringValue;
143    }
144    
145    @Override
146    public boolean equals(Object other) {
147        if(this == other)
148            return true;
149        
150        if(!hasIdentity())
151            return false;
152        
153        if(other instanceof  EntityInstanceValue that) {
154            if(!that.hasIdentity())
155                return false;
156            
157            Long thisEntityId = getEntityId();
158            Long thatEntityId = that.getEntityId();
159            
160            boolean objectsEqual = thisEntityId.equals(thatEntityId);
161            if(objectsEqual)
162                objectsEqual = isIdentical(that);
163            
164            return objectsEqual;
165        } else {
166            return false;
167        }
168    }
169    
170    public boolean isIdentical(Object other) {
171        if(other instanceof EntityInstanceValue that) {
172            boolean objectsEqual = true;
173            
174            
175            if(objectsEqual) {
176                EntityTypePK thisEntityTypePK = getEntityTypePK();
177                EntityTypePK thatEntityTypePK = that.getEntityTypePK();
178                
179                if(thisEntityTypePK == null) {
180                    objectsEqual = objectsEqual && (thatEntityTypePK == null);
181                } else {
182                    objectsEqual = objectsEqual && thisEntityTypePK.equals(thatEntityTypePK);
183                }
184            }
185            
186            if(objectsEqual) {
187                Long thisEntityUniqueId = getEntityUniqueId();
188                Long thatEntityUniqueId = that.getEntityUniqueId();
189                
190                if(thisEntityUniqueId == null) {
191                    objectsEqual = objectsEqual && (thatEntityUniqueId == null);
192                } else {
193                    objectsEqual = objectsEqual && thisEntityUniqueId.equals(thatEntityUniqueId);
194                }
195            }
196            
197            if(objectsEqual) {
198                String thisUuid = getUuid();
199                String thatUuid = that.getUuid();
200                
201                if(thisUuid == null) {
202                    objectsEqual = objectsEqual && (thatUuid == null);
203                } else {
204                    objectsEqual = objectsEqual && thisUuid.equals(thatUuid);
205                }
206            }
207            
208            return objectsEqual;
209        } else {
210            return false;
211        }
212    }
213    
214    @Override
215    public boolean hasBeenModified() {
216        return entityTypePKHasBeenModified || entityUniqueIdHasBeenModified || uuidHasBeenModified;
217    }
218    
219    @Override
220    public void clearHasBeenModified() {
221        entityTypePKHasBeenModified = false;
222        entityUniqueIdHasBeenModified = false;
223        uuidHasBeenModified = false;
224    }
225    
226    @Nonnull
227    public EntityTypePK getEntityTypePK() {
228        return entityTypePK;
229    }
230    
231    public void setEntityTypePK(@Nonnull EntityTypePK entityTypePK)
232            throws PersistenceNotNullException {
233        checkForNull(entityTypePK);
234        
235        boolean update = true;
236        
237        if(this.entityTypePK != null) {
238            if(this.entityTypePK.equals(entityTypePK)) {
239                update = false;
240            }
241        } else if(entityTypePK == null) {
242            update = false;
243        }
244        
245        if(update) {
246            this.entityTypePK = entityTypePK;
247            entityTypePKHasBeenModified = true;
248            clearHashAndString();
249        }
250    }
251    
252    public boolean getEntityTypePKHasBeenModified() {
253        return entityTypePKHasBeenModified;
254    }
255    
256    @Nonnull
257    public Long getEntityUniqueId() {
258        return entityUniqueId;
259    }
260    
261    public void setEntityUniqueId(@Nonnull Long entityUniqueId)
262            throws PersistenceNotNullException {
263        checkForNull(entityUniqueId);
264        
265        boolean update = true;
266        
267        if(this.entityUniqueId != null) {
268            if(this.entityUniqueId.equals(entityUniqueId)) {
269                update = false;
270            }
271        } else if(entityUniqueId == null) {
272            update = false;
273        }
274        
275        if(update) {
276            this.entityUniqueId = entityUniqueId;
277            entityUniqueIdHasBeenModified = true;
278            clearHashAndString();
279        }
280    }
281    
282    public boolean getEntityUniqueIdHasBeenModified() {
283        return entityUniqueIdHasBeenModified;
284    }
285    
286    @Nullable
287    public String getUuid() {
288        return uuid;
289    }
290    
291    public void setUuid(@Nullable String uuid) {
292        boolean update = true;
293        
294        if(this.uuid != null) {
295            if(this.uuid.equals(uuid)) {
296                update = false;
297            }
298        } else if(uuid == null) {
299            update = false;
300        }
301        
302        if(update) {
303            this.uuid = uuid;
304            uuidHasBeenModified = true;
305            clearHashAndString();
306        }
307    }
308    
309    public boolean getUuidHasBeenModified() {
310        return uuidHasBeenModified;
311    }
312    
313}