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