/* * This file is part of Gradoop. * * Gradoop 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. * * Gradoop 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 Gradoop. If not, see <http://www.gnu.org/licenses/>. */ package org.gradoop.common.model.impl.pojo; import com.google.common.base.Preconditions; import org.gradoop.common.model.api.entities.EPGMElement; import org.gradoop.common.model.impl.id.GradoopId; import org.gradoop.common.model.impl.properties.Properties; import org.gradoop.common.model.impl.properties.Property; import org.gradoop.common.model.impl.properties.PropertyValue; /** * Abstract base class for graphs, vertices and edges. */ public abstract class Element implements EPGMElement { /** * Entity identifier. */ protected GradoopId id; /** * Label of that entity. */ protected String label; /** * Internal property storage */ protected Properties properties; /** * Default constructor. */ protected Element() { } /** * Creates an object from the given parameters. Can only be called by * inheriting classes. * * @param id entity identifier * @param label entity label * @param properties key-value properties */ protected Element( GradoopId id, String label, Properties properties) { this.id = id; this.label = label; this.properties = properties; } /** * {@inheritDoc} */ @Override public GradoopId getId() { return id; } /** * {@inheritDoc} */ @Override public void setId(GradoopId id) { this.id = id; } /** * {@inheritDoc} */ @Override public String getLabel() { return label; } /** * {@inheritDoc} */ @Override public void setLabel(String label) { this.label = label; } /** * {@inheritDoc} */ @Override public Properties getProperties() { return properties; } /** * {@inheritDoc} */ @Override public Iterable<String> getPropertyKeys() { return (properties != null) ? properties.getKeys() : null; } /** * {@inheritDoc} */ @Override public PropertyValue getPropertyValue(String key) { return (properties != null) ? properties.get(key) : null; } /** * {@inheritDoc} */ @Override public void setProperties(Properties properties) { this.properties = properties; } /** * {@inheritDoc} */ @Override public void setProperty(Property property) { Preconditions.checkNotNull(property, "Property was null"); initProperties(); this.properties.set(property); } /** * {@inheritDoc} */ @Override public void setProperty(String key, Object value) { initProperties(); this.properties.set(key, value); } /** * {@inheritDoc} */ @Override public void setProperty(String key, PropertyValue value) { initProperties(); this.properties.set(key, value); } @Override public int getPropertyCount() { return (this.properties != null) ? this.properties.size() : 0; } /** * {@inheritDoc} */ @Override public boolean hasProperty(String key) { return getProperties() != null && getProperties().containsKey(key); } /** * {@inheritDoc} */ @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Element that = (Element) o; return !(id != null ? !id.equals(that.id) : that.id != null); } /** * {@inheritDoc} */ @Override public int hashCode() { int result = id.hashCode(); result = 31 * result + id.hashCode(); return result; } @Override public String toString() { return String.format("%s%s%s{%s}", id, label == null || label.equals("") ? "" : ":", label, properties == null ? "" : properties); } /** * Initializes the internal properties field if necessary. */ private void initProperties() { if (this.properties == null) { this.properties = Properties.create(); } } }