001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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        extends Object
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(String componentVendorName, String entityTypeName, 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(Object obj) {
051        if(obj instanceof BasePK) {
052            BasePK that = (BasePK)obj;
053            
054            return componentVendorName.equals(that.componentVendorName)
055                    && entityTypeName.equals(that.entityTypeName)
056                    &&  entityId.equals(that.entityId);
057        } else {
058            return false;
059        }
060    }
061    
062    /**
063     * Returns String representation of this PK in the form of "componentVendorName.entityTypeName.entityId"..
064     * @return String representation of this PK in the form of "componentVendorName.entityTypeName.entityId".
065     */
066    public String getEntityRef() {
067        if(_entityRef == null) {
068            _entityRef = getComponentVendorName() + "." + getEntityTypeName() + "." + getEntityId();
069        }
070        
071        return _entityRef;
072    }
073
074    /**
075     * Returns String representation of this PK in the form of "[.componentVendorName.entityTypeName.entityId]"..
076     * @return String representation of this PK in the form of "[.componentVendorName.entityTypeName.entityId]".
077     */
078    @Override
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   public String getComponentVendorName() {
092        return componentVendorName;
093    }
094    
095    /**
096     * Returns the entityTypeName.
097     * @return the entityTypeName
098     */
099    public String getEntityTypeName() {
100        return entityTypeName;
101    }
102    
103    /**
104     * Returns the entityId.
105     * @return the entityId
106     */
107    public Long getEntityId() {
108        return entityId;
109    }
110    
111}