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

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

Go to download

A driver for DataStax Enterprise (DSE) and Apache Cassandra 1.2+ clusters that works exclusively with the Cassandra Query Language version 3 (CQL3) and Cassandra's binary protocol, supporting DSE-specific features such as geospatial types, DSE Graph and DSE authentication.

There is a newer version: 2.4.0
Show newest version
/*
 * 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.Objects;
import com.google.common.reflect.TypeToken;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@SuppressWarnings({"unchecked", "rawtypes"})
public class ObjectGraphNode implements GraphNode {

  private final Object delegate;

  public ObjectGraphNode(Object delegate) {
    this.delegate = delegate;
  }

  @Override
  public boolean isNull() {
    return delegate == null;
  }

  @Override
  public boolean isObject() {
    return delegate instanceof Map;
  }

  @Override
  public boolean isArray() {
    return delegate instanceof List;
  }

  @Override
  public boolean isValue() {
    return !(isArray() || isObject());
  }

  @Override
  public Iterator fieldNames() {
    return ((Map) delegate).keySet().iterator();
  }

  @Override
  public int size() {
    return isArray() ? ((List) delegate).size() : 0;
  }

  @Override
  public int asInt() {
    return (Integer) delegate;
  }

  @Override
  public boolean asBoolean() {
    return (Boolean) delegate;
  }

  @Override
  public long asLong() {
    return (Long) delegate;
  }

  @Override
  public double asDouble() {
    return (Double) delegate;
  }

  @Override
  public String asString() {
    return (String) delegate;
  }

  @Override
  public Map asMap() {
    return (Map) delegate;
  }

  @Override
  public  T as(Class clazz) {
    return (T) delegate;
  }

  @Override
  public  T as(TypeToken type) {
    return (T) delegate;
  }

  @Override
  public GraphNode get(String fieldName) {
    if (((Map) delegate).containsKey(fieldName)) {
      return new ObjectGraphNode(((Map) delegate).get(fieldName));
    }
    return null;
  }

  @Override
  public GraphNode get(int index) {
    Object indexValue = ((List) delegate).get(index);
    if (indexValue == null) {
      return null;
    } else {
      return new ObjectGraphNode(indexValue);
    }
  }

  @Override
  public boolean isVertex() {
    return delegate instanceof Vertex;
  }

  @Override
  public boolean isEdge() {
    return delegate instanceof Edge;
  }

  @Override
  public Vertex asVertex() {
    return (Vertex) delegate;
  }

  @Override
  public Edge asEdge() {
    return (Edge) delegate;
  }

  @Override
  public Path asPath() {
    return (Path) delegate;
  }

  @Override
  public Property asProperty() {
    return (Property) delegate;
  }

  @Override
  public VertexProperty asVertexProperty() {
    return (VertexProperty) delegate;
  }

  @Override
  public String toString() {
    return this.delegate.toString();
  }

  @Override
  public boolean equals(Object other) {
    if (this == other) return true;
    // Compare each others' delegates.
    return other instanceof ObjectGraphNode
        && Objects.equal(this.delegate, ((ObjectGraphNode) other).delegate);
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy