001package org.clafer.objective;
002
003import org.clafer.ast.AstSetExpr;
004import org.clafer.common.Check;
005
006/**
007 * One objective.
008 *
009 * @author jimmy
010 */
011public class Objective {
012
013    private static int idFactory = 0;
014    private final int id = idFactory++;
015    // true - maximize
016    // false - minimize
017    private final boolean maximize;
018    private final AstSetExpr expr;
019
020    Objective(boolean maximize, AstSetExpr expr) {
021        this.maximize = maximize;
022        this.expr = Check.notNull(expr);
023    }
024
025    /**
026     * Check if this objective is a maximization objective.
027     *
028     * @return {@code true} if the objective is to maximize the expression,
029     * {@code false} otherwise
030     */
031    public boolean isMaximize() {
032        return maximize;
033    }
034
035    /**
036     * Check if this objective is a minimization objective.
037     *
038     * @return {@code true} if the objective is to minimize the expression,
039     * {@code false} otherwise
040     */
041    public boolean isMinimize() {
042        return !maximize;
043    }
044
045    /**
046     * Returns the expression to optimize.
047     *
048     * @return the expression to optimize
049     */
050    public AstSetExpr getExpr() {
051        return expr;
052    }
053
054    /**
055     * Create a new maximization objective.
056     *
057     * @param expression the expression
058     * @return maximize the expression
059     */
060    public static Objective maximize(AstSetExpr expression) {
061        return new Objective(true, expression);
062    }
063
064    /**
065     * Create a new minimization objective.
066     *
067     * @param expression the expression
068     * @return minimize the expression
069     */
070    public static Objective minimize(AstSetExpr expression) {
071        return new Objective(false, expression);
072    }
073
074    @Override
075    public boolean equals(Object obj) {
076        return this == obj;
077    }
078
079    @Override
080    public int hashCode() {
081        return id;
082    }
083
084    @Override
085    public String toString() {
086        return "<<" + (maximize ? "max " : "min ") + expr + ">>";
087    }
088}