You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2021/09/19 10:30:18 UTC
[jackrabbit-filevault] branch master updated: add toString,
hashCode and equals
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git
The following commit(s) were added to refs/heads/master by this push:
new 0087391 add toString, hashCode and equals
0087391 is described below
commit 0087391e3e2dbe0f70fab5dcb1a283c354c13c25
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sun Sep 19 12:30:12 2021 +0200
add toString, hashCode and equals
add missing nullability annotations
---
.../jackrabbit/vault/util/EffectiveNodeType.java | 40 +++++++++++++++++-----
.../jackrabbit/vault/util/EffectiveNodeTypeIT.java | 12 +++++++
2 files changed, 44 insertions(+), 8 deletions(-)
diff --git a/vault-core/src/main/java/org/apache/jackrabbit/vault/util/EffectiveNodeType.java b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/EffectiveNodeType.java
index 69808a6..7ac2488 100644
--- a/vault-core/src/main/java/org/apache/jackrabbit/vault/util/EffectiveNodeType.java
+++ b/vault-core/src/main/java/org/apache/jackrabbit/vault/util/EffectiveNodeType.java
@@ -21,6 +21,7 @@ import static javax.jcr.PropertyType.UNDEFINED;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@@ -40,33 +41,56 @@ import org.jetbrains.annotations.Nullable;
* Effective node type as defined by <a href="https://docs.adobe.com/content/docs/en/spec/jcr/2.0/3_Repository_Model.html#3.7.6.5%20Effective%20Node%20Type">JCR 2.0, Chapter 3.7.6.5</a>.
* The order is an implementation detail (compare with <a href="https://docs.adobe.com/content/docs/en/spec/jcr/2.0/3_Repository_Model.html#3.7.7%20Applicable%20Item%20Definition">JCR 2.0, Chapter 3.7.7</a>)
* but this implementation replicates the logic from Oak:
- * <ul>
+ * <ol>
* <li>local before inherited types</li>
* <li>named primary types (even inherited ones) before named mixin types</li>
* <li>residual primary types (even inherited ones) before residual mixin types</li>
* <li>all named item definitions should be considered first (of both primary and mixins) and only afterwards the unnamed ones</li>
* <li>the first potential match wins (even if it is only for the undefined type and more type-specific definitions follow later)</li>
- * </ul>
+ * </ol>
*/
public final class EffectiveNodeType {
- public static EffectiveNodeType ofNode(Node node) throws RepositoryException {
+ public static EffectiveNodeType ofNode(@NotNull Node node) throws RepositoryException {
return ofPrimaryTypeAndMixins(node.getPrimaryNodeType(), node.getMixinNodeTypes());
}
- public static EffectiveNodeType ofPrimaryTypeAndMixins(NodeType primaryType, NodeType... mixinTypes) {
+ public static EffectiveNodeType ofPrimaryTypeAndMixins(@NotNull NodeType primaryType, NodeType... mixinTypes) {
List<NodeType> types = new ArrayList<>();
types.add(primaryType);
Arrays.stream(mixinTypes).forEach(types::add);
return new EffectiveNodeType(types);
}
- private final List<NodeType> nodeTypes;
+ private final @NotNull List<NodeType> nodeTypes;
- private EffectiveNodeType(List<NodeType> nodeTypes) {
+ private EffectiveNodeType(@NotNull List<NodeType> nodeTypes) {
this.nodeTypes = nodeTypes;
}
+ @Override
+ public int hashCode() {
+ return Objects.hash(nodeTypes);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ EffectiveNodeType other = (EffectiveNodeType) obj;
+ return Objects.equals(nodeTypes, other.nodeTypes);
+ }
+
+
+ @Override
+ public String toString() {
+ return "EffectiveNodeType [" + nodeTypes + "]";
+ }
+
/**
* Returns the applicable property definition for the given name and type.
* @param name the property name
@@ -104,7 +128,7 @@ public final class EffectiveNodeType {
return getApplicableChildNodeDefinition(nd -> Arrays.stream(nd.getRequiredPrimaryTypeNames()).allMatch(requiredPrimaryType -> Arrays.stream(types).anyMatch(providedType -> providedType.isNodeType(requiredPrimaryType))), name);
}
- public Optional<NodeDefinition> getApplicableChildNodeDefinition(Predicate<NodeDefinition> predicate, @NotNull String name) {
+ public Optional<NodeDefinition> getApplicableChildNodeDefinition(@NotNull Predicate<NodeDefinition> predicate, @NotNull String name) {
List<NodeDefinition> nodeDefinitions = nodeTypes.stream().flatMap(nt -> Arrays.stream(nt.getChildNodeDefinitions())).collect(Collectors.toList());
// first named then unnamed
Optional<NodeDefinition> namedNodeDef = EffectiveNodeType.<NodeDefinition>getApplicableItemDefinition(nodeDefinitions, predicate, name);
@@ -134,7 +158,7 @@ public final class EffectiveNodeType {
* @return the qualified name of the default primary type for the given intermediate node below parent
* @throws RepositoryException
*/
- public Optional<String> getDefaultPrimaryChildNodeTypeName(Node parent, String nodeName) throws RepositoryException {
+ public Optional<String> getDefaultPrimaryChildNodeTypeName(@NotNull Node parent, @NotNull String nodeName) throws RepositoryException {
Optional<NodeDefinition> nodeDefinition = getApplicableChildNodeDefinition(nd -> nd.getDefaultPrimaryType() != null, nodeName);
return nodeDefinition.map(NodeDefinition::getDefaultPrimaryTypeName);
}
diff --git a/vault-core/src/test/java/org/apache/jackrabbit/vault/util/EffectiveNodeTypeIT.java b/vault-core/src/test/java/org/apache/jackrabbit/vault/util/EffectiveNodeTypeIT.java
index 5f3e9cb..d33e45e 100644
--- a/vault-core/src/test/java/org/apache/jackrabbit/vault/util/EffectiveNodeTypeIT.java
+++ b/vault-core/src/test/java/org/apache/jackrabbit/vault/util/EffectiveNodeTypeIT.java
@@ -16,7 +16,9 @@
*/
package org.apache.jackrabbit.vault.util;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
@@ -106,4 +108,14 @@ public class EffectiveNodeTypeIT extends IntegrationTestBase {
assertTrue(nd.isPresent());
assertTrue(nd.get().isMandatory());
}
+
+ @Test
+ public void testEquals() throws RepositoryException {
+ EffectiveNodeType effectiveNodeType = EffectiveNodeType.ofNode(node);
+ EffectiveNodeType otherEffectiveNodeType = EffectiveNodeType.ofNode(node);
+ assertEquals(effectiveNodeType, otherEffectiveNodeType);
+ node.addMixin(MY_MIXIN);
+ otherEffectiveNodeType = EffectiveNodeType.ofNode(node);
+ assertNotEquals(effectiveNodeType, otherEffectiveNodeType);
+ }
}