001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2010–2011 ApexIdentity Inc.
015 * Portions Copyright 2011-2014 ForgeRock AS.
016 */
017
018package org.forgerock.openig.text;
019
020/**
021 * A field separator specification, used to parse delimiter-separated values.
022 */
023public class Separator {
024
025    /** The character used to separate values. */
026    private final char character;
027
028    /** The character used to quote string literals, or {@code -1} if none. */
029    private final int quote;
030
031    /** The character used to escape character literals, or {@code -1} if none. */
032    private final int escape;
033
034    /**
035     * Constructs a new field separator specification.
036     *
037     * @param character the character used to separate values.
038     * @param quote the character used to quote string literals, or {@code -1} if none.
039     * @param escape the character used to escape character literals, or {@code -1} if none.
040     */
041    public Separator(char character, int quote, int escape) {
042        this.character = character;
043        this.quote = quote;
044        this.escape = escape;
045    }
046
047    /**
048     * Returns the character used to separate values.
049     * @return the character used to separate values.
050     */
051    public char getCharacter() {
052        return character;
053    }
054
055    /**
056     * Returns the character used to quote string literals, or {@code -1} if none.
057     * @return the character used to quote string literals, or {@code -1} if none.
058     */
059    public int getQuote() {
060        return quote;
061    }
062
063    /**
064     * Returns the character used to escape character literals, or {@code -1} if none.
065     * @return the character used to escape character literals, or {@code -1} if none.
066     */
067    public int getEscape() {
068        return escape;
069    }
070
071    /**
072     * Indicates whether some other object is equal to this one.
073     *
074     * @param o the reference object with which to compare.
075     * @return {@code true} if this object is the same as the {@code obj} argument.
076     */
077    @Override
078    public boolean equals(Object o) {
079        if (o == null || !(o instanceof Separator)) {
080            return false;
081        }
082        if (this == o) {
083            return true;
084        }
085        return (this.character == ((Separator) o).character
086                && this.quote == ((Separator) o).quote
087                && this.escape == ((Separator) o).escape);
088    }
089
090    @Override
091    public int hashCode() {
092        return character ^ quote ^ escape;
093    }
094}