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

se.kth.iss.ug2.UgAttribute Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
/*
 * MIT License
 *
 * Copyright (c) 2017 Kungliga Tekniska högskolan
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package se.kth.iss.ug2;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * This represents a basic UG attribute. It can either be multival or singelval.
 */
public class UgAttribute {
    private final List values;
    private final boolean multival;
    private final String attributeName;

    /**
     * Default constructor.
     *
     * @param attributeName Name of attribute in UG
     * @param values        Attribute value or values
     * @param multival      if it is a multivalue attribute in ug or not.
     */
    public UgAttribute(String attributeName, List values, boolean multival) {
        this.multival = multival;
        this.values = values;
        this.attributeName = attributeName;
    }

    /**
     * @return a list of this attributes values.
     */
    public List getValues() {
        return Collections.unmodifiableList(values);
    }

    /**
     * @return true if it is a multivalue attribute.
     */
    public boolean isMultiVal() {
        return multival;
    }

    /**
     * Gets single value as a String. Should never be calles for an attribute
     * that is multivalue in UG and throws a {@link Ug2Exception}
     * if that happens.
     *
     * @return value as a String if it exists, null otherwise.
     * @throws Ug2Exception on errors.
     */
    public String getSingleValue() throws Ug2Exception {
        if (multival) {
            throw new Ug2Exception("Attempted to get single value from multivalue attribute: " + attributeName);
        }
        if (values.isEmpty()) {
            return null;
        }

        return values.get(0);
    }

    @Override
    public String toString() {
        return "multival: " + multival + " values:" + values;
    }

    public boolean valueEquals(UgAttribute other) {
        Set myvalues = new HashSet<>(this.getValues());
        Set othervalues = new HashSet<>(other.getValues());
        return myvalues.equals(othervalues);
    }

    @Override
    public boolean equals(Object other) {
        return other instanceof UgAttribute && valueEquals((UgAttribute) other);
    }

    @Override
    public int hashCode() {
        return values.hashCode() + this.attributeName.hashCode();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy