001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.query.sql.model;
021
022/**
023 * @author Florent Guillaume
024 */
025public class OrderByExpr implements Operand {
026
027    private static final long serialVersionUID = 1L;
028
029    public final Reference reference;
030
031    public final boolean isDescending;
032
033    public OrderByExpr(Reference reference, boolean isDescending) {
034        this.reference = reference;
035        this.isDescending = isDescending;
036    }
037
038    @Override
039    public int hashCode() {
040        final int prime = 31;
041        int result = 1;
042        result = prime * result + (isDescending ? 1231 : 1237);
043        result = prime * result + (reference == null ? 0 : reference.hashCode());
044        return result;
045    }
046
047    @Override
048    public boolean equals(Object other) {
049        if (other == this) {
050            return true;
051        }
052        if (other instanceof OrderByExpr) {
053            return equals((OrderByExpr) other);
054        }
055        return false;
056    }
057
058    private boolean equals(OrderByExpr other) {
059        if (isDescending != other.isDescending) {
060            return false;
061        }
062        if (reference == null) {
063            if (other.reference != null) {
064                return false;
065            }
066        } else if (!reference.equals(other.reference)) {
067            return false;
068        }
069        return true;
070    }
071
072    @Override
073    public void accept(IVisitor visitor) {
074        visitor.visitOrderByExpr(this);
075    }
076
077    @Override
078    public String toString() {
079        if (isDescending) {
080            return reference.toString() + " DESC";
081        } else {
082            return reference.toString();
083        }
084    }
085
086}