/* ###
 * IP: GHIDRA
 *
 * 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.
 */
/* Generated by Together */

package ghidra.program.util;

import java.util.Arrays;

import ghidra.framework.options.SaveState;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.CommentType;
import ghidra.program.model.listing.Program;

/**
 * The <CODE>CommentFieldLocation</CODE> class contains specific location information
 * within the COMMENTS field of a CodeUnitLocation object.
 */
public class CommentFieldLocation extends CodeUnitLocation {

	protected String[] comment;
	protected CommentType type;

	/**
	 * Construct a new CommentFieldLocation.
	 * @param program the program of the location
	 * @param addr address of the location; should not be null
	 * hierarchy names; this parameter may be null
	 * @param componentPath if not null, it is the array of indexes that point
	 * to a specific data type inside of another data type
	 * @param comment   The array of strings that make up the comment
	 * @param type      The type of this comment (null for no-comment type)
	 * @param row       The index of the string that contains the exact location.
	 * @param charOffset       The position within the string that specifies the exact location.
	 * @exception IllegalArgumentException
	 *                      Thrown if type is not one of the comment values given in <CODE>CodeUnit</CODE>
	 */
	public CommentFieldLocation(Program program, Address addr, int[] componentPath,
			String[] comment, CommentType type, int row, int charOffset) {

		super(program, addr, componentPath, row, 0, charOffset);
		this.comment = comment;
		if (comment == null) {
			this.comment = new String[0];
		}
		this.type = type;

	}

	/**
	 * Default constructor needed for restoring
	 * a comment field location from XML.
	 */
	public CommentFieldLocation() {
	}

	/**
	 * Returns the array of strings that make up the comment.
	 * @return the comment
	 */
	public String[] getComment() {
		return comment;
	}

	/**
	 * {@return the comment type or null if no-comment.}
	 */
	public CommentType getCommentType() {
		return type;
	}

	/**
	 * Returns a String representation of this location.
	 */
	@Override
	public String toString() {
		String str = type != null ? type.name() : "NO-COMMENT";
		return super.toString() + ", Comment Type = " + str;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + Arrays.hashCode(comment);
		if (type != null) {
			result = prime * result + type.hashCode();
		}
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		CommentFieldLocation other = (CommentFieldLocation) obj;
		if (!Arrays.equals(comment, other.comment)) {
			return false;
		}
		if (type != other.type) {
			return false;
		}
		return true;
	}

	@Override
	public void saveState(SaveState obj) {
		super.saveState(obj);
		obj.putStrings("_COMMENT", comment);
		obj.putInt("_TYPE", type != null ? type.ordinal() : -1);
	}

	@Override
	public void restoreState(Program p, SaveState obj) {
		super.restoreState(p, obj);
		comment = obj.getStrings("_COMMENT", new String[0]);
		int commentType = obj.getInt("_TYPE", CommentType.EOL.ordinal());
		type = commentType < 0 ? null : CommentType.valueOf(commentType);
	}
}
