/*
 * @(#)MutuallyExclusiveOption.java
 *
 * Copyright (c) 2002-2003 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JSCAPE.
 */

package com.jscape.util.cli;

import java.util.Arrays;
import java.util.List;

/**
 * @author Alex
 */
@SuppressWarnings("unchecked")
public class MutuallyExclusiveOption
        extends AbstractOption {

    /**
     * Or sign.
     */
    protected static final String OR_SIGN = "|";

    /**
     * Names.
     */
    private List names;

    /**
     * @param names    names
     * @param required required
     * @param position position
     */
    public MutuallyExclusiveOption(String[] names, boolean required, int position) {
        super(names[0], required, position);
        this.names = Arrays.asList(names);
    }

    /**
     * @return names
     */
    public String[] getNames() {
        return (String[]) this.names.toArray(new String[this.names.size()]);
    }

    /**
     * @param o Object instance
     *
     * @return is equal
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        if (!super.equals(o)) {
            return false;
        }

        MutuallyExclusiveOption that = (MutuallyExclusiveOption) o;

        return this.names.equals(that.names);
    }

    /**
     * @return int
     */
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + names.hashCode();
        return result;
    }

    /**
     * @param option option to check
     *
     * @return found
     */
    protected boolean found(String option) {
        return this.names.contains(option);
    }

    /**
     * @return String
     */
    protected String getOptionString() {
        StringBuffer buf = new StringBuffer();

        for (int i = 0, size = this.names.size(); i < size; ++i) {
            if (i > 0) {
                buf.append(OR_SIGN);
            }
            buf.append(this.names.get(i));
        }

        return buf.toString();
    }

}
