001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.algebra;
007:
008: import java.util.ArrayList;
009: import java.util.List;
010:
011: import org.openrdf.query.algebra.QueryModelNode;
012: import org.openrdf.query.algebra.QueryModelVisitor;
013: import org.openrdf.sail.rdbms.algebra.base.RdbmsQueryModelNodeBase;
014: import org.openrdf.sail.rdbms.algebra.base.RdbmsQueryModelVisitorBase;
015: import org.openrdf.sail.rdbms.algebra.base.SqlExpr;
016:
017: /**
018: * The SQL CASE WHEN THEN END expression.
019: *
020: * @author James Leigh
021: *
022: */
023: public class SqlCase extends RdbmsQueryModelNodeBase implements SqlExpr {
024: private List<Entry> entries = new ArrayList<Entry>();
025:
026: public class Entry {
027: private SqlExpr condition;
028: private SqlExpr result;
029:
030: public Entry(SqlExpr condition, SqlExpr result) {
031: super ();
032: this .condition = condition;
033: this .result = result;
034: }
035:
036: public SqlExpr getCondition() {
037: return condition;
038: }
039:
040: public void setCondition(SqlExpr condition) {
041: this .condition = condition;
042: condition.setParentNode(SqlCase.this );
043: }
044:
045: public SqlExpr getResult() {
046: return result;
047: }
048:
049: public void setResult(SqlExpr result) {
050: this .result = result;
051: result.setParentNode(SqlCase.this );
052: }
053:
054: @Override
055: public int hashCode() {
056: final int prime = 31;
057: int result = 1;
058: result = prime * result
059: + ((condition == null) ? 0 : condition.hashCode());
060: result = prime
061: * result
062: + ((this .result == null) ? 0 : this .result
063: .hashCode());
064: return result;
065: }
066:
067: @Override
068: public boolean equals(Object obj) {
069: if (this == obj)
070: return true;
071: if (obj == null)
072: return false;
073: if (getClass() != obj.getClass())
074: return false;
075: final Entry other = (Entry) obj;
076: if (condition == null) {
077: if (other.condition != null)
078: return false;
079: } else if (!condition.equals(other.condition))
080: return false;
081: if (result == null) {
082: if (other.result != null)
083: return false;
084: } else if (!result.equals(other.result))
085: return false;
086: return true;
087: }
088: }
089:
090: public void when(SqlExpr condition, SqlExpr expr) {
091: entries.add(new Entry(condition, expr));
092: condition.setParentNode(this );
093: expr.setParentNode(this );
094: }
095:
096: public List<Entry> getEntries() {
097: return new ArrayList<Entry>(entries);
098: }
099:
100: @Override
101: public <X extends Exception> void visitChildren(
102: QueryModelVisitor<X> visitor) throws X {
103: for (Entry e : entries) {
104: e.getCondition().visit(visitor);
105: e.getResult().visit(visitor);
106: }
107: }
108:
109: @Override
110: public void replaceChildNode(QueryModelNode current,
111: QueryModelNode replacement) {
112: for (Entry e : entries) {
113: if (e.getCondition() == current) {
114: e.setCondition((SqlExpr) replacement);
115: } else if (e.getResult() == current) {
116: e.setResult((SqlExpr) replacement);
117: }
118: }
119: }
120:
121: @Override
122: public SqlCase clone() {
123: SqlCase clone = (SqlCase) super .clone();
124: clone.entries = new ArrayList<Entry>();
125: for (Entry e : entries) {
126: clone.when(e.getCondition().clone(), e.getResult().clone());
127: }
128: return clone;
129: }
130:
131: @Override
132: public <X extends Exception> void visit(
133: RdbmsQueryModelVisitorBase<X> visitor) throws X {
134: visitor.meet(this );
135: }
136:
137: public void removeEntry(Entry e) {
138: entries.remove(e);
139: }
140:
141: public void truncateEntries(Entry e) {
142: int idx = entries.indexOf(e) + 1;
143: if (idx < entries.size()) {
144: entries = entries.subList(0, idx);
145: }
146: }
147:
148: @Override
149: public int hashCode() {
150: final int prime = 31;
151: int result = 1;
152: result = prime * result
153: + ((entries == null) ? 0 : entries.hashCode());
154: return result;
155: }
156:
157: @Override
158: public boolean equals(Object obj) {
159: if (this == obj)
160: return true;
161: if (obj == null)
162: return false;
163: if (getClass() != obj.getClass())
164: return false;
165: final SqlCase other = (SqlCase) obj;
166: if (entries == null) {
167: if (other.entries != null)
168: return false;
169: } else if (!entries.equals(other.entries))
170: return false;
171: return true;
172: }
173:
174: }
|