001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2022 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconsole.base;
020
021import java.beans.ConstructorProperties;
022
023/**
024 * Defines a web console component base model following the 
025 * JavaBean conventions. Conlet models should follow these 
026 * conventions because many template engines rely on them. 
027 * Besides, following these conventions often simplifies 
028 * serialization to portable formats.
029 * 
030 * This base class defines `conletId` as only property.
031 * Additionally, it overrides {@link #hashCode()} and
032 * {@link #equals(Object)} using the `conletId` as single 
033 * criterion.
034 */
035public class ConletBaseModel {
036
037    protected String conletId;
038
039    /**
040     * Creates a new model with the given type and id.
041     * 
042     * @param conletId the web console component id
043     */
044    @ConstructorProperties({ "conletId" })
045    public ConletBaseModel(String conletId) {
046        this.conletId = conletId;
047    }
048
049    /**
050     * Returns the web console component id.
051     * 
052     * @return the web console component id
053     */
054    public String getConletId() {
055        return conletId;
056    }
057
058    /**
059     * Hash code.
060     *
061     * @return the int
062     */
063    /*
064     * (non-Javadoc)
065     * 
066     * @see java.lang.Object#hashCode()
067     */
068    @Override
069    @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
070    public int hashCode() {
071        @SuppressWarnings("PMD.AvoidFinalLocalVariable")
072        final int prime = 31;
073        int result = 1;
074        result = prime * result
075            + ((conletId == null) ? 0 : conletId.hashCode());
076        return result;
077    }
078
079    /**
080     * Two objects are equal if they have equal web console component ids.
081     * 
082     * @param obj the other object
083     * @return the result
084     */
085    @Override
086    public boolean equals(Object obj) {
087        if (this == obj) {
088            return true;
089        }
090        if (obj == null) {
091            return false;
092        }
093        if (getClass() != obj.getClass()) {
094            return false;
095        }
096        ConletBaseModel other = (ConletBaseModel) obj;
097        if (conletId == null) {
098            if (other.conletId != null) {
099                return false;
100            }
101        } else if (!conletId.equals(other.conletId)) {
102            return false;
103        }
104        return true;
105    }
106}