/*
 *  Copyright 2001-present Stephen Colebourne
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.joda.beans.sample;

import java.util.Date;
import java.util.Map;

import org.joda.beans.Bean;
import org.joda.beans.BeanBuilder;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.MetaProperty;
import org.joda.beans.Property;
import org.joda.beans.gen.BeanDefinition;
import org.joda.beans.gen.PropertyDefinition;
import org.joda.beans.impl.direct.DirectBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaBean;
import org.joda.beans.impl.direct.DirectMetaProperty;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;

/**
 * A user account class, used to demonstrate usage of Joda-Beans.
 */
@BeanDefinition
public final class UserAccount implements Bean {

    /**
     * The user identifier.
     */
    @PropertyDefinition(validate = "notEmpty")
    private String userId;
    /**
     * The email address.
     */
    @PropertyDefinition(validate = "notEmpty")
    private String emailAddress;
    /**
     * The number of logins.
     */
    @PropertyDefinition
    private int numberLogins;
    /**
     * The last login instant.
     */
    @PropertyDefinition(validate = "notNull")
    private Date lastLogin;

    //------------------------- AUTOGENERATED START -------------------------
    /**
     * The meta-bean for {@code UserAccount}.
     * @return the meta-bean, not null
     */
    public static UserAccount.Meta meta() {
        return UserAccount.Meta.INSTANCE;
    }

    static {
        MetaBean.register(UserAccount.Meta.INSTANCE);
    }

