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

com.datastax.driver.dse.graph.DefaultElement Maven / Gradle / Ivy

/*
 * Copyright DataStax, Inc.
 *
 * This software can be used solely with DataStax Enterprise. Please consult the license at
 * http://www.datastax.com/terms/datastax-dse-driver-license-terms
 */
package com.datastax.driver.dse.graph;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.common.collect.Multimap;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;

/** Base class for elements (vertices, edges and vertex properties). */
abstract class DefaultElement implements Element {

  private static final Function GRAPH_NODE_TO_PROPERTY =
      new Function() {
        @Override
        public Property apply(GraphNode input) {
          return input.asProperty();
        }
      };

  GraphNode id;

  String label;

  Multimap properties;

  DefaultElement() {}

  @Override
  public GraphNode getId() {
    return id;
  }

  @Override
  public String getLabel() {
    return label;
  }

  @Override
  public Set getPropertyNames() {
    if (properties == null || properties.isEmpty()) return Collections.emptySet();
    return Collections.unmodifiableSet(properties.keySet());
  }

  @Override
  public Property getProperty(String name) {
    if (properties == null || properties.isEmpty() || !properties.containsKey(name)) return null;
    Iterator iterator = getProperties(name);
    if (!iterator.hasNext()) return null;
    return iterator.next();
  }

  @Override
  public Iterator getProperties(String name) {
    if (properties == null || properties.isEmpty() || !properties.containsKey(name))
      return ImmutableSet.of().iterator();
    return Iterators.unmodifiableIterator(
        Iterators.transform(properties.get(name).iterator(), GRAPH_NODE_TO_PROPERTY));
  }

  @Override
  public Iterator getProperties() {
    if (properties == null || properties.isEmpty()) return ImmutableSet.of().iterator();
    return Iterators.unmodifiableIterator(
        Iterators.transform(properties.values().iterator(), GRAPH_NODE_TO_PROPERTY));
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Element)) return false;
    Element that = (Element) o;
    // we compare only ids to be consistent with Tinkerpop
    return Objects.equal(getId(), that.getId());
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy