001/* 002 * Copyright (c) 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Bogdan Stefanescu 011 * Florent Guillaume 012 */ 013package org.nuxeo.ecm.core.query.sql.model; 014 015import org.apache.commons.lang.StringUtils; 016 017/** 018 * A named reference to a variable (this can be a field or table). 019 * <p> 020 * Can also include a cast. 021 */ 022public class Reference implements Operand { 023 024 private static final long serialVersionUID = -1725102431543210430L; 025 026 public final String name; 027 028 public final String cast; 029 030 public final EsHint esHint; 031 032 public Reference(String name) { 033 this.name = name; 034 cast = null; 035 esHint = null; 036 } 037 038 /** @since 5.6 */ 039 public Reference(String name, String cast) { 040 this.name = name; 041 this.cast = cast; 042 esHint = null; 043 } 044 045 /** @since 5.6 */ 046 public Reference(Reference other, String cast) { 047 this.name = other.name; 048 this.cast = cast; 049 esHint = null; 050 } 051 052 /** @since 7.3 */ 053 public Reference(Reference other, EsHint hint) { 054 this.name = other.name; 055 cast = null; 056 this.esHint = hint; 057 } 058 059 @Override 060 public void accept(IVisitor visitor) { 061 visitor.visitReference(this); 062 } 063 064 @Override 065 public String toString() { 066 if (cast != null) { 067 return cast + '(' + name + ')'; 068 } else if (esHint != null) { 069 return esHint.toString() + " " + name; 070 } 071 return name; 072 } 073 074 @Override 075 public boolean equals(Object obj) { 076 if (obj == this) { 077 return true; 078 } 079 if (!(obj instanceof Reference)) { 080 return false; 081 } 082 return equals((Reference) obj); 083 } 084 085 private boolean equals(Reference other) { 086 if (!name.equals(other.name)) { 087 return false; 088 } 089 if (cast != null || other.cast != null) { 090 return StringUtils.equals(cast, other.cast); 091 } 092 if (esHint != null) { 093 return esHint.equals(other.esHint); 094 } else if (other.esHint != null) { 095 return false; 096 } 097 return true; 098 } 099 100 @Override 101 public int hashCode() { 102 int result = 31 + (cast == null ? 0 : cast.hashCode()) + (esHint == null ? 0 : esHint.hashCode()); 103 return 31 * result + name.hashCode(); 104 } 105 106 public boolean isPathReference() { 107 return false; 108 } 109 110}