All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xowl.infra.server.api.base.BaseUser Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2015 Laurent Wouters
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this program.
 * If not, see .
 *
 * Contributors:
 *     Laurent Wouters - [email protected]
 ******************************************************************************/

package org.xowl.infra.server.api.base;

import org.xowl.hime.redist.ASTNode;
import org.xowl.infra.server.api.XOWLUser;
import org.xowl.infra.store.IOUtils;

/**
 * Base implementation of a user
 *
 * @author Laurent Wouters
 */
public class BaseUser implements XOWLUser {
    /**
     * The user's name
     */
    protected final String name;

    /**
     * Initializes this user
     *
     * @param name The user's name
     */
    public BaseUser(String name) {
        this.name = name;
    }

    /**
     * Initializes this user
     *
     * @param root The user's definition
     */
    public BaseUser(ASTNode root) {
        String value = null;
        for (ASTNode child : root.getChildren()) {
            ASTNode nodeMemberName = child.getChildren().get(0);
            String name = IOUtils.unescape(nodeMemberName.getValue());
            name = name.substring(1, name.length() - 1);
            if (name.equals("name")) {
                ASTNode nodeValue = child.getChildren().get(1);
                value = IOUtils.unescape(nodeValue.getValue());
                value = value.substring(1, value.length() - 1);
            }
        }
        this.name = value;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String serializedString() {
        return getName();
    }

    @Override
    public String serializedJSON() {
        return "{\"type\": \"" + IOUtils.escapeStringJSON(XOWLUser.class.getCanonicalName()) + "\", \"name\": \"" + IOUtils.escapeStringJSON(getName()) + "\"}";
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof XOWLUser && name.equals(((XOWLUser) obj).getName());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy