001/* 002 * Copyright (c) 2006-2011 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 * Nuxeo - initial API and implementation 011 * 012 * $Id$ 013 */ 014 015package org.nuxeo.ecm.core.query.sql.model; 016 017import org.nuxeo.ecm.core.query.sql.NXQL; 018 019/** 020 * An infix expression. 021 * 022 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 023 */ 024public class Expression implements Operand { 025 026 private static final long serialVersionUID = 6007989243273673300L; 027 028 public final Operator operator; 029 030 public final Operand lvalue; 031 032 public final Operand rvalue; 033 034 public Expression(Operand lvalue, Operator operator, Operand rvalue) { 035 this.lvalue = lvalue; 036 this.rvalue = rvalue; 037 this.operator = operator; 038 } 039 040 @Override 041 public void accept(IVisitor visitor) { 042 visitor.visitExpression(this); 043 } 044 045 /** 046 * Is the unary operator pretty-printed after the operand? 047 */ 048 public boolean isSuffix() { 049 return operator == Operator.ISNULL || operator == Operator.ISNOTNULL; 050 } 051 052 @Override 053 public String toString() { 054 if (rvalue == null) { 055 if (isSuffix()) { 056 return lvalue.toString() + ' ' + operator.toString(); 057 } else { 058 return operator.toString() + ' ' + lvalue.toString(); 059 } 060 } else { 061 return lvalue.toString() + ' ' + operator.toString() + ' ' + rvalue.toString(); 062 } 063 } 064 065 @Override 066 public boolean equals(Object obj) { 067 if (obj == this) { 068 return true; 069 } 070 if (obj instanceof Expression) { 071 Expression e = (Expression) obj; 072 if (operator.id != e.operator.id) { 073 return false; 074 } 075 if (!lvalue.equals(e.lvalue)) { 076 return false; 077 } 078 if (rvalue != null) { 079 if (!rvalue.equals(e.rvalue)) { 080 return false; 081 } 082 } else if (e.rvalue != null) { 083 return false; 084 } 085 return true; 086 } 087 return false; 088 } 089 090 @Override 091 public int hashCode() { 092 int result = 17; 093 result = 37 * result + operator.hashCode(); 094 result = 37 * result + lvalue.hashCode(); 095 result = 37 * result + (rvalue == null ? 0 : rvalue.hashCode()); 096 return result; 097 } 098 099 public boolean isPathExpression() { 100 return (lvalue instanceof Reference) && NXQL.ECM_PATH.equals(((Reference) lvalue).name); 101 } 102 103}