Skip to content

Commit

Permalink
ValueFilterImpl.java: code cleanup and reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Sep 6, 2020
1 parent 77ee491 commit fe5cdac
Showing 1 changed file with 43 additions and 39 deletions.
@@ -1,30 +1,31 @@
/*
* Copyright (c) 2010-2019 Evolveum and contributors
* Copyright (C) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.prism.impl.query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.xml.namespace.QName;

import org.apache.commons.lang3.Validate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.match.MatchingRule;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.prism.path.*;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.ValueFilter;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.MiscUtil;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public abstract class ValueFilterImpl<V extends PrismValue, D extends ItemDefinition> extends ObjectFilterImpl implements
ValueFilter<V, D> {
Expand Down Expand Up @@ -88,13 +89,13 @@ public ItemName getElementName() {
return definition.getItemName(); // this is more precise, as the name in path can be unqualified
}
if (fullPath.isEmpty()) {
throw new IllegalStateException("Empty full path in filter "+this);
throw new IllegalStateException("Empty full path in filter " + this);
}
Object last = fullPath.last();
if (ItemPath.isName(last)) {
return ItemPath.toName(last);
} else {
throw new IllegalStateException("Got "+last+" as a last path segment in value filter "+this);
throw new IllegalStateException("Got " + last + " as a last path segment in value filter " + this);
}
}

Expand All @@ -121,11 +122,11 @@ public void setMatchingRule(@Nullable QName matchingRule) {
MatchingRule getMatchingRuleFromRegistry(MatchingRuleRegistry matchingRuleRegistry, Item filterItem) {
ItemDefinition itemDefinition = filterItem.getDefinition();
if (itemDefinition == null) {
throw new IllegalArgumentException("No definition in item "+filterItem);
throw new IllegalArgumentException("No definition in item " + filterItem);
}
try {
return matchingRuleRegistry.getMatchingRule(matchingRule, itemDefinition.getTypeName());
} catch (SchemaException ex){
} catch (SchemaException ex) {
throw new IllegalArgumentException(ex.getMessage(), ex);
}
}
Expand Down Expand Up @@ -233,7 +234,7 @@ public ItemPath getPath() {

public boolean isRaw() {
if (values != null) {
for (V value: values) {
for (V value : values) {
if (value.isRaw()) {
return true;
}
Expand Down Expand Up @@ -270,13 +271,13 @@ Collection<PrismValue> getObjectItemValues(PrismContainerValue value) {
// TODO revise
@NotNull
Item getFilterItem() throws SchemaException {
if (getDefinition() == null){
if (getDefinition() == null) {
throw new SchemaException("Could not find definition for item " + getPath());
}
Item filterItem = getDefinition().instantiate();
if (getValues() != null && !getValues().isEmpty()) {
try {
for (PrismValue v : getValues()){
for (PrismValue v : getValues()) {
filterItem.add(v.clone());
}
} catch (SchemaException e) {
Expand All @@ -297,16 +298,20 @@ public boolean equals(Object o) {

@Override
public boolean equals(Object o, boolean exact) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ValueFilterImpl<?, ?> that = (ValueFilterImpl<?, ?>) o;
return fullPath.equals(that.fullPath, exact) &&
(!exact || Objects.equals(definition, that.definition)) &&
Objects.equals(matchingRule, that.matchingRule) &&
MiscUtil.nullableCollectionsEqual(values, that.values) &&
Objects.equals(expression, that.expression) &&
(rightHandSidePath == null && that.rightHandSidePath == null ||
rightHandSidePath != null && rightHandSidePath.equals(that.rightHandSidePath, exact)) &&
rightHandSidePath != null && rightHandSidePath.equals(that.rightHandSidePath, exact)) &&
(!exact || Objects.equals(rightHandSideDefinition, that.rightHandSideDefinition));
}

Expand Down Expand Up @@ -335,12 +340,12 @@ public String toString() {

protected void debugDump(int indent, StringBuilder sb) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent+1);
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("PATH: ");
sb.append(getFullPath().toString());

sb.append("\n");
DebugUtil.indentDebugDump(sb, indent+1);
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("DEF: ");
if (getDefinition() != null) {
sb.append(getDefinition().toString());
Expand All @@ -351,7 +356,7 @@ protected void debugDump(int indent, StringBuilder sb) {
List<V> values = getValues();
if (values != null) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent+1);
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("VALUE:");
for (PrismValue val : getValues()) {
sb.append("\n");
Expand All @@ -362,19 +367,19 @@ protected void debugDump(int indent, StringBuilder sb) {
ExpressionWrapper expression = getExpression();
if (expression != null && expression.getExpression() != null) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent+1);
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("EXPRESSION:");
sb.append("\n");
sb.append(DebugUtil.debugDump(expression.getExpression(), indent + 2));
}

if (getRightHandSidePath() != null) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent+1);
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("RIGHT SIDE PATH: ");
sb.append(getFullPath().toString());
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent+1);
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("RIGHT SIDE DEF: ");
if (getRightHandSideDefinition() != null) {
sb.append(getRightHandSideDefinition().toString());
Expand All @@ -386,7 +391,7 @@ protected void debugDump(int indent, StringBuilder sb) {
QName matchingRule = getMatchingRule();
if (matchingRule != null) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent+1);
DebugUtil.indentDebugDump(sb, indent + 1);
sb.append("MATCHING: ");
sb.append(matchingRule);
}
Expand All @@ -396,14 +401,14 @@ protected String toString(StringBuilder sb) {
sb.append(getFullPath().toString());
sb.append(",");
if (getValues() != null) {
for (int i = 0; i< getValues().size() ; i++) {
for (int i = 0; i < getValues().size(); i++) {
PrismValue value = getValues().get(i);
if (value == null) {
sb.append("null");
} else {
sb.append(value.toString());
}
if (i != getValues().size() -1) {
if (i != getValues().size() - 1) {
sb.append(",");
}
}
Expand All @@ -417,17 +422,17 @@ protected String toString(StringBuilder sb) {
@Override
public void checkConsistence(boolean requireDefinitions) {
if (requireDefinitions && definition == null) {
throw new IllegalArgumentException("Null definition in "+this);
throw new IllegalArgumentException("Null definition in " + this);
}
if (fullPath.isEmpty()) {
throw new IllegalArgumentException("Empty path in "+this);
throw new IllegalArgumentException("Empty path in " + this);
}
Object last = fullPath.last();
if (!ItemPath.isName(last) && !ItemPath.isIdentifier(last)) {
throw new IllegalArgumentException("Last segment of item path is not a name or identifier segment: " + fullPath + " (it is " + last + ")");
}
if (rightHandSidePath != null && rightHandSidePath.isEmpty()) {
throw new IllegalArgumentException("Not-null but empty right side path in "+this);
throw new IllegalArgumentException("Not-null but empty right side path in " + this);
}
int count = 0;
if (values != null) {
Expand All @@ -444,15 +449,15 @@ public void checkConsistence(boolean requireDefinitions) {
+ "), expression (" + expression + "), rightHandSidePath (" + rightHandSidePath + ") in " + this);
}
if (values != null) {
for (V value: values) {
for (V value : values) {
if (value == null) {
throw new IllegalArgumentException("Null value in "+this);
throw new IllegalArgumentException("Null value in " + this);
}
if (value.getParent() != this) {
throw new IllegalArgumentException("Value "+value+" in "+this+" has a bad parent "+value.getParent());
throw new IllegalArgumentException("Value " + value + " in " + this + " has a bad parent " + value.getParent());
}
if (value.isEmpty() && !value.isRaw()) {
throw new IllegalArgumentException("Empty value in "+this);
throw new IllegalArgumentException("Empty value in " + this);
}
}
}
Expand All @@ -464,5 +469,4 @@ public void checkConsistence(boolean requireDefinitions) {
// todo check consistence for ID-based filters
}
}

}

0 comments on commit fe5cdac

Please sign in to comment.