001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2006-2009 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.rolap.agg;
011
012import mondrian.rolap.*;
013import mondrian.rolap.sql.SqlQuery;
014
015import java.util.Collection;
016import java.util.List;
017
018/**
019 * A constraint which always returns true or false.
020 *
021 * @author jhyde
022 * @since Nov 2, 2006
023 */
024public class LiteralStarPredicate extends AbstractColumnPredicate {
025    private final boolean value;
026
027    public static final LiteralStarPredicate TRUE =
028        new LiteralStarPredicate(null, true);
029    public static final LiteralStarPredicate FALSE =
030        new LiteralStarPredicate(null, false);
031
032    /**
033     * Creates a LiteralStarPredicate.
034     *
035     * @param column Constrained column
036     * @param value Truth value
037     */
038    public LiteralStarPredicate(RolapStar.Column column, boolean value) {
039        super(column);
040        this.value = value;
041    }
042
043
044    public int hashCode() {
045        return value ? 2 : 1;
046    }
047
048    public boolean equals(Object obj) {
049        if (obj instanceof LiteralStarPredicate) {
050            LiteralStarPredicate that =
051                (LiteralStarPredicate) obj;
052            return this.value == that.value;
053        } else {
054            return false;
055        }
056    }
057
058    public boolean evaluate(List<Object> valueList) {
059        assert valueList.isEmpty();
060        return value;
061    }
062
063    public boolean equalConstraint(StarPredicate that) {
064        throw new UnsupportedOperationException();
065    }
066
067    public String toString() {
068        return Boolean.toString(value);
069    }
070
071    public void values(Collection<Object> collection) {
072        collection.add(value);
073    }
074
075    public boolean evaluate(Object value) {
076        return this.value;
077    }
078
079    public void describe(StringBuilder buf) {
080        buf.append("=any");
081    }
082
083    public Overlap intersect(
084        StarColumnPredicate predicate)
085    {
086        return new Overlap(value, null, 0f);
087    }
088
089    public boolean mightIntersect(StarPredicate other) {
090        // FALSE intersects nothing
091        // TRUE intersects everything except FALSE
092        if (!value) {
093            return false;
094        } else if (other instanceof LiteralStarPredicate) {
095            return ((LiteralStarPredicate) other).value;
096        } else {
097            return true;
098        }
099    }
100
101    public StarColumnPredicate minus(StarPredicate predicate) {
102        assert predicate != null;
103        if (value) {
104            // We have no 'not' operator, so there's no shorter way to represent
105            // "true - constraint".
106            return new MinusStarPredicate(
107                this, (StarColumnPredicate) predicate);
108        } else {
109            // "false - constraint" is "false"
110            return this;
111        }
112    }
113
114    public StarColumnPredicate cloneWithColumn(RolapStar.Column column) {
115        return this;
116    }
117
118    public boolean getValue() {
119        return value;
120    }
121
122    public void toSql(SqlQuery sqlQuery, StringBuilder buf) {
123        // e.g. "true"
124        buf.append(value);
125    }
126}
127
128// End LiteralStarPredicate.java