    @Override
    public UserAccount.Meta metaBean() {
        return UserAccount.Meta.INSTANCE;
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the user identifier.
     * @return the value of the property, not empty
     */
    public String getUserId() {
        return userId;
    }

    /**
     * Sets the user identifier.
     * @param userId  the new value of the property, not empty
     */
    public void setUserId(String userId) {
        JodaBeanUtils.notEmpty(userId, "userId");
        this.userId = userId;
    }

    /**
     * Gets the the {@code userId} property.
     * @return the property, not null
     */
    public Property<String> userId() {
        return metaBean().userId().createProperty(this);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the email address.
     * @return the value of the property, not empty
     */
    public String getEmailAddress() {
        return emailAddress;
    }

    /**
     * Sets the email address.
     * @param emailAddress  the new value of the property, not empty
     */
    public void setEmailAddress(String emailAddress) {
        JodaBeanUtils.notEmpty(emailAddress, "emailAddress");
        this.emailAddress = emailAddress;
    }

    /**
     * Gets the the {@code emailAddress} property.
     * @return the property, not null
     */
    public Property<String> emailAddress() {
        return metaBean().emailAddress().createProperty(this);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the number of logins.
     * @return the value of the property
     */
    public int getNumberLogins() {
        return numberLogins;
    }

    /**
     * Sets the number of logins.
     * @param numberLogins  the new value of the property
     */
    public void setNumberLogins(int numberLogins) {
        this.numberLogins = numberLogins;
    }

    /**
     * Gets the the {@code numberLogins} property.
     * @return the property, not null
     */
    public Property<Integer> numberLogins() {
        return metaBean().numberLogins().createProperty(this);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the last login instant.
     * @return the value of the property, not null
     */
    public Date getLastLogin() {
        return lastLogin;
    }

    /**
     * Sets the last login instant.
     * @param lastLogin  the new value of the property, not null
     */
    public void setLastLogin(Date lastLogin) {
        JodaBeanUtils.notNull(lastLogin, "lastLogin");
        this.lastLogin = lastLogin;
    }

    /**
     * Gets the the {@code lastLogin} property.
     * @return the property, not null
     */
    public Property<Date> lastLogin() {
        return metaBean().lastLogin().createProperty(this);
    }

    //-----------------------------------------------------------------------
    @Override
    public UserAccount clone() {
        return JodaBeanUtils.cloneAlways(this);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj != null && obj.getClass() == this.getClass()) {
            UserAccount other = (UserAccount) obj;
            return JodaBeanUtils.equal(this.getUserId(), other.getUserId()) &&
                    JodaBeanUtils.equal(this.getEmailAddress(), other.getEmailAddress()) &&
                    (this.getNumberLogins() == other.getNumberLogins()) &&
                    JodaBeanUtils.equal(this.getLastLogin(), other.getLastLogin());
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = getClass().hashCode();
        hash = hash * 31 + JodaBeanUtils.hashCode(getUserId());
        hash = hash * 31 + JodaBeanUtils.hashCode(getEmailAddress());
        hash = hash * 31 + JodaBeanUtils.hashCode(getNumberLogins());
        hash = hash * 31 + JodaBeanUtils.hashCode(getLastLogin());
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder(160);
        buf.append("UserAccount{");
        buf.append("userId").append('=').append(JodaBeanUtils.toString(getUserId())).append(',').append(' ');
        buf.append("emailAddress").append('=').append(JodaBeanUtils.toString(getEmailAddress())).append(',').append(' ');
        buf.append("numberLogins").append('=').append(JodaBeanUtils.toString(getNumberLogins())).append(',').append(' ');
        buf.append("lastLogin").append('=').append(JodaBeanUtils.toString(getLastLogin()));
        buf.append('}');
        return buf.toString();
    }

    //-----------------------------------------------------------------------
    /**
     * The meta-bean for {@code UserAccount}.
     */
    public static final class Meta extends DirectMetaBean {
        /**
         * The singleton instance of the meta-bean.
         */
        static final Meta INSTANCE = new Meta();

        /**
         * The meta-property for the {@code userId} property.
         */
        private final MetaProperty<String> userId = DirectMetaProperty.ofReadWrite(
                this, "userId", UserAccount.class, String.class);
        /**
         * The meta-property for the {@code emailAddress} property.
         */
        private final MetaProperty<String> emailAddress = DirectMetaProperty.ofReadWrite(
                this, "emailAddress", UserAccount.class, String.class);
        /**
         * The meta-property for the {@code numberLogins} property.
         */
        private final MetaProperty<Integer> numberLogins = DirectMetaProperty.ofReadWrite(
                this, "numberLogins", UserAccount.class, Integer.TYPE);
        /**
         * The meta-property for the {@code lastLogin} property.
         */
        private final MetaProperty<Date> lastLogin = DirectMetaProperty.ofReadWrite(
                this, "lastLogin", UserAccount.class, Date.class);
        /**
         * The meta-properties.
         */
        private final Map<String, MetaProperty<?>> metaPropertyMap$ = new DirectMetaPropertyMap(
                this, null,
                "userId",
                "emailAddress",
                "numberLogins",
                "lastLogin");

        /**
         * Restricted constructor.
         */
        private Meta() {
        }

        @Override
        protected MetaProperty<?> metaPropertyGet(String propertyName) {
            switch (propertyName.hashCode()) {
                case -836030906:  // userId
                    return this.userId;
                case -1070931784:  // emailAddress
                    return this.emailAddress;
                case 850160339:  // numberLogins
                    return this.numberLogins;
                case 1995610739:  // lastLogin
                    return this.lastLogin;
            }
            return super.metaPropertyGet(propertyName);
        }

        @Override
        public BeanBuilder<? extends UserAccount> builder() {
            return new DirectBeanBuilder<>(new UserAccount());
        }

        @Override
        public Class<? extends UserAccount> beanType() {
            return UserAccount.class;
        }

        @Override
        public Map<String, MetaProperty<?>> metaPropertyMap() {
            return metaPropertyMap$;
        }

        //-----------------------------------------------------------------------
        /**
         * The meta-property for the {@code userId} property.
         * @return the meta-property, not null
         */
        public MetaProperty<String> userId() {
            return userId;
        }

        /**
         * The meta-property for the {@code emailAddress} property.
         * @return the meta-property, not null
         */
        public MetaProperty<String> emailAddress() {
            return emailAddress;
        }

        /**
         * The meta-property for the {@code numberLogins} property.
         * @return the meta-property, not null
         */
        public MetaProperty<Integer> numberLogins() {
            return numberLogins;
        }

        /**
         * The meta-property for the {@code lastLogin} property.
         * @return the meta-property, not null
         */
        public MetaProperty<Date> lastLogin() {
            return lastLogin;
        }

        //-----------------------------------------------------------------------
        @Override
        protected Object propertyGet(Bean bean, String propertyName, boolean quiet) {
            switch (propertyName.hashCode()) {
                case -836030906:  // userId
                    return ((UserAccount) bean).getUserId();
                case -1070931784:  // emailAddress
                    return ((UserAccount) bean).getEmailAddress();
                case 850160339:  // numberLogins
                    return ((UserAccount) bean).getNumberLogins();
                case 1995610739:  // lastLogin
                    return ((UserAccount) bean).getLastLogin();
            }
            return super.propertyGet(bean, propertyName, quiet);
        }

        @Override
        protected void propertySet(Bean bean, String propertyName, Object newValue, boolean quiet) {
            switch (propertyName.hashCode()) {
                case -836030906:  // userId
                    ((UserAccount) bean).setUserId((String) newValue);
                    return;
                case -1070931784:  // emailAddress
                    ((UserAccount) bean).setEmailAddress((String) newValue);
                    return;
                case 850160339:  // numberLogins
                    ((UserAccount) bean).setNumberLogins((Integer) newValue);
                    return;
                case 1995610739:  // lastLogin
                    ((UserAccount) bean).setLastLogin((Date) newValue);
                    return;
            }
            super.propertySet(bean, propertyName, newValue, quiet);
        }

        @Override
        protected void validate(Bean bean) {
            JodaBeanUtils.notEmpty(((UserAccount) bean).userId, "userId");
            JodaBeanUtils.notEmpty(((UserAccount) bean).emailAddress, "emailAddress");
            JodaBeanUtils.notNull(((UserAccount) bean).lastLogin, "lastLogin");
        }

    }

    //-------------------------- AUTOGENERATED END --------------------------
}
