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 *     Florent Guillaume
011 */
012
013package org.nuxeo.ecm.core.query.sql.model;
014
015/**
016 * @author Florent Guillaume
017 */
018public class OrderByExpr implements Operand {
019
020    private static final long serialVersionUID = 1L;
021
022    public final Reference reference;
023
024    public final boolean isDescending;
025
026    public OrderByExpr(Reference reference, boolean isDescending) {
027        this.reference = reference;
028        this.isDescending = isDescending;
029    }
030
031    @Override
032    public int hashCode() {
033        final int prime = 31;
034        int result = 1;
035        result = prime * result + (isDescending ? 1231 : 1237);
036        result = prime * result + (reference == null ? 0 : reference.hashCode());
037        return result;
038    }
039
040    @Override
041    public boolean equals(Object other) {
042        if (other == this) {
043            return true;
044        }
045        if (other instanceof OrderByExpr) {
046            return equals((OrderByExpr) other);
047        }
048        return false;
049    }
050
051    private boolean equals(OrderByExpr other) {
052        if (isDescending != other.isDescending) {
053            return false;
054        }
055        if (reference == null) {
056            if (other.reference != null) {
057                return false;
058            }
059        } else if (!reference.equals(other.reference)) {
060            return false;
061        }
062        return true;
063    }
064
065    @Override
066    public void accept(IVisitor visitor) {
067        visitor.visitOrderByExpr(this);
068    }
069
070    @Override
071    public String toString() {
072        if (isDescending) {
073            return reference.toString() + " DESC";
074        } else {
075            return reference.toString();
076        }
077    }
078
079}