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
017package com.echothree.util.common.persistence;
018
019import java.io.Serializable;
020import javax.annotation.Nonnull;
021
022public class BasePK
023        implements Serializable {
024    
025    private final String componentVendorName;
026    private final String entityTypeName;
027    private final Long entityId;
028    
029    private transient Integer _hashCode;
030    private transient String _entityRef;
031    private transient String _stringValue;
032    
033    /** Creates a new instance of BasePK */
034    public BasePK(final String componentVendorName, final String entityTypeName, final Long entityId) {
035        this.componentVendorName = componentVendorName;
036        this.entityTypeName = entityTypeName;
037        this.entityId = entityId;
038    }
039    
040    @Override
041    public int hashCode() {
042        if(_hashCode == null) {
043            _hashCode = this.entityId.hashCode();
044        }
045        
046        return _hashCode;
047    }
048
049    @Override
050    public boolean equals(final Object obj) {
051        if(obj instanceof BasePK that) {
052            return componentVendorName.equals(that.componentVendorName)
053                    && entityTypeName.equals(that.entityTypeName)
054                    &&  entityId.equals(that.entityId);
055        } else {
056            return false;
057        }
058    }
059    
060    /**
061     * Returns String representation of this PK in the form of "componentVendorName.entityTypeName.entityId"..
062     * @return String representation of this PK in the form of "componentVendorName.entityTypeName.entityId".
063     */
064    @Nonnull
065    public String getEntityRef() {
066        if(_entityRef == null) {
067            _entityRef = getComponentVendorName() + "." + getEntityTypeName() + "." + getEntityId();
068        }
069        
070        return _entityRef;
071    }
072
073    /**
074     * Returns String representation of this PK in the form of "[.componentVendorName.entityTypeName.entityId]"..
075     * @return String representation of this PK in the form of "[.componentVendorName.entityTypeName.entityId]".
076     */
077    @Override
078    @Nonnull
079    public String toString() {
080        if( _stringValue == null ) {
081            _stringValue = "[." + getEntityRef() + ']';
082        }
083        
084        return _stringValue;
085    }
086    
087    /**
088     * Returns the componentVendorName.
089     * @return the componentVendorName
090     */
091    @Nonnull
092    public String getComponentVendorName() {
093        return componentVendorName;
094    }
095    
096    /**
097     * Returns the entityTypeName.
098     * @return the entityTypeName
099     */
100    @Nonnull
101    public String getEntityTypeName() {
102        return entityTypeName;
103    }
104    
105    /**
106     * Returns the entityId.
107     * @return the entityId
108     */
109    @Nonnull
110    public Long getEntityId() {
111        return entityId;
112    }
113    
114}