/*
 * Copyright 2011, Mysema Ltd
 * 
 * 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 com.mysema.query.types;

import java.io.Serializable;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import com.google.common.base.Objects;

/**
 * PathMetadata provides metadata for {@link Path} expressions.
 *
 * @author tiwe
 */
@Immutable
public final class PathMetadata<T> implements Serializable{

    private static final long serialVersionUID = -1055994185028970065L;

    private final Object element;
    
    private final int hashCode;

    @Nullable
    private final Path<?> parent, root;

    private final PathType pathType;
    
    public PathMetadata(@Nullable Path<?> parent, Object element, PathType type) {
        this.parent = parent;
        this.element = element;
        this.pathType = type;
        this.hashCode = 31 * element.hashCode() + pathType.hashCode();
        this.root = parent != null ? parent.getRoot() : null;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        } else if (obj instanceof PathMetadata<?>) { 
            PathMetadata<?> p = (PathMetadata<?>) obj;
            return element.equals(p.element) &&
                    pathType == p.pathType &&
                    Objects.equal(parent, p.parent);
        } else {
            return false;
        }

    }

    public Object getElement() {
        return element;
    }
    
    public String getName() {
        return (String)element;
    }

    @Nullable
    public Path<?> getParent() {
        return parent;
    }
    
    public PathType getPathType() {
        return pathType;
    }

    @Nullable
    public Path<?> getRoot() {
        return root;
    }

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

    public boolean isRoot() {
        return parent == null || (pathType == PathType.DELEGATE && parent.getMetadata().isRoot()); 
    }

}

@@ -28,18 +28,17 @@
 a Ltd%0A *
- 
 %0A
+
  * Licen
@@ -1032,21 +1032,17 @@
 lement;%0A
-    
 %0A
+
     priv
@@ -1158,21 +1158,17 @@
 thType;%0A
-    
 %0A
+
     publ
@@ -1469,29 +1469,25 @@
 null;%0A    %7D%0A
-    
 %0A
+
     @Overrid
@@ -1632,17 +1632,16 @@
 ta%3C?%3E) %7B
- 
 %0A       
@@ -1951,37 +1951,33 @@
  element;%0A    %7D%0A
-    
 %0A
+
     public Strin
@@ -2002,31 +2002,324 @@
     
-return (String)element;
+if (pathType == PathType.VARIABLE %7C%7C pathType == PathType.PROPERTY) %7B%0A            return (String)element;%0A        %7D else %7B%0A            throw new IllegalStateException(%22name property not available for path of type %22 + pathType +%0A                    %22. Use getElement() to access the generic path element.%22);%0A        %7D
 %0A   
@@ -2402,13 +2402,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2636,32 +2636,32 @@
 lean isRoot() %7B%0A
+
         return p
@@ -2742,17 +2742,16 @@
 Root());
- 
 %0A    %7D%0A%0A

Add better exception for wrong getName() usage #512