/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.prestosql.sql.analyzer;

import io.prestosql.sql.tree.Node;

import javax.annotation.Nullable;

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.lang.String.format;
import static java.lang.System.identityHashCode;
import static java.util.Objects.requireNonNull;

public class RelationId
{
    /**
     * Creates {@link RelationId} equal to any {@link RelationId} created from exactly the same source.
     */
    public static RelationId of(Node sourceNode)
    {
        return new RelationId(requireNonNull(sourceNode, "source cannot be null"));
    }

    /**
     * Creates {@link RelationId} equal only to itself
     */
    public static RelationId anonymous()
    {
        return new RelationId(null);
    }

    @Nullable
    private final Node sourceNode;

    private RelationId(Node sourceNode)
    {
        this.sourceNode = sourceNode;
    }

    public boolean isAnonymous()
    {
        return sourceNode == null;
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        RelationId that = (RelationId) o;
        return sourceNode != null && sourceNode == that.sourceNode;
    }

    @Override
    public int hashCode()
    {
        return identityHashCode(sourceNode);
    }

    @Override
    public String toString()
    {
        if (isAnonymous()) {
            return toStringHelper(this)
                    .addValue("anonymous")
                    .addValue(format("x%08x", identityHashCode(this)))
                    .toString();
        }
        else {
            return toStringHelper(this)
                    .addValue(sourceNode.getClass().getSimpleName())
                    .addValue(format("x%08x", identityHashCode(sourceNode)))
                    .toString();
        }
    }
}

@@ -630,41 +630,98 @@
 de;%0A
-%0Aimport javax.annotation.Nullable
+import io.prestosql.sql.tree.NodeRef;%0A%0Aimport java.util.Objects;%0Aimport java.util.Optional
 ;%0A%0Ai
@@ -875,56 +875,8 @@
 ode;
-%0Aimport static java.util.Objects.requireNonNull;
 %0A%0Apu
@@ -1108,58 +1108,42 @@
 nId(
-requireNonNull(sourceNode, %22source cannot be null%22
+Optional.of(NodeRef.of(sourceNode)
 ));%0A
@@ -1301,58 +1301,75 @@
 nId(
-null);%0A    %7D%0A%0A    @Nullable%0A    private final Node
+Optional.empty());%0A    %7D%0A%0A    private final Optional%3CNodeRef%3CNode%3E%3E
  sou
@@ -1401,20 +1401,39 @@
 ationId(
-Node
+Optional%3CNodeRef%3CNode%3E%3E
  sourceN
@@ -1534,32 +1534,33 @@
 %0A        return 
+!
 sourceNode == nu
@@ -1553,24 +1553,28 @@
 urceNode
- == null
+.isPresent()
 ;%0A    %7D%0A
@@ -1852,34 +1852,73 @@
 Node
- != null && sourceNode == 
+.isPresent() && that.sourceNode.isPresent() && sourceNode.equals(
 that
@@ -1920,32 +1920,33 @@
 (that.sourceNode
+)
 ;%0A    %7D%0A%0A    @Ov
@@ -1999,32 +1999,28 @@
  return 
-identityHashCode
+Objects.hash
 (sourceN
@@ -2403,16 +2403,22 @@
 rceNode.
+get().
 getClass
@@ -2510,16 +2510,22 @@
 urceNode
+.get()
 )))%0A    

Use optional to represent missing node