[New] Added a StrolchQuery.hasSelection() and Selection.hasSelection()

This allows for checking if a query really has a selection.
This commit is contained in:
Robert von Burg 2014-07-15 18:50:45 +02:00
parent c8cedae647
commit c246c76236
6 changed files with 51 additions and 2 deletions

View File

@ -45,6 +45,20 @@ public abstract class BooleanSelection implements Selection {
this.selections = Arrays.asList(selections); this.selections = Arrays.asList(selections);
} }
@Override
public boolean hasSelection() {
if (this.selections == null || this.selections.isEmpty())
return false;
for (Selection selection : this.selections) {
if (selection.hasSelection()) {
return true;
}
}
return false;
}
public List<Selection> getSelections() { public List<Selection> getSelections() {
return Collections.unmodifiableList(this.selections); return Collections.unmodifiableList(this.selections);
} }

View File

@ -17,7 +17,7 @@ package li.strolch.model.query;
/** /**
* @author Robert von Burg <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
* *
*/ */
public abstract class OrderSelection implements Selection { public abstract class OrderSelection implements Selection {
@ -26,5 +26,10 @@ public abstract class OrderSelection implements Selection {
accept((OrderSelectionVisitor) visitor); accept((OrderSelectionVisitor) visitor);
} }
@Override
public boolean hasSelection() {
return true;
}
public abstract void accept(OrderSelectionVisitor visitor); public abstract void accept(OrderSelectionVisitor visitor);
} }

View File

@ -45,11 +45,27 @@ public abstract class ParameterSelection implements Selection {
return this.paramKey; return this.paramKey;
} }
@Override
public boolean hasSelection() {
return true;
}
@Override @Override
public void accept(QueryVisitor visitor) { public void accept(QueryVisitor visitor) {
accept((ParameterSelectionVisitor) visitor); accept((ParameterSelectionVisitor) visitor);
} }
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(getClass().getSimpleName() + " [bagKey=");
builder.append(this.bagKey);
builder.append(", paramKey=");
builder.append(this.paramKey);
builder.append("]");
return builder.toString();
}
public abstract void accept(ParameterSelectionVisitor visitor); public abstract void accept(ParameterSelectionVisitor visitor);
public static StringParameterSelection stringSelection(String bagKey, String paramKey, String value) { public static StringParameterSelection stringSelection(String bagKey, String paramKey, String value) {

View File

@ -20,6 +20,7 @@ package li.strolch.model.query;
*/ */
public interface Selection { public interface Selection {
// marker interface
public void accept(QueryVisitor visitor); public void accept(QueryVisitor visitor);
public boolean hasSelection();
} }

View File

@ -25,5 +25,10 @@ public abstract class StrolchElementSelection implements Selection {
accept((StrolchElementSelectionVisitor) visitor); accept((StrolchElementSelectionVisitor) visitor);
} }
@Override
public boolean hasSelection() {
return true;
}
public abstract void accept(StrolchElementSelectionVisitor visitor); public abstract void accept(StrolchElementSelectionVisitor visitor);
} }

View File

@ -28,6 +28,14 @@ public abstract class StrolchQuery<T extends QueryVisitor> {
public StrolchQuery(Navigation navigation) { public StrolchQuery(Navigation navigation) {
this.navigation = navigation; this.navigation = navigation;
} }
public boolean hasNavigation() {
return navigation != null;
}
public boolean hasSelection() {
return selection != null && selection.hasSelection();
}
public void select(Selection selection) { public void select(Selection selection) {
DBC.PRE.assertNull("A selection is already set! Use a boolean operator to perform multiple selections", DBC.PRE.assertNull("A selection is already set! Use a boolean operator to perform multiple selections",