Skip to content

Commit

Permalink
Improve small bits
Browse files Browse the repository at this point in the history
This includes automatic debug dump of embedded objects in composite
references.
  • Loading branch information
mederly committed Mar 9, 2024
1 parent 227719f commit 382abc0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,13 @@ default String getHelp() {

/**
* Returns the values for this item. Although the ordering of this values is not important, and each value should
* be present at most once, we currently return them as a list instead of a set. TODO reconsider this
* be present at most once, we currently return them as a list instead of a set.
*
* TODO reconsider this
*
* FIXME we should return immutable list, to avoid manipulating the values directly
*
* FIXME and maybe we should return List<? extends V> to avoid specializing the values, see ShadowAssociationValue in midPoint
*/
@NotNull
List<V> getValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
*/
package com.evolveum.midpoint.prism;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.xml.namespace.QName;
Expand All @@ -25,6 +22,8 @@
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.util.exception.SchemaException;

import static com.evolveum.midpoint.util.MiscUtil.stateCheck;

/**
* @author semancik
*/
Expand Down Expand Up @@ -346,6 +345,14 @@ static <C extends Containerable> Collection<C> asContainerables(Collection<Prism
*/
boolean hasNoItems();

/** For simplicity, everything must be qualified: names to check, and existing names. */
default void checkNothingExceptFor(QName... allowedItemNames) {
var actualItems = new HashSet<>(getItemNames());
var expectedItems = Set.of(allowedItemNames);
actualItems.removeAll(expectedItems);
stateCheck(actualItems.isEmpty(), "Unexpected items in %s: %s", this, actualItems);
}

/** Used when accessing an item whose definition was removed. To be used only at very specific places! */
class RemovedItemDefinitionException extends CommonException {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,4 @@ public interface PrismReference extends Item<PrismReferenceValue,PrismReferenceD
*/
@Experimental
<I extends Item<?,?>> I findReferencedItem(ItemPath path, Class<I> type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jetbrains.annotations.NotNull;

import javax.xml.namespace.QName;
import java.io.Serial;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -37,7 +38,7 @@
*
*/
public class PrismReferenceImpl extends ItemImpl<PrismReferenceValue, PrismReferenceDefinition> implements PrismReference {
private static final long serialVersionUID = 1872343401395762657L;
@Serial private static final long serialVersionUID = 1872343401395762657L;

@SuppressWarnings("unused") // called dynamically from ItemImpl.createNewDefinitionlessItem
public PrismReferenceImpl(QName name) {
Expand Down Expand Up @@ -247,11 +248,12 @@ public String toString() {
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);

PrismReferenceDefinition definition = getDefinition();
boolean multiline = true;
if (definition != null) {
multiline = definition.isMultiValue() || definition.isComposite();
}
boolean composite = definition != null && definition.isComposite();
boolean multivalue = definition != null && definition.isMultiValue();

boolean multiline = definition == null || multivalue || composite;
if (DebugUtil.isDetailedDebugDump()) {
sb.append(getDebugDumpClassName()).append(": ");
}
Expand All @@ -265,22 +267,26 @@ public String debugDump(int indent) {
sb.append(" def ");
}
for (PrismReferenceValue value : values) {
if (multiline) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent + 1);
}
if (DebugUtil.isDetailedDebugDump()) {
if (multiline) {
sb.append(value.debugDump(indent + 1, true));
} else {
sb.append(value.toString());
sb.append(value);
}
} else {
sb.append(value.toHumanReadableString());
if (composite) {
sb.append("\n");
sb.append(value.debugDump(indent + 1, true));
} else {
if (multiline) {
sb.append("\n");
DebugUtil.indentDebugDump(sb, indent + 1);
}
sb.append(value.toHumanReadableString());
}
}
}
}

return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ public String debugDump(int indent) {
public String debugDump(int indent, boolean expandObject) {
StringBuilder sb = new StringBuilder();
DebugUtil.indentDebugDump(sb, indent);
sb.append(toString());
sb.append(this);
if (expandObject && object != null) {
sb.append("\n");
sb.append(object.debugDump(indent + 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@
public interface Checkable {

void checkConsistence() throws SchemaException;

/** To throw unchecked exception in case of consistency check failure. */
default IllegalStateException checkFailedException(Throwable e) {
throw new IllegalStateException("Consistency check failed for " + this + ": " + e.getMessage(), e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -958,10 +958,16 @@ public static <T> List<T> singletonOrEmptyList(T value) {
}

public static <T> T castSafely(Object value, Class<T> expectedClass) throws SchemaException {
return castSafely(value, expectedClass, null);
}

public static <T> T castSafely(Object value, Class<T> expectedClass, Object errorCtx) throws SchemaException {
if (value == null) {
return null;
} else if (!expectedClass.isAssignableFrom(value.getClass())) {
throw new SchemaException("Expected '" + expectedClass.getName() + "' but got '" + value.getClass().getName() + "'");
var suffix = errorCtx != null ? " in " + errorCtx : "";
throw new SchemaException("Expected '%s' but got '%s'%s".formatted(
expectedClass.getName(), value.getClass().getName(), suffix));
} else {
//noinspection unchecked
return (T) value;
Expand Down

0 comments on commit 382abc0

Please sign in to comment.