001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.wake.profiler;
020
021import org.apache.reef.tang.types.ConstructorDef;
022
023import java.util.Arrays;
024
025/**
026 * A vertex in the object graph.  There is no edge type, since that would be redundant.
027 */
028public class Vertex<T> {
029  private final Object object;
030  private final String name;
031  private final ConstructorDef<T> constructorDef;
032  private final Vertex<?>[] constructorArguments;
033//  private final Set<Object> referencesToThisObject = new MonotonicHashSet<>();
034
035  public Vertex(final T object, final String name, final ConstructorDef<T> constructorDef,
036                final Vertex<?>[] constructorArguments) {
037    this.object = object;
038    if (object == null) {
039      throw new NullPointerException();
040    }
041    this.name = name;
042    this.constructorDef = constructorDef;
043    this.constructorArguments = constructorArguments;
044    for (final Vertex<?> v : constructorArguments) {
045      if (v == null) {
046        throw new NullPointerException();
047      }
048    }
049  }
050
051  public Vertex(final T object, final ConstructorDef<T> constructorDef, final Vertex<?>[] constructorArguments) {
052    this.object = object;
053    if (object == null) {
054      throw new NullPointerException();
055    }
056    this.name = null;
057    this.constructorDef = constructorDef;
058    this.constructorArguments = constructorArguments;
059    for (final Vertex<?> v : constructorArguments) {
060      if (v == null) {
061        throw new NullPointerException();
062      }
063    }
064  }
065
066  public Vertex(final Object object) {
067    this.object = object;
068    if (object == null) {
069      throw new NullPointerException();
070    }
071    this.name = null;
072    this.constructorDef = null;
073    this.constructorArguments = null;
074  }
075
076  //  public void addReference(Vertex<?> v) {
077//    referencesToThisObject.add(v);
078//  }
079//  public Vertex<?>[] getInEdges() {
080//    return referencesToThisObject.toArray(new Vertex[0]);
081//  }
082  public ConstructorDef<T> getConstructorDef() {
083    return this.constructorDef;
084  }
085
086  public Vertex<?>[] getOutEdges() {
087    if (constructorArguments == null) {
088      return new Vertex[0];
089    } else {
090      return Arrays.copyOf(constructorArguments, constructorArguments.length);
091    }
092  }
093
094  public Object getObject() {
095    return object;
096  }
097
098  public String getName() {
099    return name;
100  }
101}