/*
 * Copyright (C) 2018 Thomas Almy
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package us.almy.jelbumpro;

/**
 * Class for Jelbum program to define attributes of words
 * @author tom
 */
public class AWord implements Cloneable {

    /**
     * The text for the word. Sometimes this field is not used!
     */
    public String text;

    /**
     * The checksum of text or what it would be if text were present.
     * This is just the sum of all the character values, a=0, b=1, ..., z=25
     */
    public int checksum;

    /**
     * The number of characters in the word.
     */
    public int length;

    /**
     * The number of occurences of each letter in the word.
     */
    public byte[] map = new byte[26];

    /**
     * If this AWord is in a linked list, this is the link to the next AWord
     */
    public AWord next;
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        AWord obj = (AWord)super.clone();
        obj.map = map.clone();
        return obj;
    }
    
    /**
     * Compares map with that of AWord obj to determine if they have
     * the same set of letters.
     * @param obj The AWord to compare to
     * @return they are equal
     */
    public boolean equalMap(AWord obj) {
        for (int i = 0; i<26; i++) {
            if (map[i] != obj.map[i]) return false;
        }
        return true;
    }
    
    /**
     * Compares map with that of AWord obj to determine if all the characters
     * in obj appear in us.
     * @param obj The AWord to compare to
     * @return obj is contained within
     */
    public boolean fitMap(AWord obj) { // we have to fit the object
        for (int i = 0; i<26; i++) {
            if (map[i] > obj.map[i]) return false;
        }
        return true;
    }
}
