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;
022import java.io.Serializable;
023
024/**
025 * Defines a web console component base model following the 
026 * JavaBean conventions. Conlet models should follow these 
027 * conventions because many template engines rely on them. 
028 * Besides, following these conventions often simplifies 
029 * serialization to portable formats.
030 * 
031 * This base class defines `conletId` as only property.
032 * Additionally, it overrides {@link #hashCode()} and
033 * {@link #equals(Object)} using the `conletId` as single 
034 * criterion.
035 */
036@SuppressWarnings("serial")
037public class ConletBaseModel implements Serializable {
038
039    protected String conletId;
040
041    /**
042     * Creates a new model with the given type and id.
043     * 
044     * @param conletId the web console component id
045     */
046    @ConstructorProperties({ "conletId" })
047    public ConletBaseModel(String conletId) {
048        this.conletId = conletId;
049    }
050
051    /**
052     * Returns the web console component id.
053     * 
054     * @return the web console component id
055     */
056    public String getConletId() {
057        return conletId;
058    }
059
060    /**
061     * Hash code.
062     *
063     * @return the int
064     */
065    /*
066     * (non-Javadoc)
067     * 
068     * @see java.lang.Object#hashCode()
069     */
070    @Override
071    @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
072    public int hashCode() {
073        @SuppressWarnings("PMD.AvoidFinalLocalVariable")
074        final int prime = 31;
075        int result = 1;
076        result = prime * result
077            + ((conletId == null) ? 0 : conletId.hashCode());
078        return result;
079    }
080
081    /**
082     * Two objects are equal if they have equal web console component ids.
083     * 
084     * @param obj the other object
085     * @return the result
086     */
087    @Override
088    public boolean equals(Object obj) {
089        if (this == obj) {
090            return true;
091        }
092        if (obj == null) {
093            return false;
094        }
095        if (getClass() != obj.getClass()) {
096            return false;
097        }
098        ConletBaseModel other = (ConletBaseModel) obj;
099        if (conletId == null) {
100            if (other.conletId != null) {
101                return false;
102            }
103        } else if (!conletId.equals(other.conletId)) {
104            return false;
105        }
106        return true;
107    }
108}