Java tutorial
package com.conwet.silbops.model; /* * #%L * SilboPS API * %% * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import java.util.Set; import org.json.simple.JSONObject; import com.conwet.silbops.model.basic.Operator; import com.conwet.silbops.model.basic.Value; import com.conwet.silbops.util.JSONizable; /** * Represents a single constraint for an attribute. This class is not meant to * be used directly but as part of subscriptions. * * * <p>Immutable class.</p> * * @author sergio * @apiviz.owns com.conwet.silbops.model.basic.Operator * @apiviz.owns com.conwet.silbops.model.basic.Value */ public class Constraint implements JSONizable { /** * Constraint object representing Exist restriction */ public static final Constraint EXIST = new Constraint(Operator.EXISTS, null); /** * Constraint operator. */ private final Operator operator; /** * Value to compare with. */ private final Value value; /** * Create a new constraint with the given operator. * * @param operator Operator * @param value Operand whose operator must match the operator */ public Constraint(Operator operator, Value value) { operator = Objects.requireNonNull(operator, "Operator is null"); if (!operator.isValidOperand(value)) { throw new IllegalArgumentException("Operator " + operator + " isn't compatible with value " + value); } this.operator = operator; // EXISTS doesn't need a value this.value = operator == Operator.EXISTS ? null : value; } public Operator getOperator() { return operator; } public Value getValue() { return value; } /** * True if the constraint is satisfied by the value * * @param value Value to compare to * @return Whether the constraint is satisfied */ public boolean match(Value value) { return operator.compare(value, this.value); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof Constraint) { Constraint other = (Constraint) obj; return operator == other.operator && Objects.equals(value, other.value); } return false; } @Override public int hashCode() { return 41 * operator.hashCode() + Objects.hashCode(value); } @Override public String toString() { return "[operator=" + operator + ", value=" + value + "]"; } /** * Deserialize from a JSON representation * * @param json JSON representation * @return A new constraint */ public static Constraint fromJSON(JSONObject json) { @SuppressWarnings("unchecked") Set<Map.Entry<String, Object>> entries = json.entrySet(); if (entries.size() != 1) { throw new IllegalArgumentException("Malformed object: " + json); } Entry<String, Object> entry = entries.iterator().next(); Operator operator = Operator.fromJSON(entry.getKey()); return (operator == Operator.EXISTS) ? EXIST : new Constraint(operator, Value.fromJSON(entry.getValue())); } @Override @SuppressWarnings("unchecked") public JSONObject toJSON() { Object val = operator == Operator.EXISTS ? "" : value.toJSON(); JSONObject json = new JSONObject(); json.put(operator.toJSON(), val); return json; } @Override public String toJSONString() { return this.toJSON().toJSONString(); } }