/*
 *  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.io.Serializable;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;

import org.joda.beans.Bean;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaBean;
import org.joda.beans.TypedMetaBean;
import org.joda.beans.gen.BeanDefinition;
import org.joda.beans.gen.DerivedProperty;
import org.joda.beans.gen.PropertyDefinition;
import org.joda.beans.impl.light.LightMetaBean;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;

/**
 * Mock light bean, used for testing.
 * 
 * @author Stephen Colebourne
 */
@BeanDefinition(style = "light", constructorScope = "package")
public final class LightMutable implements Bean, Serializable {

    /**
     * The number.
     */
    @PropertyDefinition
    private int number;
    /**
     * The number.
     */
    @PropertyDefinition
    private boolean flag;
    /**
     * The street.
     */
    @PropertyDefinition(validate = "notNull", get = "field")
    private String street;
    /**
     * The text.
     */
    @PropertyDefinition(get = "optionalGuava")
    private String town;
    /**
     * The city.
     */
    @PropertyDefinition(validate = "notNull", alias = "place")
    private String city;
    /**
     * The list.
     */
    @PropertyDefinition(validate = "notNull")
    private final List<String> list = new ArrayList<>();
    /**
     * The currency.
     */
    @PropertyDefinition(get = "optionalGuava")
    private Currency currency;
    /**
     * The hidden text.
     */
    @PropertyDefinition(get = "")
    private String hiddenText;

    //-----------------------------------------------------------------------
    // manual getter with a different name
    public String getStreetName() {
        return street;
    }

    // derived
    @DerivedProperty
    public String getAddress() {
        return number + " " + street + " " + city;
    }

    //------------------------- AUTOGENERATED START -------------------------
    /**
     * The meta-bean for {@code LightMutable}.
     */
    private static final TypedMetaBean<LightMutable> META_BEAN =
            LightMetaBean.of(
                    LightMutable.class,
                    MethodHandles.lookup(),
                    new String[] {
                            "number",
                            "flag",
                            "street",
                            "town",
                            "city",
                            "list",
                            "currency",
                            "hiddenText"},
                    0,
                    Boolean.FALSE,
                    null,
                    null,
                    null,
                    ImmutableList.of(),
                    null,
                    null)
                    .withAlias("place", "city");

    /**
     * The meta-bean for {@code LightMutable}.
     * @return the meta-bean, not null
     */
    public static TypedMetaBean<LightMutable> meta() {
        return META_BEAN;
    }

    static {
        MetaBean.register(META_BEAN);
    }

    /**
     * The serialization version id.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Creates an instance.
     * @param number  the value of the property
     * @param flag  the value of the property
     * @param street  the value of the property, not null
     * @param town  the value of the property
     * @param city  the value of the property, not null
     * @param list  the value of the property, not null
     * @param currency  the value of the property
     * @param hiddenText  the value of the property
     */
    LightMutable(
            int number,
            boolean flag,
            String street,
            String town,
            String city,
            List<String> list,
            Currency currency,
            String hiddenText) {
        setNumber(number);
        setFlag(flag);
        setStreet(street);
        setTown(town);
        setCity(city);
        setList(list);
        setCurrency(currency);
        setHiddenText(hiddenText);
    }

    @Override
    public TypedMetaBean<LightMutable> metaBean() {
        return META_BEAN;
    }

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

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

    //-----------------------------------------------------------------------
    /**
     * Gets the number.
     * @return the value of the property
     */
    public boolean isFlag() {
        return flag;
    }

    /**
     * Sets the number.
     * @param flag  the new value of the property
     */
    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    //-----------------------------------------------------------------------
    /**
     * Sets the street.
     * @param street  the new value of the property, not null
     */
    public void setStreet(String street) {
        JodaBeanUtils.notNull(street, "street");
        this.street = street;
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the text.
     * @return the optional value of the property, not null
     */
    public Optional<String> getTown() {
        return Optional.fromNullable(town);
    }

    /**
     * Sets the text.
     * @param town  the new value of the property
     */
    public void setTown(String town) {
        this.town = town;
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the city.
     * @return the value of the property, not null
     */
    public String getCity() {
        return city;
    }

    /**
     * Sets the city.
     * @param city  the new value of the property, not null
     */
    public void setCity(String city) {
        JodaBeanUtils.notNull(city, "city");
        this.city = city;
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the list.
     * @return the value of the property, not null
     */
    public List<String> getList() {
        return list;
    }

    /**
     * Sets the list.
     * @param list  the new value of the property, not null
     */
    public void setList(List<String> list) {
        JodaBeanUtils.notNull(list, "list");
        this.list.clear();
        this.list.addAll(list);
    }

    //-----------------------------------------------------------------------
    /**
     * Gets the currency.
     * @return the optional value of the property, not null
     */
    public Optional<Currency> getCurrency() {
        return Optional.fromNullable(currency);
    }

    /**
     * Sets the currency.
     * @param currency  the new value of the property
     */
    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    //-----------------------------------------------------------------------
    /**
     * Sets the hidden text.
     * @param hiddenText  the new value of the property
     */
    public void setHiddenText(String hiddenText) {
        this.hiddenText = hiddenText;
    }

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

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj != null && obj.getClass() == this.getClass()) {
            LightMutable other = (LightMutable) obj;
            return (this.getNumber() == other.getNumber()) &&
                    (this.isFlag() == other.isFlag()) &&
                    JodaBeanUtils.equal(this.street, other.street) &&
                    JodaBeanUtils.equal(this.town, other.town) &&
                    JodaBeanUtils.equal(this.getCity(), other.getCity()) &&
                    JodaBeanUtils.equal(this.getList(), other.getList()) &&
                    JodaBeanUtils.equal(this.currency, other.currency) &&
                    JodaBeanUtils.equal(this.hiddenText, other.hiddenText);
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = getClass().hashCode();
        hash = hash * 31 + JodaBeanUtils.hashCode(getNumber());
        hash = hash * 31 + JodaBeanUtils.hashCode(isFlag());
        hash = hash * 31 + JodaBeanUtils.hashCode(street);
        hash = hash * 31 + JodaBeanUtils.hashCode(town);
        hash = hash * 31 + JodaBeanUtils.hashCode(getCity());
        hash = hash * 31 + JodaBeanUtils.hashCode(getList());
        hash = hash * 31 + JodaBeanUtils.hashCode(currency);
        hash = hash * 31 + JodaBeanUtils.hashCode(hiddenText);
        return hash;
    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder(320);
        buf.append("LightMutable{");
        buf.append("number").append('=').append(JodaBeanUtils.toString(getNumber())).append(',').append(' ');
        buf.append("flag").append('=').append(JodaBeanUtils.toString(isFlag())).append(',').append(' ');
        buf.append("street").append('=').append(JodaBeanUtils.toString(street)).append(',').append(' ');
        buf.append("town").append('=').append(JodaBeanUtils.toString(town)).append(',').append(' ');
        buf.append("city").append('=').append(JodaBeanUtils.toString(getCity())).append(',').append(' ');
        buf.append("list").append('=').append(JodaBeanUtils.toString(getList())).append(',').append(' ');
        buf.append("currency").append('=').append(JodaBeanUtils.toString(currency)).append(',').append(' ');
        buf.append("hiddenText").append('=').append(JodaBeanUtils.toString(hiddenText)).append(',').append(' ');
        buf.append("address").append('=').append(JodaBeanUtils.toString(getAddress()));
        buf.append('}');
        return buf.toString();
    }

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